2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5 * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
6 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #ifdef HAVE_KERNEL_OPTION_HEADERS
35 #include <dev/sound/pcm/sound.h>
36 #include <sys/ctype.h>
38 #include <sys/rwlock.h>
39 #include <sys/sysent.h>
42 #include <vm/vm_object.h>
43 #include <vm/vm_page.h>
44 #include <vm/vm_pager.h>
46 SND_DECLARE_FILE("$FreeBSD$");
48 static int dsp_mmap_allow_prot_exec = 0;
49 SYSCTL_INT(_hw_snd, OID_AUTO, compat_linux_mmap, CTLFLAG_RWTUN,
50 &dsp_mmap_allow_prot_exec, 0,
51 "linux mmap compatibility (-1=force disable 0=auto 1=force enable)");
53 static int dsp_basename_clone = 1;
54 SYSCTL_INT(_hw_snd, OID_AUTO, basename_clone, CTLFLAG_RWTUN,
55 &dsp_basename_clone, 0,
56 "DSP basename cloning (0: Disable; 1: Enabled)");
59 struct pcm_channel *rdch, *wrch;
60 struct pcm_channel *volch;
62 TAILQ_ENTRY(dsp_cdevinfo) link;
65 #define PCM_RDCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->rdch)
66 #define PCM_WRCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->wrch)
67 #define PCM_VOLCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->volch)
68 #define PCM_SIMPLEX(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->simplex)
70 #define DSP_CDEVINFO_CACHESIZE 8
72 #define DSP_REGISTERED(x, y) (PCM_REGISTERED(x) && \
73 (y) != NULL && (y)->si_drv1 != NULL)
77 static d_open_t dsp_open;
78 static d_close_t dsp_close;
79 static d_read_t dsp_read;
80 static d_write_t dsp_write;
81 static d_ioctl_t dsp_ioctl;
82 static d_poll_t dsp_poll;
83 static d_mmap_t dsp_mmap;
84 static d_mmap_single_t dsp_mmap_single;
86 struct cdevsw dsp_cdevsw = {
87 .d_version = D_VERSION,
95 .d_mmap_single = dsp_mmap_single,
99 static eventhandler_tag dsp_ehtag = NULL;
100 static int dsp_umax = -1;
101 static int dsp_cmax = -1;
103 static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group);
104 static int dsp_oss_syncstart(int sg_id);
105 static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy);
106 static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled);
107 static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
108 static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
109 static int dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch, int *mask);
110 #ifdef OSSV4_EXPERIMENT
111 static int dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
112 static int dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
113 static int dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
114 static int dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
115 static int dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name);
118 static struct snddev_info *
119 dsp_get_info(struct cdev *dev)
121 return (devclass_get_softc(pcm_devclass, PCMUNIT(dev)));
125 dsp_get_flags(struct cdev *dev)
129 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
131 return ((bdev != NULL) ? pcm_getflags(bdev) : 0xffffffff);
135 dsp_set_flags(struct cdev *dev, uint32_t flags)
139 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
142 pcm_setflags(bdev, flags);
146 * return the channels associated with an open device instance.
147 * lock channels specified.
150 getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch,
153 struct snddev_info *d;
154 struct pcm_channel *ch;
157 if (PCM_SIMPLEX(dev) != 0) {
158 d = dsp_get_info(dev);
159 if (!PCM_REGISTERED(d))
165 * Note: order is important -
166 * pcm flags -> prio query flags -> wild guess
169 flags = dsp_get_flags(dev);
170 if (flags & SD_F_PRIO_WR) {
172 PCM_RDCH(dev) = NULL;
173 } else if (flags & SD_F_PRIO_RD) {
175 PCM_WRCH(dev) = NULL;
176 } else if (prio & SD_F_PRIO_WR) {
178 PCM_RDCH(dev) = NULL;
179 flags |= SD_F_PRIO_WR;
180 } else if (prio & SD_F_PRIO_RD) {
182 PCM_WRCH(dev) = NULL;
183 flags |= SD_F_PRIO_RD;
184 } else if (PCM_WRCH(dev) != NULL) {
186 PCM_RDCH(dev) = NULL;
187 flags |= SD_F_PRIO_WR;
188 } else if (PCM_RDCH(dev) != NULL) {
190 PCM_WRCH(dev) = NULL;
191 flags |= SD_F_PRIO_RD;
193 PCM_SIMPLEX(dev) = 0;
194 dsp_set_flags(dev, flags);
204 *rdch = PCM_RDCH(dev);
205 *wrch = PCM_WRCH(dev);
207 if (*rdch != NULL && (prio & SD_F_PRIO_RD))
209 if (*wrch != NULL && (prio & SD_F_PRIO_WR))
215 /* unlock specified channels */
217 relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch,
220 if (wrch != NULL && (prio & SD_F_PRIO_WR))
222 if (rdch != NULL && (prio & SD_F_PRIO_RD))
227 dsp_cdevinfo_alloc(struct cdev *dev,
228 struct pcm_channel *rdch, struct pcm_channel *wrch,
229 struct pcm_channel *volch)
231 struct snddev_info *d;
232 struct dsp_cdevinfo *cdi;
235 d = dsp_get_info(dev);
237 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 == NULL &&
238 ((rdch == NULL && wrch == NULL) || rdch != wrch),
239 ("bogus %s(), what are you trying to accomplish here?", __func__));
243 simplex = (dsp_get_flags(dev) & SD_F_SIMPLEX) ? 1 : 0;
246 * Scan for free instance entry and put it into the end of list.
247 * Create new one if necessary.
249 TAILQ_FOREACH(cdi, &d->dsp_cdevinfo_pool, link) {
255 cdi->simplex = simplex;
257 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
258 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
263 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
268 cdi->simplex = simplex;
270 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
275 dsp_cdevinfo_free(struct cdev *dev)
277 struct snddev_info *d;
278 struct dsp_cdevinfo *cdi, *tmp;
282 d = dsp_get_info(dev);
284 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 != NULL &&
285 PCM_RDCH(dev) == NULL && PCM_WRCH(dev) == NULL &&
286 PCM_VOLCH(dev) == NULL,
287 ("bogus %s(), what are you trying to accomplish here?", __func__));
300 * Once it is free, move it back to the beginning of list for
301 * faster new entry allocation.
303 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
304 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
307 * Scan the list, cache free entries up to DSP_CDEVINFO_CACHESIZE.
308 * Reset simplex flags.
310 flags = dsp_get_flags(dev) & ~SD_F_PRIO_SET;
311 i = DSP_CDEVINFO_CACHESIZE;
312 TAILQ_FOREACH_SAFE(cdi, &d->dsp_cdevinfo_pool, link, tmp) {
313 if (cdi->busy != 0) {
314 if (cdi->simplex == 0) {
315 if (cdi->rdch != NULL)
316 flags |= SD_F_PRIO_RD;
317 if (cdi->wrch != NULL)
318 flags |= SD_F_PRIO_WR;
322 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
328 dsp_set_flags(dev, flags);
332 dsp_cdevinfo_init(struct snddev_info *d)
334 struct dsp_cdevinfo *cdi;
337 KASSERT(d != NULL, ("NULL snddev_info"));
341 TAILQ_INIT(&d->dsp_cdevinfo_pool);
342 for (i = 0; i < DSP_CDEVINFO_CACHESIZE; i++) {
343 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
344 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
349 dsp_cdevinfo_flush(struct snddev_info *d)
351 struct dsp_cdevinfo *cdi, *tmp;
353 KASSERT(d != NULL, ("NULL snddev_info"));
357 cdi = TAILQ_FIRST(&d->dsp_cdevinfo_pool);
358 while (cdi != NULL) {
359 tmp = TAILQ_NEXT(cdi, link);
363 TAILQ_INIT(&d->dsp_cdevinfo_pool);
366 /* duplex / simplex cdev type */
368 DSP_CDEV_TYPE_RDONLY, /* simplex read-only (record) */
369 DSP_CDEV_TYPE_WRONLY, /* simplex write-only (play) */
370 DSP_CDEV_TYPE_RDWR /* duplex read, write, or both */
374 DSP_CDEV_VOLCTL_NONE,
375 DSP_CDEV_VOLCTL_READ,
376 DSP_CDEV_VOLCTL_WRITE
379 #define DSP_F_VALID(x) ((x) & (FREAD | FWRITE))
380 #define DSP_F_DUPLEX(x) (((x) & (FREAD | FWRITE)) == (FREAD | FWRITE))
381 #define DSP_F_SIMPLEX(x) (!DSP_F_DUPLEX(x))
382 #define DSP_F_READ(x) ((x) & FREAD)
383 #define DSP_F_WRITE(x) ((x) & FWRITE)
385 static const struct {
397 { SND_DEV_DSP, "dsp", ".", NULL, 0, 0, 0, 0,
398 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
399 DSP_CDEV_TYPE_RDWR },
400 { SND_DEV_AUDIO, "audio", ".", NULL, 0, 0, 0, 0,
401 SND_FORMAT(AFMT_MU_LAW, 1, 0), DSP_DEFAULT_SPEED,
402 DSP_CDEV_TYPE_RDWR },
403 { SND_DEV_DSP16, "dspW", ".", NULL, 0, 0, 0, 0,
404 SND_FORMAT(AFMT_S16_LE, 1, 0), DSP_DEFAULT_SPEED,
405 DSP_CDEV_TYPE_RDWR },
406 { SND_DEV_DSPHW_PLAY, "dsp", ".p", NULL, 1, 1, SND_MAXHWCHAN, 1,
407 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
408 { SND_DEV_DSPHW_VPLAY, "dsp", ".vp", NULL, 1, 1, SND_MAXVCHANS, 1,
409 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
410 { SND_DEV_DSPHW_REC, "dsp", ".r", NULL, 1, 1, SND_MAXHWCHAN, 1,
411 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
412 { SND_DEV_DSPHW_VREC, "dsp", ".vr", NULL, 1, 1, SND_MAXVCHANS, 1,
413 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
414 { SND_DEV_DSPHW_CD, "dspcd", ".", NULL, 0, 0, 0, 0,
415 SND_FORMAT(AFMT_S16_LE, 2, 0), 44100, DSP_CDEV_TYPE_RDWR },
416 /* Low priority, OSSv4 aliases. */
417 { SND_DEV_DSP, "dsp_ac3", ".", "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_mmap", ".", "dsp", 0, 0, 0, 0,
421 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
422 DSP_CDEV_TYPE_RDWR },
423 { SND_DEV_DSP, "dsp_multich", ".", "dsp", 0, 0, 0, 0,
424 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
425 DSP_CDEV_TYPE_RDWR },
426 { SND_DEV_DSP, "dsp_spdifout", ".", "dsp", 0, 0, 0, 0,
427 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
428 DSP_CDEV_TYPE_RDWR },
429 { SND_DEV_DSP, "dsp_spdifin", ".", "dsp", 0, 0, 0, 0,
430 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
431 DSP_CDEV_TYPE_RDWR },
434 #define DSP_FIXUP_ERROR() do { \
435 prio = dsp_get_flags(i_dev); \
436 if (!DSP_F_VALID(flags)) \
438 if (!DSP_F_DUPLEX(flags) && \
439 ((DSP_F_READ(flags) && d->reccount == 0) || \
440 (DSP_F_WRITE(flags) && d->playcount == 0))) \
442 else if (!DSP_F_DUPLEX(flags) && (prio & SD_F_SIMPLEX) && \
443 ((DSP_F_READ(flags) && (prio & SD_F_PRIO_WR)) || \
444 (DSP_F_WRITE(flags) && (prio & SD_F_PRIO_RD)))) \
446 else if (DSP_REGISTERED(d, i_dev)) \
451 dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
453 struct pcm_channel *rdch, *wrch;
454 struct snddev_info *d;
455 uint32_t fmt, spd, prio, volctl;
456 int i, error, rderror, wrerror, devtype, wdevunit, rdevunit;
458 /* Kind of impossible.. */
459 if (i_dev == NULL || td == NULL)
462 d = dsp_get_info(i_dev);
463 if (PCM_DETACHING(d) || !PCM_REGISTERED(d))
468 /* Lock snddev so nobody else can monkey with it. */
473 * Try to acquire cloned device before someone else pick it.
474 * ENODEV means this is not a cloned droids.
476 error = snd_clone_acquire(i_dev);
477 if (!(error == 0 || error == ENODEV)) {
488 (void)snd_clone_release(i_dev);
495 * That is just enough. Acquire and unlock pcm lock so
496 * the other will just have to wait until we finish doing
502 devtype = PCMDEV(i_dev);
507 volctl = DSP_CDEV_VOLCTL_NONE;
509 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
510 if (devtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
513 * Volume control only valid for DSPHW devices,
514 * and it must be opened in opposite direction be it
515 * simplex or duplex. Anything else will be handled
518 if (dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY) {
519 if (dsp_cdevs[i].volctl != 0 &&
521 volctl = DSP_CDEV_VOLCTL_WRITE;
525 if (DSP_F_READ(flags)) {
526 (void)snd_clone_release(i_dev);
527 PCM_RELEASE_QUICK(d);
531 wdevunit = dev2unit(i_dev);
532 } else if (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY) {
533 if (dsp_cdevs[i].volctl != 0 &&
534 DSP_F_WRITE(flags)) {
535 volctl = DSP_CDEV_VOLCTL_READ;
539 if (DSP_F_WRITE(flags)) {
540 (void)snd_clone_release(i_dev);
541 PCM_RELEASE_QUICK(d);
545 rdevunit = dev2unit(i_dev);
547 fmt = dsp_cdevs[i].fmt;
548 spd = dsp_cdevs[i].spd;
552 /* No matching devtype? */
553 if (fmt == 0 || spd == 0)
554 panic("impossible devtype %d", devtype);
562 * if we get here, the open request is valid- either:
563 * * we were previously not open
564 * * we were open for play xor record and the opener wants
565 * the non-open direction
567 if (DSP_F_READ(flags)) {
569 rderror = pcm_chnalloc(d, &rdch, PCMDIR_REC,
570 td->td_proc->p_pid, td->td_proc->p_comm, rdevunit);
572 if (rderror == 0 && chn_reset(rdch, fmt, spd) != 0)
575 if (volctl == DSP_CDEV_VOLCTL_READ)
580 pcm_chnrelease(rdch);
581 if (!DSP_F_DUPLEX(flags)) {
582 (void)snd_clone_release(i_dev);
583 PCM_RELEASE_QUICK(d);
588 } else if (volctl == DSP_CDEV_VOLCTL_READ) {
591 pcm_chnrelease(rdch);
594 if (flags & O_NONBLOCK)
595 rdch->flags |= CHN_F_NBIO;
597 rdch->flags |= CHN_F_EXCLUSIVE;
599 if (volctl == DSP_CDEV_VOLCTL_NONE)
600 chn_vpc_reset(rdch, SND_VOL_C_PCM, 0);
605 if (DSP_F_WRITE(flags)) {
607 wrerror = pcm_chnalloc(d, &wrch, PCMDIR_PLAY,
608 td->td_proc->p_pid, td->td_proc->p_comm, wdevunit);
610 if (wrerror == 0 && chn_reset(wrch, fmt, spd) != 0)
613 if (volctl == DSP_CDEV_VOLCTL_WRITE)
618 pcm_chnrelease(wrch);
619 if (!DSP_F_DUPLEX(flags)) {
622 * Lock, deref and release previously
623 * created record channel
626 pcm_chnref(rdch, -1);
627 pcm_chnrelease(rdch);
629 (void)snd_clone_release(i_dev);
630 PCM_RELEASE_QUICK(d);
635 } else if (volctl == DSP_CDEV_VOLCTL_WRITE) {
638 pcm_chnrelease(wrch);
641 if (flags & O_NONBLOCK)
642 wrch->flags |= CHN_F_NBIO;
644 wrch->flags |= CHN_F_EXCLUSIVE;
646 if (volctl == DSP_CDEV_VOLCTL_NONE)
647 chn_vpc_reset(wrch, SND_VOL_C_PCM, 0);
655 * We're done. Allocate channels information for this cdev.
658 case DSP_CDEV_VOLCTL_READ:
659 KASSERT(wrch == NULL, ("wrch=%p not null!", wrch));
660 dsp_cdevinfo_alloc(i_dev, NULL, NULL, rdch);
662 case DSP_CDEV_VOLCTL_WRITE:
663 KASSERT(rdch == NULL, ("rdch=%p not null!", rdch));
664 dsp_cdevinfo_alloc(i_dev, NULL, NULL, wrch);
666 case DSP_CDEV_VOLCTL_NONE:
668 if (wrch == NULL && rdch == NULL) {
669 (void)snd_clone_release(i_dev);
679 dsp_cdevinfo_alloc(i_dev, rdch, wrch, NULL);
681 CHN_INSERT_HEAD(d, rdch, channels.pcm.opened);
683 CHN_INSERT_HEAD(d, wrch, channels.pcm.opened);
688 * Increase clone refcount for its automatic garbage collector.
690 (void)snd_clone_ref(i_dev);
701 dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
703 struct pcm_channel *rdch, *wrch, *volch;
704 struct snddev_info *d;
705 int sg_ids, rdref, wdref;
707 d = dsp_get_info(i_dev);
708 if (!DSP_REGISTERED(d, i_dev))
717 rdch = PCM_RDCH(i_dev);
718 wrch = PCM_WRCH(i_dev);
719 volch = PCM_VOLCH(i_dev);
721 PCM_RDCH(i_dev) = NULL;
722 PCM_WRCH(i_dev) = NULL;
723 PCM_VOLCH(i_dev) = NULL;
731 else if (volch == wrch)
735 pcm_chnref(volch, -1);
741 CHN_REMOVE(d, rdch, channels.pcm.opened);
743 CHN_REMOVE(d, wrch, channels.pcm.opened);
745 if (rdch != NULL || wrch != NULL) {
749 * The channel itself need not be locked because:
750 * a) Adding a channel to a syncgroup happens only
751 * in dsp_ioctl(), which cannot run concurrently
753 * b) The syncmember pointer (sm) is protected by
754 * the global syncgroup list lock.
755 * c) A channel can't just disappear, invalidating
756 * pointers, unless it's closed/dereferenced
760 sg_ids = chn_syncdestroy(rdch);
763 free_unr(pcmsg_unrhdr, sg_ids);
766 pcm_chnref(rdch, rdref);
767 chn_abort(rdch); /* won't sleep */
768 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
769 CHN_F_DEAD | CHN_F_EXCLUSIVE);
770 chn_reset(rdch, 0, 0);
771 pcm_chnrelease(rdch);
775 * Please see block above.
778 sg_ids = chn_syncdestroy(wrch);
781 free_unr(pcmsg_unrhdr, sg_ids);
784 pcm_chnref(wrch, wdref);
785 chn_flush(wrch); /* may sleep */
786 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
787 CHN_F_DEAD | CHN_F_EXCLUSIVE);
788 chn_reset(wrch, 0, 0);
789 pcm_chnrelease(wrch);
794 dsp_cdevinfo_free(i_dev);
796 * Release clone busy state and unref it so the automatic
797 * garbage collector will get the hint and do the remaining
800 (void)snd_clone_release(i_dev);
803 * destroy_dev() might sleep, so release pcm lock
804 * here and rely on pcm cv serialization.
807 (void)snd_clone_unref(i_dev);
819 dsp_io_ops(struct cdev *i_dev, struct uio *buf)
821 struct snddev_info *d;
822 struct pcm_channel **ch, *rdch, *wrch;
823 int (*chn_io)(struct pcm_channel *, struct uio *);
827 KASSERT(i_dev != NULL && buf != NULL &&
828 (buf->uio_rw == UIO_READ || buf->uio_rw == UIO_WRITE),
829 ("%s(): io train wreck!", __func__));
831 d = dsp_get_info(i_dev);
832 if (PCM_DETACHING(d) || !DSP_REGISTERED(d, i_dev))
837 switch (buf->uio_rw) {
849 panic("invalid/corrupted uio direction: %d", buf->uio_rw);
855 runpid = buf->uio_td->td_proc->p_pid;
857 getchns(i_dev, &rdch, &wrch, prio);
859 if (*ch == NULL || !((*ch)->flags & CHN_F_BUSY)) {
860 if (rdch != NULL || wrch != NULL)
861 relchns(i_dev, rdch, wrch, prio);
866 if (((*ch)->flags & (CHN_F_MMAP | CHN_F_DEAD)) ||
867 (((*ch)->flags & CHN_F_RUNNING) && (*ch)->pid != runpid)) {
868 relchns(i_dev, rdch, wrch, prio);
871 } else if (!((*ch)->flags & CHN_F_RUNNING)) {
872 (*ch)->flags |= CHN_F_RUNNING;
877 * chn_read/write must give up channel lock in order to copy bytes
878 * from/to userland, so up the "in progress" counter to make sure
879 * someone else doesn't come along and muss up the buffer.
882 ret = chn_io(*ch, buf);
885 CHN_BROADCAST(&(*ch)->cv);
887 relchns(i_dev, rdch, wrch, prio);
895 dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
897 return (dsp_io_ops(i_dev, buf));
901 dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
903 return (dsp_io_ops(i_dev, buf));
907 dsp_get_volume_channel(struct cdev *dev, struct pcm_channel **volch)
909 struct snddev_info *d;
910 struct pcm_channel *c;
913 KASSERT(dev != NULL && volch != NULL,
914 ("%s(): NULL query dev=%p volch=%p", __func__, dev, volch));
916 d = dsp_get_info(dev);
917 if (!PCM_REGISTERED(d)) {
928 if (!(c->feederflags & (1 << FEEDER_VOLUME)))
938 unit = dev2unit(dev);
940 CHN_FOREACH(c, d, channels.pcm) {
942 if (c->unit != unit) {
952 return ((c->feederflags & (1 << FEEDER_VOLUME)) ? 0 : -1);
962 dsp_ioctl_channel(struct cdev *dev, struct pcm_channel *volch, u_long cmd,
965 struct snddev_info *d;
966 struct pcm_channel *rdch, *wrch;
968 int left, right, center, mute;
970 d = dsp_get_info(dev);
971 if (!PCM_REGISTERED(d) || !(dsp_get_flags(dev) & SD_F_VPC))
978 rdch = PCM_RDCH(dev);
979 wrch = PCM_WRCH(dev);
981 /* No specific channel, look into cache */
983 volch = PCM_VOLCH(dev);
987 if (j == SOUND_MIXER_RECLEV && rdch != NULL)
989 else if (j == SOUND_MIXER_PCM && wrch != NULL)
993 devtype = PCMDEV(dev);
995 /* Look super harder */
997 (devtype == SND_DEV_DSPHW_PLAY || devtype == SND_DEV_DSPHW_VPLAY ||
998 devtype == SND_DEV_DSPHW_REC || devtype == SND_DEV_DSPHW_VREC)) {
999 ret = dsp_get_volume_channel(dev, &volch);
1006 /* Final validation */
1011 if (!(volch->feederflags & (1 << FEEDER_VOLUME))) {
1016 switch (cmd & ~0xff) {
1017 case MIXER_WRITE(0):
1019 case SOUND_MIXER_MUTE:
1020 if (volch->direction == PCMDIR_REC) {
1021 chn_setmute_multi(volch, SND_VOL_C_PCM, (*(int *)arg & SOUND_MASK_RECLEV) != 0);
1023 chn_setmute_multi(volch, SND_VOL_C_PCM, (*(int *)arg & SOUND_MASK_PCM) != 0);
1026 case SOUND_MIXER_PCM:
1027 if (volch->direction != PCMDIR_PLAY)
1029 left = *(int *)arg & 0x7f;
1030 right = ((*(int *)arg) >> 8) & 0x7f;
1031 center = (left + right) >> 1;
1032 chn_setvolume_multi(volch, SND_VOL_C_PCM,
1033 left, right, center);
1035 case SOUND_MIXER_RECLEV:
1036 if (volch->direction != PCMDIR_REC)
1038 left = *(int *)arg & 0x7f;
1039 right = ((*(int *)arg) >> 8) & 0x7f;
1040 center = (left + right) >> 1;
1041 chn_setvolume_multi(volch, SND_VOL_C_PCM,
1042 left, right, center);
1045 /* ignore all other mixer writes */
1052 case SOUND_MIXER_MUTE:
1053 mute = CHN_GETMUTE(volch, SND_VOL_C_PCM, SND_CHN_T_FL) ||
1054 CHN_GETMUTE(volch, SND_VOL_C_PCM, SND_CHN_T_FR);
1055 if (volch->direction == PCMDIR_REC) {
1056 *(int *)arg = mute << SOUND_MIXER_RECLEV;
1058 *(int *)arg = mute << SOUND_MIXER_PCM;
1061 case SOUND_MIXER_PCM:
1062 if (volch->direction != PCMDIR_PLAY)
1064 *(int *)arg = CHN_GETVOLUME(volch,
1065 SND_VOL_C_PCM, SND_CHN_T_FL);
1066 *(int *)arg |= CHN_GETVOLUME(volch,
1067 SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
1069 case SOUND_MIXER_RECLEV:
1070 if (volch->direction != PCMDIR_REC)
1072 *(int *)arg = CHN_GETVOLUME(volch,
1073 SND_VOL_C_PCM, SND_CHN_T_FL);
1074 *(int *)arg |= CHN_GETVOLUME(volch,
1075 SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
1077 case SOUND_MIXER_DEVMASK:
1078 case SOUND_MIXER_CAPS:
1079 case SOUND_MIXER_STEREODEVS:
1080 if (volch->direction == PCMDIR_REC)
1081 *(int *)arg = SOUND_MASK_RECLEV;
1083 *(int *)arg = SOUND_MASK_PCM;
1099 dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1102 struct pcm_channel *chn, *rdch, *wrch;
1103 struct snddev_info *d;
1105 int *arg_i, ret, tmp;
1107 d = dsp_get_info(i_dev);
1108 if (PCM_DETACHING(d) || !DSP_REGISTERED(d, i_dev))
1118 if (IOCGROUP(cmd) == 'M') {
1119 if (cmd == OSS_GETVERSION) {
1120 *arg_i = SOUND_VERSION;
1124 ret = dsp_ioctl_channel(i_dev, PCM_VOLCH(i_dev), cmd, arg);
1130 if (d->mixer_dev != NULL) {
1131 PCM_ACQUIRE_QUICK(d);
1132 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1134 PCM_RELEASE_QUICK(d);
1144 * Certain ioctls may be made on any type of device (audio, mixer,
1145 * and MIDI). Handle those special cases here.
1147 if (IOCGROUP(cmd) == 'X') {
1148 PCM_ACQUIRE_QUICK(d);
1150 case SNDCTL_SYSINFO:
1151 sound_oss_sysinfo((oss_sysinfo *)arg);
1153 case SNDCTL_CARDINFO:
1154 ret = sound_oss_card_info((oss_card_info *)arg);
1156 case SNDCTL_AUDIOINFO:
1157 case SNDCTL_AUDIOINFO_EX:
1158 case SNDCTL_ENGINEINFO:
1159 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
1161 case SNDCTL_MIXERINFO:
1162 ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
1167 PCM_RELEASE_QUICK(d);
1172 getchns(i_dev, &rdch, &wrch, 0);
1174 if (wrch != NULL && (wrch->flags & CHN_F_DEAD))
1176 if (rdch != NULL && (rdch->flags & CHN_F_DEAD))
1179 if (wrch == NULL && rdch == NULL) {
1187 * we start with the new ioctl interface.
1189 case AIONWRITE: /* how many bytes can write ? */
1193 if (wrch && wrch->bufhard.dl)
1194 while (chn_wrfeed(wrch) == 0);
1196 *arg_i = sndbuf_getfree(wrch->bufsoft);
1204 case AIOSSIZE: /* set the current blocksize */
1206 struct snd_size *p = (struct snd_size *)arg;
1210 PCM_ACQUIRE_QUICK(d);
1213 chn_setblocksize(wrch, 2, p->play_size);
1214 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1219 chn_setblocksize(rdch, 2, p->rec_size);
1220 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1223 PCM_RELEASE_QUICK(d);
1226 case AIOGSIZE: /* get the current blocksize */
1228 struct snd_size *p = (struct snd_size *)arg;
1232 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1237 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1246 snd_chan_param *p = (snd_chan_param *)arg;
1248 if (cmd == AIOSFMT &&
1249 ((p->play_format != 0 && p->play_rate == 0) ||
1250 (p->rec_format != 0 && p->rec_rate == 0))) {
1254 PCM_ACQUIRE_QUICK(d);
1257 if (cmd == AIOSFMT && p->play_format != 0) {
1259 SND_FORMAT(p->play_format,
1260 AFMT_CHANNEL(wrch->format),
1261 AFMT_EXTCHANNEL(wrch->format)));
1262 chn_setspeed(wrch, p->play_rate);
1264 p->play_rate = wrch->speed;
1265 p->play_format = AFMT_ENCODING(wrch->format);
1273 if (cmd == AIOSFMT && p->rec_format != 0) {
1275 SND_FORMAT(p->rec_format,
1276 AFMT_CHANNEL(rdch->format),
1277 AFMT_EXTCHANNEL(rdch->format)));
1278 chn_setspeed(rdch, p->rec_rate);
1280 p->rec_rate = rdch->speed;
1281 p->rec_format = AFMT_ENCODING(rdch->format);
1287 PCM_RELEASE_QUICK(d);
1291 case AIOGCAP: /* get capabilities */
1293 snd_capabilities *p = (snd_capabilities *)arg;
1294 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
1300 rcaps = chn_getcaps(rdch);
1304 pcaps = chn_getcaps(wrch);
1306 p->rate_min = max(rcaps? rcaps->minspeed : 0,
1307 pcaps? pcaps->minspeed : 0);
1308 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
1309 pcaps? pcaps->maxspeed : 1000000);
1310 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
1311 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
1312 /* XXX bad on sb16 */
1313 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
1314 (wrch? chn_getformats(wrch) : 0xffffffff);
1316 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
1317 pdev = d->mixer_dev;
1318 p->mixers = 1; /* default: one mixer */
1319 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
1320 p->left = p->right = 100;
1330 if (*arg_i == AIOSYNC_PLAY && wrch) {
1332 *arg_i = chn_abort(wrch);
1334 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
1336 *arg_i = chn_abort(rdch);
1339 printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
1345 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
1346 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
1350 * here follow the standard ioctls (filio.h etc.)
1352 case FIONREAD: /* get # bytes to read */
1355 /* if (rdch && rdch->bufhard.dl)
1356 while (chn_rdfeed(rdch) == 0);
1358 *arg_i = sndbuf_getready(rdch->bufsoft);
1366 case FIOASYNC: /*set/clear async i/o */
1367 DEB( printf("FIOASYNC\n") ; )
1370 case SNDCTL_DSP_NONBLOCK: /* set non-blocking i/o */
1371 case FIONBIO: /* set/clear non-blocking i/o */
1374 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1375 rdch->flags |= CHN_F_NBIO;
1377 rdch->flags &= ~CHN_F_NBIO;
1382 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1383 wrch->flags |= CHN_F_NBIO;
1385 wrch->flags &= ~CHN_F_NBIO;
1391 * Finally, here is the linux-compatible ioctl interface
1393 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
1394 case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
1395 case SNDCTL_DSP_GETBLKSIZE:
1396 chn = wrch ? wrch : rdch;
1399 *arg_i = sndbuf_getblksz(chn->bufsoft);
1407 case SNDCTL_DSP_SETBLKSIZE:
1408 RANGE(*arg_i, 16, 65536);
1409 PCM_ACQUIRE_QUICK(d);
1412 chn_setblocksize(wrch, 2, *arg_i);
1417 chn_setblocksize(rdch, 2, *arg_i);
1420 PCM_RELEASE_QUICK(d);
1423 case SNDCTL_DSP_RESET:
1424 DEB(printf("dsp reset\n"));
1439 case SNDCTL_DSP_SYNC:
1440 DEB(printf("dsp sync\n"));
1441 /* chn_sync may sleep */
1449 case SNDCTL_DSP_SPEED:
1450 /* chn_setspeed may sleep */
1452 PCM_ACQUIRE_QUICK(d);
1455 ret = chn_setspeed(wrch, *arg_i);
1459 if (rdch && ret == 0) {
1461 ret = chn_setspeed(rdch, *arg_i);
1466 PCM_RELEASE_QUICK(d);
1470 case SOUND_PCM_READ_RATE:
1471 chn = wrch ? wrch : rdch;
1474 *arg_i = chn->speed;
1482 case SNDCTL_DSP_STEREO:
1484 *arg_i = (*arg_i)? 2 : 1;
1485 PCM_ACQUIRE_QUICK(d);
1488 ret = chn_setformat(wrch,
1489 SND_FORMAT(wrch->format, *arg_i, 0));
1490 tmp = (AFMT_CHANNEL(wrch->format) > 1)? 1 : 0;
1493 if (rdch && ret == 0) {
1495 ret = chn_setformat(rdch,
1496 SND_FORMAT(rdch->format, *arg_i, 0));
1498 tmp = (AFMT_CHANNEL(rdch->format) > 1)? 1 : 0;
1501 PCM_RELEASE_QUICK(d);
1505 case SOUND_PCM_WRITE_CHANNELS:
1506 /* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
1513 struct pcmchan_matrix *m;
1517 if (*arg_i > SND_CHN_MAX)
1518 *arg_i = SND_CHN_MAX;
1520 m = feeder_matrix_default_channel_map(*arg_i);
1526 PCM_ACQUIRE_QUICK(d);
1529 ret = chn_setformat(wrch,
1530 SND_FORMAT(wrch->format, *arg_i, ext));
1531 tmp = AFMT_CHANNEL(wrch->format);
1534 if (rdch && ret == 0) {
1536 ret = chn_setformat(rdch,
1537 SND_FORMAT(rdch->format, *arg_i, ext));
1539 tmp = AFMT_CHANNEL(rdch->format);
1542 PCM_RELEASE_QUICK(d);
1545 chn = wrch ? wrch : rdch;
1547 *arg_i = AFMT_CHANNEL(chn->format);
1552 case SOUND_PCM_READ_CHANNELS:
1553 chn = wrch ? wrch : rdch;
1556 *arg_i = AFMT_CHANNEL(chn->format);
1564 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
1565 chn = wrch ? wrch : rdch;
1568 *arg_i = chn_getformats(chn);
1576 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
1577 if (*arg_i != AFMT_QUERY) {
1579 PCM_ACQUIRE_QUICK(d);
1582 ret = chn_setformat(wrch, SND_FORMAT(*arg_i,
1583 AFMT_CHANNEL(wrch->format),
1584 AFMT_EXTCHANNEL(wrch->format)));
1588 if (rdch && ret == 0) {
1590 ret = chn_setformat(rdch, SND_FORMAT(*arg_i,
1591 AFMT_CHANNEL(rdch->format),
1592 AFMT_EXTCHANNEL(rdch->format)));
1597 PCM_RELEASE_QUICK(d);
1598 *arg_i = AFMT_ENCODING(tmp);
1600 chn = wrch ? wrch : rdch;
1602 *arg_i = AFMT_ENCODING(chn->format);
1607 case SNDCTL_DSP_SETFRAGMENT:
1608 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
1610 uint32_t fragln = (*arg_i) & 0x0000ffff;
1611 uint32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
1613 uint32_t r_maxfrags, r_fragsz;
1615 RANGE(fragln, 4, 16);
1616 fragsz = 1 << fragln;
1619 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1622 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
1623 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1625 DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
1626 PCM_ACQUIRE_QUICK(d);
1629 ret = chn_setblocksize(rdch, maxfrags, fragsz);
1630 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
1631 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
1634 r_maxfrags = maxfrags;
1637 if (wrch && ret == 0) {
1639 ret = chn_setblocksize(wrch, maxfrags, fragsz);
1640 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
1641 fragsz = sndbuf_getblksz(wrch->bufsoft);
1643 } else { /* use whatever came from the read channel */
1644 maxfrags = r_maxfrags;
1647 PCM_RELEASE_QUICK(d);
1650 while (fragsz > 1) {
1654 *arg_i = (maxfrags << 16) | fragln;
1658 case SNDCTL_DSP_GETISPACE:
1659 /* return the size of data available in the input queue */
1661 audio_buf_info *a = (audio_buf_info *)arg;
1663 struct snd_dbuf *bs = rdch->bufsoft;
1666 a->bytes = sndbuf_getready(bs);
1667 a->fragments = a->bytes / sndbuf_getblksz(bs);
1668 a->fragstotal = sndbuf_getblkcnt(bs);
1669 a->fragsize = sndbuf_getblksz(bs);
1676 case SNDCTL_DSP_GETOSPACE:
1677 /* return space available in the output queue */
1679 audio_buf_info *a = (audio_buf_info *)arg;
1681 struct snd_dbuf *bs = wrch->bufsoft;
1684 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1685 a->bytes = sndbuf_getfree(bs);
1686 a->fragments = a->bytes / sndbuf_getblksz(bs);
1687 a->fragstotal = sndbuf_getblkcnt(bs);
1688 a->fragsize = sndbuf_getblksz(bs);
1695 case SNDCTL_DSP_GETIPTR:
1697 count_info *a = (count_info *)arg;
1699 struct snd_dbuf *bs = rdch->bufsoft;
1702 /* XXX abusive DMA update: chn_rdupdate(rdch); */
1703 a->bytes = sndbuf_gettotal(bs);
1704 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
1705 a->ptr = sndbuf_getfreeptr(bs);
1706 rdch->blocks = sndbuf_getblocks(bs);
1713 case SNDCTL_DSP_GETOPTR:
1715 count_info *a = (count_info *)arg;
1717 struct snd_dbuf *bs = wrch->bufsoft;
1720 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1721 a->bytes = sndbuf_gettotal(bs);
1722 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
1723 a->ptr = sndbuf_getreadyptr(bs);
1724 wrch->blocks = sndbuf_getblocks(bs);
1731 case SNDCTL_DSP_GETCAPS:
1733 *arg_i = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER;
1734 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1735 *arg_i |= PCM_CAP_DUPLEX;
1736 if (rdch && (rdch->flags & CHN_F_VIRTUAL) != 0)
1737 *arg_i |= PCM_CAP_VIRTUAL;
1738 if (wrch && (wrch->flags & CHN_F_VIRTUAL) != 0)
1739 *arg_i |= PCM_CAP_VIRTUAL;
1743 case SOUND_PCM_READ_BITS:
1744 chn = wrch ? wrch : rdch;
1747 if (chn->format & AFMT_8BIT)
1749 else if (chn->format & AFMT_16BIT)
1751 else if (chn->format & AFMT_24BIT)
1753 else if (chn->format & AFMT_32BIT)
1764 case SNDCTL_DSP_SETTRIGGER:
1767 rdch->flags &= ~CHN_F_NOTRIGGER;
1768 if (*arg_i & PCM_ENABLE_INPUT)
1773 rdch->flags |= CHN_F_NOTRIGGER;
1779 wrch->flags &= ~CHN_F_NOTRIGGER;
1780 if (*arg_i & PCM_ENABLE_OUTPUT)
1785 wrch->flags |= CHN_F_NOTRIGGER;
1791 case SNDCTL_DSP_GETTRIGGER:
1795 if (wrch->flags & CHN_F_TRIGGERED)
1796 *arg_i |= PCM_ENABLE_OUTPUT;
1801 if (rdch->flags & CHN_F_TRIGGERED)
1802 *arg_i |= PCM_ENABLE_INPUT;
1807 case SNDCTL_DSP_GETODELAY:
1809 struct snd_dbuf *bs = wrch->bufsoft;
1812 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1813 *arg_i = sndbuf_getready(bs);
1819 case SNDCTL_DSP_POST:
1822 wrch->flags &= ~CHN_F_NOTRIGGER;
1828 case SNDCTL_DSP_SETDUPLEX:
1830 * switch to full-duplex mode if card is in half-duplex
1831 * mode and is able to work in full-duplex mode
1834 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1835 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1840 * The following four ioctls are simple wrappers around mixer_ioctl
1841 * with no further processing. xcmd is short for "translated
1844 case SNDCTL_DSP_GETRECVOL:
1846 xcmd = SOUND_MIXER_READ_RECLEV;
1850 case SNDCTL_DSP_SETRECVOL:
1852 xcmd = SOUND_MIXER_WRITE_RECLEV;
1856 case SNDCTL_DSP_GETPLAYVOL:
1858 xcmd = SOUND_MIXER_READ_PCM;
1862 case SNDCTL_DSP_SETPLAYVOL:
1864 xcmd = SOUND_MIXER_WRITE_PCM;
1868 ret = dsp_ioctl_channel(i_dev, chn, xcmd, arg);
1874 if (d->mixer_dev != NULL) {
1875 PCM_ACQUIRE_QUICK(d);
1876 ret = mixer_ioctl_cmd(d->mixer_dev, xcmd, arg, -1, td,
1878 PCM_RELEASE_QUICK(d);
1884 case SNDCTL_DSP_GET_RECSRC_NAMES:
1885 case SNDCTL_DSP_GET_RECSRC:
1886 case SNDCTL_DSP_SET_RECSRC:
1887 if (d->mixer_dev != NULL) {
1888 PCM_ACQUIRE_QUICK(d);
1889 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1891 PCM_RELEASE_QUICK(d);
1897 * The following 3 ioctls aren't very useful at the moment. For
1898 * now, only a single channel is associated with a cdev (/dev/dspN
1899 * instance), so there's only a single output routing to use (i.e.,
1900 * the wrch bound to this cdev).
1902 case SNDCTL_DSP_GET_PLAYTGT_NAMES:
1904 oss_mixer_enuminfo *ei;
1905 ei = (oss_mixer_enuminfo *)arg;
1908 ei->version = 0; /* static for now */
1909 ei->strindex[0] = 0;
1913 strlcpy(ei->strings, wrch->name,
1914 sizeof(ei->strings));
1917 ei->strings[0] = '\0';
1921 case SNDCTL_DSP_GET_PLAYTGT:
1922 case SNDCTL_DSP_SET_PLAYTGT: /* yes, they are the same for now */
1925 * OSSv4: "The value that was accepted by the device will
1926 * be returned back in the variable pointed by the
1935 case SNDCTL_DSP_SILENCE:
1937 * Flush the software (pre-feed) buffer, but try to minimize playback
1938 * interruption. (I.e., record unplayed samples with intent to
1939 * restore by SNDCTL_DSP_SKIP.) Intended for application "pause"
1945 struct snd_dbuf *bs;
1947 while (wrch->inprog != 0)
1948 cv_wait(&wrch->cv, wrch->lock);
1950 if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) {
1951 bs->sl = sndbuf_getready(bs);
1952 sndbuf_dispose(bs, bs->shadbuf, sndbuf_getready(bs));
1953 sndbuf_fillsilence(bs);
1960 case SNDCTL_DSP_SKIP:
1962 * OSSv4 docs: "This ioctl call discards all unplayed samples in the
1963 * playback buffer by moving the current write position immediately
1964 * before the point where the device is currently reading the samples."
1969 struct snd_dbuf *bs;
1971 while (wrch->inprog != 0)
1972 cv_wait(&wrch->cv, wrch->lock);
1974 if ((bs->shadbuf != NULL) && (bs->sl > 0)) {
1975 sndbuf_softreset(bs);
1976 sndbuf_acquire(bs, bs->shadbuf, bs->sl);
1984 case SNDCTL_DSP_CURRENT_OPTR:
1985 case SNDCTL_DSP_CURRENT_IPTR:
1987 * @note Changing formats resets the buffer counters, which differs
1988 * from the 4Front drivers. However, I don't expect this to be
1989 * much of a problem.
1991 * @note In a test where @c CURRENT_OPTR is called immediately after write
1992 * returns, this driver is about 32K samples behind whereas
1993 * 4Front's is about 8K samples behind. Should determine source
1994 * of discrepancy, even if only out of curiosity.
1996 * @todo Actually test SNDCTL_DSP_CURRENT_IPTR.
1998 chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch;
2002 struct snd_dbuf *bs;
2005 oss_count_t *oc = (oss_count_t *)arg;
2010 tmp = (sndbuf_getsize(b) + chn_getptr(chn) - sndbuf_gethwptr(b)) % sndbuf_getsize(b);
2011 oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getalign(b);
2012 oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getalign(b);
2014 oc->samples = sndbuf_gettotal(bs) / sndbuf_getalign(bs);
2015 oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getalign(bs);
2021 case SNDCTL_DSP_HALT_OUTPUT:
2022 case SNDCTL_DSP_HALT_INPUT:
2023 chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch;
2033 case SNDCTL_DSP_LOW_WATER:
2035 * Set the number of bytes required to attract attention by
2040 wrch->lw = (*arg_i > 1) ? *arg_i : 1;
2045 rdch->lw = (*arg_i > 1) ? *arg_i : 1;
2050 case SNDCTL_DSP_GETERROR:
2052 * OSSv4 docs: "All errors and counters will automatically be
2053 * cleared to zeroes after the call so each call will return only
2054 * the errors that occurred after the previous invocation. ... The
2055 * play_underruns and rec_overrun fields are the only useful fields
2056 * returned by OSS 4.0."
2059 audio_errinfo *ei = (audio_errinfo *)arg;
2061 bzero((void *)ei, sizeof(*ei));
2065 ei->play_underruns = wrch->xruns;
2071 ei->rec_overruns = rdch->xruns;
2078 case SNDCTL_DSP_SYNCGROUP:
2079 PCM_ACQUIRE_QUICK(d);
2080 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
2081 PCM_RELEASE_QUICK(d);
2084 case SNDCTL_DSP_SYNCSTART:
2085 PCM_ACQUIRE_QUICK(d);
2086 ret = dsp_oss_syncstart(*arg_i);
2087 PCM_RELEASE_QUICK(d);
2090 case SNDCTL_DSP_POLICY:
2091 PCM_ACQUIRE_QUICK(d);
2092 ret = dsp_oss_policy(wrch, rdch, *arg_i);
2093 PCM_RELEASE_QUICK(d);
2096 case SNDCTL_DSP_COOKEDMODE:
2097 PCM_ACQUIRE_QUICK(d);
2098 if (!(dsp_get_flags(i_dev) & SD_F_BITPERFECT))
2099 ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
2100 PCM_RELEASE_QUICK(d);
2102 case SNDCTL_DSP_GET_CHNORDER:
2103 PCM_ACQUIRE_QUICK(d);
2104 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
2105 PCM_RELEASE_QUICK(d);
2107 case SNDCTL_DSP_SET_CHNORDER:
2108 PCM_ACQUIRE_QUICK(d);
2109 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
2110 PCM_RELEASE_QUICK(d);
2112 case SNDCTL_DSP_GETCHANNELMASK: /* XXX vlc */
2113 PCM_ACQUIRE_QUICK(d);
2114 ret = dsp_oss_getchannelmask(wrch, rdch, (int *)arg);
2115 PCM_RELEASE_QUICK(d);
2117 case SNDCTL_DSP_BIND_CHANNEL: /* XXX what?!? */
2120 #ifdef OSSV4_EXPERIMENT
2122 * XXX The following ioctls are not yet supported and just return
2125 case SNDCTL_DSP_GETOPEAKS:
2126 case SNDCTL_DSP_GETIPEAKS:
2127 chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch;
2131 oss_peaks_t *op = (oss_peaks_t *)arg;
2135 ret = chn_getpeaks(chn, &lpeak, &rpeak);
2147 * XXX Once implemented, revisit this for proper cv protection
2150 case SNDCTL_GETLABEL:
2151 ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg);
2153 case SNDCTL_SETLABEL:
2154 ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg);
2156 case SNDCTL_GETSONG:
2157 ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg);
2159 case SNDCTL_SETSONG:
2160 ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg);
2162 case SNDCTL_SETNAME:
2163 ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg);
2167 * @note The S/PDIF interface ioctls, @c SNDCTL_DSP_READCTL and
2168 * @c SNDCTL_DSP_WRITECTL have been omitted at the suggestion of
2169 * 4Front Technologies.
2171 case SNDCTL_DSP_READCTL:
2172 case SNDCTL_DSP_WRITECTL:
2175 #endif /* !0 (explicitly omitted ioctls) */
2177 #endif /* !OSSV4_EXPERIMENT */
2178 case SNDCTL_DSP_MAPINBUF:
2179 case SNDCTL_DSP_MAPOUTBUF:
2180 case SNDCTL_DSP_SETSYNCRO:
2183 case SNDCTL_DSP_SUBDIVIDE:
2184 case SOUND_PCM_WRITE_FILTER:
2185 case SOUND_PCM_READ_FILTER:
2186 /* dunno what these do, don't sound important */
2189 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
2200 dsp_poll(struct cdev *i_dev, int events, struct thread *td)
2202 struct snddev_info *d;
2203 struct pcm_channel *wrch, *rdch;
2206 d = dsp_get_info(i_dev);
2207 if (PCM_DETACHING(d) || !DSP_REGISTERED(d, i_dev)) {
2208 /* XXX many clients don't understand POLLNVAL */
2209 return (events & (POLLHUP | POLLPRI | POLLIN |
2210 POLLRDNORM | POLLOUT | POLLWRNORM));
2218 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2220 if (wrch != NULL && !(wrch->flags & CHN_F_DEAD)) {
2221 e = (events & (POLLOUT | POLLWRNORM));
2223 ret |= chn_poll(wrch, e, td);
2226 if (rdch != NULL && !(rdch->flags & CHN_F_DEAD)) {
2227 e = (events & (POLLIN | POLLRDNORM));
2229 ret |= chn_poll(rdch, e, td);
2232 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2240 dsp_mmap(struct cdev *i_dev, vm_ooffset_t offset, vm_paddr_t *paddr,
2241 int nprot, vm_memattr_t *memattr)
2245 * offset is in range due to checks in dsp_mmap_single().
2246 * XXX memattr is not honored.
2248 *paddr = vtophys(offset);
2253 dsp_mmap_single(struct cdev *i_dev, vm_ooffset_t *offset,
2254 vm_size_t size, struct vm_object **object, int nprot)
2256 struct snddev_info *d;
2257 struct pcm_channel *wrch, *rdch, *c;
2260 * Reject PROT_EXEC by default. It just doesn't makes sense.
2261 * Unfortunately, we have to give up this one due to linux_mmap
2264 * https://lists.freebsd.org/pipermail/freebsd-emulation/2007-June/003698.html
2268 if ((nprot & PROT_EXEC) && (dsp_mmap_allow_prot_exec < 0 ||
2269 (dsp_mmap_allow_prot_exec == 0 &&
2270 SV_CURPROC_ABI() != SV_ABI_LINUX)))
2272 if ((nprot & PROT_EXEC) && dsp_mmap_allow_prot_exec < 1)
2277 * PROT_READ (alone) selects the input buffer.
2278 * PROT_WRITE (alone) selects the output buffer.
2279 * PROT_WRITE|PROT_READ together select the output buffer.
2281 if ((nprot & (PROT_READ | PROT_WRITE)) == 0)
2284 d = dsp_get_info(i_dev);
2285 if (PCM_DETACHING(d) || !DSP_REGISTERED(d, i_dev))
2290 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2292 c = ((nprot & PROT_WRITE) != 0) ? wrch : rdch;
2293 if (c == NULL || (c->flags & CHN_F_MMAP_INVALID) ||
2294 (*offset + size) > sndbuf_getsize(c->bufsoft) ||
2295 (wrch != NULL && (wrch->flags & CHN_F_MMAP_INVALID)) ||
2296 (rdch != NULL && (rdch->flags & CHN_F_MMAP_INVALID))) {
2297 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2303 wrch->flags |= CHN_F_MMAP;
2305 rdch->flags |= CHN_F_MMAP;
2307 *offset = (uintptr_t)sndbuf_getbufofs(c->bufsoft, *offset);
2308 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2309 *object = vm_pager_allocate(OBJT_DEVICE, i_dev,
2310 size, nprot, *offset, curthread->td_ucred);
2314 if (*object == NULL)
2319 /* So much for dev_stdclone() */
2321 dsp_stdclone(char *name, char *namep, char *sep, int use_sep, int *u, int *c)
2325 len = strlen(namep);
2326 if (strncmp(name, namep, len) != 0)
2331 if (isdigit(*name) == 0)
2336 if (*name == '0' && !(name[1] == '\0' || bcmp(name + 1, sep, len) == 0))
2339 for (*u = 0; isdigit(*name) != 0; name++) {
2347 return ((use_sep == 0) ? 0 : ENODEV);
2349 if (bcmp(name, sep, len) != 0 || isdigit(name[len]) == 0)
2354 if (*name == '0' && name[1] != '\0')
2357 for (*c = 0; isdigit(*name) != 0; name++) {
2371 dsp_clone(void *arg,
2373 char *name, int namelen, struct cdev **dev)
2375 struct snddev_info *d;
2376 struct snd_clone_entry *ce;
2377 struct pcm_channel *c;
2378 int i, unit, udcmask, cunit, devtype, devhw, devcmax, tumax;
2379 char *devname, *devcmp, *devsep;
2381 KASSERT(dsp_umax >= 0 && dsp_cmax >= 0, ("Uninitialized unit!"));
2395 for (i = 0; unit == -1 &&
2396 i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2397 devtype = dsp_cdevs[i].type;
2398 devcmp = dsp_cdevs[i].name;
2399 devsep = dsp_cdevs[i].sep;
2400 devname = dsp_cdevs[i].alias;
2401 if (devname == NULL)
2403 devhw = dsp_cdevs[i].hw;
2404 devcmax = dsp_cdevs[i].max - 1;
2405 if (strcmp(name, devcmp) == 0) {
2406 if (dsp_basename_clone != 0)
2408 } else if (dsp_stdclone(name, devcmp, devsep,
2409 dsp_cdevs[i].use_sep, &unit, &cunit) != 0) {
2415 d = devclass_get_softc(pcm_devclass, unit);
2416 if (!PCM_REGISTERED(d) || d->clones == NULL)
2419 /* XXX Need Giant magic entry ??? */
2422 if (snd_clone_disabled(d->clones)) {
2431 udcmask = snd_u2unit(unit) | snd_d2unit(devtype);
2434 KASSERT(devcmax <= dsp_cmax,
2435 ("overflow: devcmax=%d, dsp_cmax=%d", devcmax, dsp_cmax));
2436 if (cunit > devcmax) {
2437 PCM_RELEASE_QUICK(d);
2440 udcmask |= snd_c2unit(cunit);
2441 CHN_FOREACH(c, d, channels.pcm) {
2443 if (c->unit != udcmask) {
2448 udcmask &= ~snd_c2unit(cunit);
2450 * Temporarily increase clone maxunit to overcome
2451 * vchan flexibility.
2453 * # sysctl dev.pcm.0.play.vchans=256
2454 * dev.pcm.0.play.vchans: 1 -> 256
2455 * # cat /dev/zero > /dev/dsp0.vp255 &
2457 * # sysctl dev.pcm.0.play.vchans=0
2458 * dev.pcm.0.play.vchans: 256 -> 1
2460 * [1] + running cat /dev/zero > /dev/dsp0.vp255
2462 * # cat /dev/zero > /dev/dsp0.vp255
2463 * zsh: operation not supported: /dev/dsp0.vp255
2465 tumax = snd_clone_getmaxunit(d->clones);
2467 snd_clone_setmaxunit(d->clones, cunit);
2470 goto dsp_clone_alloc;
2473 * Ok, so we're requesting unallocated vchan, but still
2474 * within maximum vchan limit.
2476 if (((devtype == SND_DEV_DSPHW_VPLAY && d->pvchancount > 0) ||
2477 (devtype == SND_DEV_DSPHW_VREC && d->rvchancount > 0)) &&
2478 cunit < snd_maxautovchans) {
2479 udcmask &= ~snd_c2unit(cunit);
2480 tumax = snd_clone_getmaxunit(d->clones);
2482 snd_clone_setmaxunit(d->clones, cunit);
2485 goto dsp_clone_alloc;
2487 PCM_RELEASE_QUICK(d);
2492 ce = snd_clone_alloc(d->clones, dev, &cunit, udcmask);
2494 snd_clone_setmaxunit(d->clones, tumax);
2496 udcmask |= snd_c2unit(cunit);
2497 *dev = make_dev(&dsp_cdevsw, PCMMINOR(udcmask),
2498 UID_ROOT, GID_WHEEL, 0666, "%s%d%s%d",
2499 devname, unit, devsep, cunit);
2500 snd_clone_register(ce, *dev);
2503 PCM_RELEASE_QUICK(d);
2510 dsp_sysinit(void *p)
2512 if (dsp_ehtag != NULL)
2514 /* initialize unit numbering */
2516 dsp_umax = PCMMAXUNIT;
2517 dsp_cmax = PCMMAXCHAN;
2518 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
2522 dsp_sysuninit(void *p)
2524 if (dsp_ehtag == NULL)
2526 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
2530 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
2531 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
2534 dsp_unit2name(char *buf, size_t len, int unit)
2538 KASSERT(buf != NULL && len != 0,
2539 ("bogus buf=%p len=%ju", buf, (uintmax_t)len));
2541 dtype = snd_unit2d(unit);
2543 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2544 if (dtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
2546 snprintf(buf, len, "%s%d%s%d", dsp_cdevs[i].name,
2547 snd_unit2u(unit), dsp_cdevs[i].sep, snd_unit2c(unit));
2555 * @brief Handler for SNDCTL_AUDIOINFO.
2557 * Gathers information about the audio device specified in ai->dev. If
2558 * ai->dev == -1, then this function gathers information about the current
2559 * device. If the call comes in on a non-audio device and ai->dev == -1,
2562 * This routine is supposed to go practically straight to the hardware,
2563 * getting capabilities directly from the sound card driver, side-stepping
2564 * the intermediate channel interface.
2566 * Note, however, that the usefulness of this command is significantly
2567 * decreased when requesting info about any device other than the one serving
2568 * the request. While each snddev_channel refers to a specific device node,
2569 * the converse is *not* true. Currently, when a sound device node is opened,
2570 * the sound subsystem scans for an available audio channel (or channels, if
2571 * opened in read+write) and then assigns them to the si_drv[12] private
2572 * data fields. As a result, any information returned linking a channel to
2573 * a specific character device isn't necessarily accurate.
2576 * Calling threads must not hold any snddev_info or pcm_channel locks.
2578 * @param dev device on which the ioctl was issued
2579 * @param ai ioctl request data container
2582 * @retval EINVAL ai->dev specifies an invalid device
2584 * @todo Verify correctness of Doxygen tags. ;)
2587 dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
2589 struct pcmchan_caps *caps;
2590 struct pcm_channel *ch;
2591 struct snddev_info *d;
2593 int i, nchan, *rates, minch, maxch;
2594 char *devname, buf[CHN_NAMELEN];
2597 * If probing the device that received the ioctl, make sure it's a
2598 * DSP device. (Users may use this ioctl with /dev/mixer and
2601 if (ai->dev == -1 && i_dev->si_devsw != &dsp_cdevsw)
2607 bzero(buf, sizeof(buf));
2610 * Search for the requested audio device (channel). Start by
2611 * iterating over pcm devices.
2613 for (i = 0; pcm_devclass != NULL &&
2614 i < devclass_get_maxunit(pcm_devclass); i++) {
2615 d = devclass_get_softc(pcm_devclass, i);
2616 if (!PCM_REGISTERED(d))
2619 /* XXX Need Giant magic entry ??? */
2621 /* See the note in function docblock */
2622 PCM_UNLOCKASSERT(d);
2625 CHN_FOREACH(ch, d, channels.pcm) {
2626 CHN_UNLOCKASSERT(ch);
2628 if (ai->dev == -1) {
2629 if (DSP_REGISTERED(d, i_dev) &&
2630 (ch == PCM_RDCH(i_dev) || /* record ch */
2631 ch == PCM_WRCH(i_dev))) { /* playback ch */
2632 devname = dsp_unit2name(buf,
2633 sizeof(buf), ch->unit);
2635 } else if (ai->dev == nchan) {
2636 devname = dsp_unit2name(buf, sizeof(buf),
2639 if (devname != NULL)
2645 if (devname != NULL) {
2647 * At this point, the following synchronization stuff
2649 * - a specific PCM device is locked.
2650 * - a specific audio channel has been locked, so be
2651 * sure to unlock when exiting;
2654 caps = chn_getcaps(ch);
2657 * With all handles collected, zero out the user's
2658 * container and begin filling in its fields.
2660 bzero((void *)ai, sizeof(oss_audioinfo));
2663 strlcpy(ai->name, ch->name, sizeof(ai->name));
2665 if ((ch->flags & CHN_F_BUSY) == 0)
2668 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
2672 * @c cmd - OSSv4 docs: "Only supported under Linux at
2673 * this moment." Cop-out, I know, but I'll save
2674 * running around in the process table for later.
2675 * Is there a risk of leaking information?
2680 * These flags stolen from SNDCTL_DSP_GETCAPS handler.
2681 * Note, however, that a single channel operates in
2682 * only one direction, so PCM_CAP_DUPLEX is out.
2685 * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep
2686 * these in pcmchan::caps?
2688 ai->caps = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER |
2689 ((ch->flags & CHN_F_VIRTUAL) ? PCM_CAP_VIRTUAL : 0) |
2690 ((ch->direction == PCMDIR_PLAY) ? PCM_CAP_OUTPUT : PCM_CAP_INPUT);
2693 * Collect formats supported @b natively by the
2694 * device. Also determine min/max channels. (I.e.,
2695 * mono, stereo, or both?)
2697 * If any channel is stereo, maxch = 2;
2698 * if all channels are stereo, minch = 2, too;
2699 * if any channel is mono, minch = 1;
2700 * and if all channels are mono, maxch = 1.
2705 for (i = 0; caps->fmtlist[i]; i++) {
2706 fmts |= caps->fmtlist[i];
2707 if (AFMT_CHANNEL(caps->fmtlist[i]) > 1) {
2708 minch = (minch == 0) ? 2 : minch;
2712 maxch = (maxch == 0) ? 1 : maxch;
2716 if (ch->direction == PCMDIR_PLAY)
2717 ai->oformats = fmts;
2719 ai->iformats = fmts;
2723 * @c magic - OSSv4 docs: "Reserved for internal use
2727 * @c card_number - OSSv4 docs: "Number of the sound
2728 * card where this device belongs or -1 if this
2729 * information is not available. Applications
2730 * should normally not use this field for any
2733 ai->card_number = -1;
2735 * @todo @c song_name - depends first on
2736 * SNDCTL_[GS]ETSONG @todo @c label - depends
2737 * on SNDCTL_[GS]ETLABEL
2738 * @todo @c port_number - routing information?
2740 ai->port_number = -1;
2741 ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1;
2744 * @c real_device - OSSv4 docs: "Obsolete."
2746 ai->real_device = -1;
2747 strlcpy(ai->devnode, "/dev/", sizeof(ai->devnode));
2748 strlcat(ai->devnode, devname, sizeof(ai->devnode));
2749 ai->enabled = device_is_attached(d->dev) ? 1 : 0;
2752 * @c flags - OSSv4 docs: "Reserved for future use."
2755 * @c binding - OSSv4 docs: "Reserved for future use."
2757 * @todo @c handle - haven't decided how to generate
2758 * this yet; bus, vendor, device IDs?
2760 ai->min_rate = caps->minspeed;
2761 ai->max_rate = caps->maxspeed;
2763 ai->min_channels = minch;
2764 ai->max_channels = maxch;
2766 ai->nrates = chn_getrates(ch, &rates);
2767 if (ai->nrates > OSS_MAX_SAMPLE_RATES)
2768 ai->nrates = OSS_MAX_SAMPLE_RATES;
2770 for (i = 0; i < ai->nrates; i++)
2771 ai->rates[i] = rates[i];
2773 ai->next_play_engine = 0;
2774 ai->next_rec_engine = 0;
2781 if (devname != NULL)
2785 /* Exhausted the search -- nothing is locked, so return. */
2790 * @brief Assigns a PCM channel to a sync group.
2792 * Sync groups are used to enable audio operations on multiple devices
2793 * simultaneously. They may be used with any number of devices and may
2794 * span across applications. Devices are added to groups with
2795 * the SNDCTL_DSP_SYNCGROUP ioctl, and operations are triggered with the
2796 * SNDCTL_DSP_SYNCSTART ioctl.
2798 * If the @c id field of the @c group parameter is set to zero, then a new
2799 * sync group is created. Otherwise, wrch and rdch (if set) are added to
2800 * the group specified.
2802 * @todo As far as memory allocation, should we assume that things are
2803 * okay and allocate with M_WAITOK before acquiring channel locks,
2804 * freeing later if not?
2806 * @param wrch output channel associated w/ device (if any)
2807 * @param rdch input channel associated w/ device (if any)
2808 * @param group Sync group parameters
2811 * @retval non-zero error to be propagated upstream
2814 dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
2816 struct pcmchan_syncmember *smrd, *smwr;
2817 struct pcmchan_syncgroup *sg;
2826 * Free_unr() may sleep, so store released syncgroup IDs until after
2827 * all locks are released.
2829 sg_ids[0] = sg_ids[1] = sg_ids[2] = 0;
2834 * - Insert channel(s) into group's member list.
2835 * - Set CHN_F_NOTRIGGER on channel(s).
2836 * - Stop channel(s).
2840 * If device's channels are already mapped to a group, unmap them.
2844 sg_ids[0] = chn_syncdestroy(wrch);
2849 sg_ids[1] = chn_syncdestroy(rdch);
2853 * Verify that mode matches character device properites.
2854 * - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
2855 * - Bail if PCM_ENABLE_INPUT && rdch == NULL.
2857 if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) ||
2858 ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) {
2864 * An id of zero indicates the user wants to create a new
2867 if (group->id == 0) {
2868 sg = (struct pcmchan_syncgroup *)malloc(sizeof(*sg), M_DEVBUF, M_NOWAIT);
2870 SLIST_INIT(&sg->members);
2871 sg->id = alloc_unr(pcmsg_unrhdr);
2874 SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link);
2878 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2879 if (sg->id == group->id)
2886 /* Couldn't create or find a syncgroup. Fail. */
2891 * Allocate a syncmember, assign it and a channel together, and
2892 * insert into syncgroup.
2894 if (group->mode & PCM_ENABLE_INPUT) {
2895 smrd = (struct pcmchan_syncmember *)malloc(sizeof(*smrd), M_DEVBUF, M_NOWAIT);
2901 SLIST_INSERT_HEAD(&sg->members, smrd, link);
2906 rdch->flags |= CHN_F_NOTRIGGER;
2910 if (group->mode & PCM_ENABLE_OUTPUT) {
2911 smwr = (struct pcmchan_syncmember *)malloc(sizeof(*smwr), M_DEVBUF, M_NOWAIT);
2917 SLIST_INSERT_HEAD(&sg->members, smwr, link);
2922 wrch->flags |= CHN_F_NOTRIGGER;
2929 free(smrd, M_DEVBUF);
2930 if ((sg != NULL) && SLIST_EMPTY(&sg->members)) {
2932 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2950 free_unr(pcmsg_unrhdr, sg_ids[0]);
2952 free_unr(pcmsg_unrhdr, sg_ids[1]);
2954 free_unr(pcmsg_unrhdr, sg_ids[2]);
2960 * @brief Launch a sync group into action
2962 * Sync groups are established via SNDCTL_DSP_SYNCGROUP. This function
2963 * iterates over all members, triggering them along the way.
2965 * @note Caller must not hold any channel locks.
2967 * @param sg_id sync group identifier
2970 * @retval non-zero error worthy of propagating upstream to user
2973 dsp_oss_syncstart(int sg_id)
2975 struct pcmchan_syncmember *sm, *sm_tmp;
2976 struct pcmchan_syncgroup *sg;
2977 struct pcm_channel *c;
2980 /* Get the synclists lock */
2987 /* Search for syncgroup by ID */
2988 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2989 if (sg->id == sg_id)
2993 /* Return EINVAL if not found */
2999 /* Any removals resulting in an empty group should've handled this */
3000 KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup"));
3003 * Attempt to lock all member channels - if any are already
3004 * locked, unlock those acquired, sleep for a bit, and try
3007 SLIST_FOREACH(sm, &sg->members, link) {
3008 if (CHN_TRYLOCK(sm->ch) == 0) {
3009 int timo = hz * 5/1000;
3013 /* Release all locked channels so far, retry */
3014 SLIST_FOREACH(sm_tmp, &sg->members, link) {
3015 /* sm is the member already locked */
3018 CHN_UNLOCK(sm_tmp->ch);
3021 /** @todo Is PRIBIO correct/ */
3022 ret = msleep(sm, &snd_pcm_syncgroups_mtx,
3023 PRIBIO | PCATCH, "pcmsg", timo);
3024 if (ret == EINTR || ret == ERESTART)
3028 ret = 0; /* Assumes ret == EAGAIN... */
3031 } while (needlocks && ret == 0);
3033 /* Proceed only if no errors encountered. */
3035 /* Launch channels */
3036 while ((sm = SLIST_FIRST(&sg->members)) != NULL) {
3037 SLIST_REMOVE_HEAD(&sg->members, link);
3042 c->flags &= ~CHN_F_NOTRIGGER;
3048 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
3055 * Free_unr() may sleep, so be sure to give up the syncgroup lock
3059 free_unr(pcmsg_unrhdr, sg_id);
3065 * @brief Handler for SNDCTL_DSP_POLICY
3067 * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment
3068 * size and count like with SNDCTL_DSP_SETFRAGMENT. Instead of the user
3069 * specifying those two parameters, s/he simply selects a number from 0..10
3070 * which corresponds to a buffer size. Smaller numbers request smaller
3071 * buffers with lower latencies (at greater overhead from more frequent
3072 * interrupts), while greater numbers behave in the opposite manner.
3074 * The 4Front spec states that a value of 5 should be the default. However,
3075 * this implementation deviates slightly by using a linear scale without
3076 * consulting drivers. I.e., even though drivers may have different default
3077 * buffer sizes, a policy argument of 5 will have the same result across
3080 * See http://manuals.opensound.com/developer/SNDCTL_DSP_POLICY.html for
3083 * @todo When SNDCTL_DSP_COOKEDMODE is supported, it'll be necessary to
3084 * work with hardware drivers directly.
3086 * @note PCM channel arguments must not be locked by caller.
3088 * @param wrch Pointer to opened playback channel (optional; may be NULL)
3089 * @param rdch " recording channel (optional; may be NULL)
3090 * @param policy Integer from [0:10]
3092 * @retval 0 constant (for now)
3095 dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
3099 if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX)
3102 /* Default: success */
3107 ret = chn_setlatency(rdch, policy);
3111 if (wrch && ret == 0) {
3113 ret = chn_setlatency(wrch, policy);
3124 * @brief Enable or disable "cooked" mode
3126 * This is a handler for @c SNDCTL_DSP_COOKEDMODE. When in cooked mode, which
3127 * is the default, the sound system handles rate and format conversions
3128 * automatically (ex: user writing 11025Hz/8 bit/unsigned but card only
3129 * operates with 44100Hz/16bit/signed samples).
3131 * Disabling cooked mode is intended for applications wanting to mmap()
3132 * a sound card's buffer space directly, bypassing the FreeBSD 2-stage
3133 * feeder architecture, presumably to gain as much control over audio
3134 * hardware as possible.
3136 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_COOKEDMODE.html
3139 * @param wrch playback channel (optional; may be NULL)
3140 * @param rdch recording channel (optional; may be NULL)
3141 * @param enabled 0 = raw mode, 1 = cooked mode
3143 * @retval EINVAL Operation not yet supported.
3146 dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
3150 * XXX I just don't get it. Why don't they call it
3151 * "BITPERFECT" ~ SNDCTL_DSP_BITPERFECT !?!?.
3152 * This is just plain so confusing, incoherent,
3153 * <insert any non-printable characters here>.
3155 if (!(enabled == 1 || enabled == 0))
3159 * I won't give in. I'm inverting its logic here and now.
3160 * Brag all you want, but "BITPERFECT" should be the better
3163 enabled ^= 0x00000001;
3167 wrch->flags &= ~CHN_F_BITPERFECT;
3168 wrch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3174 rdch->flags &= ~CHN_F_BITPERFECT;
3175 rdch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3183 * @brief Retrieve channel interleaving order
3185 * This is the handler for @c SNDCTL_DSP_GET_CHNORDER.
3187 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html
3190 * @note As the ioctl definition is still under construction, FreeBSD
3191 * does not currently support SNDCTL_DSP_GET_CHNORDER.
3193 * @param wrch playback channel (optional; may be NULL)
3194 * @param rdch recording channel (optional; may be NULL)
3195 * @param map channel map (result will be stored there)
3197 * @retval EINVAL Operation not yet supported.
3200 dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3202 struct pcm_channel *ch;
3205 ch = (wrch != NULL) ? wrch : rdch;
3208 ret = chn_oss_getorder(ch, map);
3217 * @brief Specify channel interleaving order
3219 * This is the handler for @c SNDCTL_DSP_SET_CHNORDER.
3221 * @note As the ioctl definition is still under construction, FreeBSD
3222 * does not currently support @c SNDCTL_DSP_SET_CHNORDER.
3224 * @param wrch playback channel (optional; may be NULL)
3225 * @param rdch recording channel (optional; may be NULL)
3226 * @param map channel map
3228 * @retval EINVAL Operation not yet supported.
3231 dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3239 ret = chn_oss_setorder(wrch, map);
3243 if (ret == 0 && rdch != NULL) {
3245 ret = chn_oss_setorder(rdch, map);
3253 dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch,
3256 struct pcm_channel *ch;
3261 ch = (wrch != NULL) ? wrch : rdch;
3265 ret = chn_oss_getmask(ch, &chnmask);
3276 #ifdef OSSV4_EXPERIMENT
3278 * @brief Retrieve an audio device's label
3280 * This is a handler for the @c SNDCTL_GETLABEL ioctl.
3282 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3285 * From Hannu@4Front: "For example ossxmix (just like some HW mixer
3286 * consoles) can show variable "labels" for certain controls. By default
3287 * the application name (say quake) is shown as the label but
3288 * applications may change the labels themselves."
3290 * @note As the ioctl definition is still under construction, FreeBSD
3291 * does not currently support @c SNDCTL_GETLABEL.
3293 * @param wrch playback channel (optional; may be NULL)
3294 * @param rdch recording channel (optional; may be NULL)
3295 * @param label label gets copied here
3297 * @retval EINVAL Operation not yet supported.
3300 dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3306 * @brief Specify an audio device's label
3308 * This is a handler for the @c SNDCTL_SETLABEL ioctl. Please see the
3309 * comments for @c dsp_oss_getlabel immediately above.
3311 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3314 * @note As the ioctl definition is still under construction, FreeBSD
3315 * does not currently support SNDCTL_SETLABEL.
3317 * @param wrch playback channel (optional; may be NULL)
3318 * @param rdch recording channel (optional; may be NULL)
3319 * @param label label gets copied from here
3321 * @retval EINVAL Operation not yet supported.
3324 dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3330 * @brief Retrieve name of currently played song
3332 * This is a handler for the @c SNDCTL_GETSONG ioctl. Audio players could
3333 * tell the system the name of the currently playing song, which would be
3334 * visible in @c /dev/sndstat.
3336 * See @c http://manuals.opensound.com/developer/SNDCTL_GETSONG.html
3339 * @note As the ioctl definition is still under construction, FreeBSD
3340 * does not currently support SNDCTL_GETSONG.
3342 * @param wrch playback channel (optional; may be NULL)
3343 * @param rdch recording channel (optional; may be NULL)
3344 * @param song song name gets copied here
3346 * @retval EINVAL Operation not yet supported.
3349 dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3355 * @brief Retrieve name of currently played song
3357 * This is a handler for the @c SNDCTL_SETSONG ioctl. Audio players could
3358 * tell the system the name of the currently playing song, which would be
3359 * visible in @c /dev/sndstat.
3361 * See @c http://manuals.opensound.com/developer/SNDCTL_SETSONG.html
3364 * @note As the ioctl definition is still under construction, FreeBSD
3365 * does not currently support SNDCTL_SETSONG.
3367 * @param wrch playback channel (optional; may be NULL)
3368 * @param rdch recording channel (optional; may be NULL)
3369 * @param song song name gets copied from here
3371 * @retval EINVAL Operation not yet supported.
3374 dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3380 * @brief Rename a device
3382 * This is a handler for the @c SNDCTL_SETNAME ioctl.
3384 * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for
3387 * From Hannu@4Front: "This call is used to change the device name
3388 * reported in /dev/sndstat and ossinfo. So instead of using some generic
3389 * 'OSS loopback audio (MIDI) driver' the device may be given a meaningfull
3390 * name depending on the current context (for example 'OSS virtual wave table
3391 * synth' or 'VoIP link to London')."
3393 * @note As the ioctl definition is still under construction, FreeBSD
3394 * does not currently support SNDCTL_SETNAME.
3396 * @param wrch playback channel (optional; may be NULL)
3397 * @param rdch recording channel (optional; may be NULL)
3398 * @param name new device name gets copied from here
3400 * @retval EINVAL Operation not yet supported.
3403 dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
3407 #endif /* !OSSV4_EXPERIMENT */