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/devfs.h>
36 #include <sys/device.h>
37 #include <sys/eventhandler.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: head/sys/dev/sound/pcm/dsp.c 274035 2014-11-03 11:11:45Z bapt $");
48 static int dsp_mmap_allow_prot_exec = 0;
49 SYSCTL_INT(_hw_snd, OID_AUTO, compat_linux_mmap, CTLFLAG_RW,
50 &dsp_mmap_allow_prot_exec, 0,
51 "linux mmap compatibility (-1=force disable 0=auto 1=force enable)");
54 struct pcm_channel *rdch, *wrch;
55 struct pcm_channel *volch;
57 TAILQ_ENTRY(dsp_cdevinfo) link;
60 #define PCM_RDCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->rdch)
61 #define PCM_WRCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->wrch)
62 #define PCM_VOLCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->volch)
63 #define PCM_SIMPLEX(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->simplex)
65 #define DSP_CDEVINFO_CACHESIZE 8
67 #define DSP_REGISTERED(x, y) (PCM_REGISTERED(x) && \
68 (y) != NULL && (y)->si_drv1 != NULL)
72 static d_open_t dsp_open;
73 static d_close_t dsp_close;
74 static d_read_t dsp_read;
75 static d_write_t dsp_write;
76 static d_ioctl_t dsp_ioctl;
77 static d_kqfilter_t dsp_kqfilter;
78 static d_mmap_t dsp_mmap;
79 static d_mmap_single_t dsp_mmap_single;
81 static void dsp_filter_detach(struct knote *);
82 static int dsp_filter_read(struct knote *, long);
83 static int dsp_filter_write(struct knote *, long);
85 DEVFS_DEFINE_CLONE_BITMAP(dsp);
87 struct dev_ops dsp_ops = {
88 { "sound", 0, D_MPSAFE },
94 .d_kqfilter = dsp_kqfilter,
96 .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)) {
168 * Note: order is important -
169 * pcm flags -> prio query flags -> wild guess
172 flags = dsp_get_flags(dev);
173 if (flags & SD_F_PRIO_WR) {
175 PCM_RDCH(dev) = NULL;
176 } else if (flags & SD_F_PRIO_RD) {
178 PCM_WRCH(dev) = NULL;
179 } else if (prio & SD_F_PRIO_WR) {
181 PCM_RDCH(dev) = NULL;
182 flags |= SD_F_PRIO_WR;
183 } else if (prio & SD_F_PRIO_RD) {
185 PCM_WRCH(dev) = NULL;
186 flags |= SD_F_PRIO_RD;
187 } else if (PCM_WRCH(dev) != NULL) {
189 PCM_RDCH(dev) = NULL;
190 flags |= SD_F_PRIO_WR;
191 } else if (PCM_RDCH(dev) != NULL) {
193 PCM_WRCH(dev) = NULL;
194 flags |= SD_F_PRIO_RD;
196 PCM_SIMPLEX(dev) = 0;
197 dsp_set_flags(dev, flags);
207 *rdch = PCM_RDCH(dev);
208 *wrch = PCM_WRCH(dev);
210 if (*rdch != NULL && (prio & SD_F_PRIO_RD))
212 if (*wrch != NULL && (prio & SD_F_PRIO_WR))
218 /* unlock specified channels */
220 relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch,
223 if (wrch != NULL && (prio & SD_F_PRIO_WR))
225 if (rdch != NULL && (prio & SD_F_PRIO_RD))
230 dsp_cdevinfo_alloc(struct cdev *dev,
231 struct pcm_channel *rdch, struct pcm_channel *wrch,
232 struct pcm_channel *volch)
234 struct snddev_info *d;
235 struct dsp_cdevinfo *cdi;
238 d = dsp_get_info(dev);
240 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 == NULL &&
241 ((rdch == NULL && wrch == NULL) || rdch != wrch),
242 ("bogus %s(), what are you trying to accomplish here?", __func__));
246 simplex = (dsp_get_flags(dev) & SD_F_SIMPLEX) ? 1 : 0;
249 * Scan for free instance entry and put it into the end of list.
250 * Create new one if necessary.
252 TAILQ_FOREACH(cdi, &d->dsp_cdevinfo_pool, link) {
258 cdi->simplex = simplex;
260 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
261 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
266 cdi = kmalloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
271 cdi->simplex = simplex;
273 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
278 dsp_cdevinfo_free(struct cdev *dev)
280 struct snddev_info *d;
281 struct dsp_cdevinfo *cdi, *tmp;
285 d = dsp_get_info(dev);
287 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 != NULL &&
288 PCM_RDCH(dev) == NULL && PCM_WRCH(dev) == NULL &&
289 PCM_VOLCH(dev) == NULL,
290 ("bogus %s(), what are you trying to accomplish here?", __func__));
303 * Once it is free, move it back to the beginning of list for
304 * faster new entry allocation.
306 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
307 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
310 * Scan the list, cache free entries up to DSP_CDEVINFO_CACHESIZE.
311 * Reset simplex flags.
313 flags = dsp_get_flags(dev) & ~SD_F_PRIO_SET;
314 i = DSP_CDEVINFO_CACHESIZE;
315 TAILQ_FOREACH_MUTABLE(cdi, &d->dsp_cdevinfo_pool, link, tmp) {
316 if (cdi->busy != 0) {
317 if (cdi->simplex == 0) {
318 if (cdi->rdch != NULL)
319 flags |= SD_F_PRIO_RD;
320 if (cdi->wrch != NULL)
321 flags |= SD_F_PRIO_WR;
325 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
326 kfree(cdi, M_DEVBUF);
331 dsp_set_flags(dev, flags);
335 dsp_cdevinfo_init(struct snddev_info *d)
337 struct dsp_cdevinfo *cdi;
340 KASSERT(d != NULL, ("NULL snddev_info"));
344 TAILQ_INIT(&d->dsp_cdevinfo_pool);
345 for (i = 0; i < DSP_CDEVINFO_CACHESIZE; i++) {
346 cdi = kmalloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
347 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
352 dsp_cdevinfo_flush(struct snddev_info *d)
354 struct dsp_cdevinfo *cdi, *tmp;
356 KASSERT(d != NULL, ("NULL snddev_info"));
360 cdi = TAILQ_FIRST(&d->dsp_cdevinfo_pool);
361 while (cdi != NULL) {
362 tmp = TAILQ_NEXT(cdi, link);
363 kfree(cdi, M_DEVBUF);
366 TAILQ_INIT(&d->dsp_cdevinfo_pool);
369 /* duplex / simplex cdev type */
371 DSP_CDEV_TYPE_RDONLY, /* simplex read-only (record) */
372 DSP_CDEV_TYPE_WRONLY, /* simplex write-only (play) */
373 DSP_CDEV_TYPE_RDWR /* duplex read, write, or both */
377 DSP_CDEV_VOLCTL_NONE,
378 DSP_CDEV_VOLCTL_READ,
379 DSP_CDEV_VOLCTL_WRITE
382 #define DSP_F_VALID(x) ((x) & (FREAD | FWRITE))
383 #define DSP_F_DUPLEX(x) (((x) & (FREAD | FWRITE)) == (FREAD | FWRITE))
384 #define DSP_F_SIMPLEX(x) (!DSP_F_DUPLEX(x))
385 #define DSP_F_READ(x) ((x) & FREAD)
386 #define DSP_F_WRITE(x) ((x) & FWRITE)
388 static const struct {
400 { SND_DEV_DSP, "dsp", ".", NULL, 0, 0, 0, 0,
401 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
402 DSP_CDEV_TYPE_RDWR },
403 { SND_DEV_AUDIO, "audio", ".", NULL, 0, 0, 0, 0,
404 SND_FORMAT(AFMT_MU_LAW, 1, 0), DSP_DEFAULT_SPEED,
405 DSP_CDEV_TYPE_RDWR },
406 { SND_DEV_DSP16, "dspW", ".", NULL, 0, 0, 0, 0,
407 SND_FORMAT(AFMT_S16_LE, 1, 0), DSP_DEFAULT_SPEED,
408 DSP_CDEV_TYPE_RDWR },
409 { SND_DEV_DSPHW_PLAY, "dsp", ".p", NULL, 1, 1, SND_MAXHWCHAN, 1,
410 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
411 { SND_DEV_DSPHW_VPLAY, "dsp", ".vp", NULL, 1, 1, SND_MAXVCHANS, 1,
412 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
413 { SND_DEV_DSPHW_REC, "dsp", ".r", NULL, 1, 1, SND_MAXHWCHAN, 1,
414 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
415 { SND_DEV_DSPHW_VREC, "dsp", ".vr", NULL, 1, 1, SND_MAXVCHANS, 1,
416 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
417 { SND_DEV_DSPHW_CD, "dspcd", ".", NULL, 0, 0, 0, 0,
418 SND_FORMAT(AFMT_S16_LE, 2, 0), 44100, DSP_CDEV_TYPE_RDWR },
419 /* Low priority, OSSv4 aliases. */
420 { SND_DEV_DSP, "dsp_ac3", ".", "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_mmap", ".", "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_multich", ".", "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_spdifout", ".", "dsp", 0, 0, 0, 0,
430 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
431 DSP_CDEV_TYPE_RDWR },
432 { SND_DEV_DSP, "dsp_spdifin", ".", "dsp", 0, 0, 0, 0,
433 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
434 DSP_CDEV_TYPE_RDWR },
437 #define DSP_FIXUP_ERROR() do { \
438 prio = dsp_get_flags(i_dev); \
439 if (!DSP_F_VALID(flags)) \
441 if (!DSP_F_DUPLEX(flags) && \
442 ((DSP_F_READ(flags) && d->reccount == 0) || \
443 (DSP_F_WRITE(flags) && d->playcount == 0))) \
445 else if (!DSP_F_DUPLEX(flags) && (prio & SD_F_SIMPLEX) && \
446 ((DSP_F_READ(flags) && (prio & SD_F_PRIO_WR)) || \
447 (DSP_F_WRITE(flags) && (prio & SD_F_PRIO_RD)))) \
449 else if (DSP_REGISTERED(d, i_dev)) \
454 dsp_open(struct dev_open_args *ap)
456 struct cdev *i_dev = ap->a_head.a_dev;
457 int flags = ap->a_oflags;
458 struct pcm_channel *rdch, *wrch;
459 struct snddev_info *d;
460 uint32_t fmt, spd, prio, volctl;
461 int i, error, rderror, wrerror, devtype, wdevunit, rdevunit;
463 /* Kind of impossible.. */
467 d = dsp_get_info(i_dev);
468 if (!PCM_REGISTERED(d))
473 /* Lock snddev so nobody else can monkey with it. */
478 * Try to acquire cloned device before someone else pick it.
479 * ENODEV means this is not a cloned droids.
481 error = snd_clone_acquire(i_dev);
482 if (!(error == 0 || error == ENODEV)) {
493 (void)snd_clone_release(i_dev);
500 * That is just enough. Acquire and unlock pcm lock so
501 * the other will just have to wait until we finish doing
507 devtype = PCMDEV(i_dev);
512 volctl = DSP_CDEV_VOLCTL_NONE;
514 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
515 if (devtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
518 * Volume control only valid for DSPHW devices,
519 * and it must be opened in opposite direction be it
520 * simplex or duplex. Anything else will be handled
523 if (dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY) {
524 if (dsp_cdevs[i].volctl != 0 &&
526 volctl = DSP_CDEV_VOLCTL_WRITE;
530 if (DSP_F_READ(flags)) {
531 (void)snd_clone_release(i_dev);
532 PCM_RELEASE_QUICK(d);
536 wdevunit = dev2unit(i_dev);
537 } else if (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY) {
538 if (dsp_cdevs[i].volctl != 0 &&
539 DSP_F_WRITE(flags)) {
540 volctl = DSP_CDEV_VOLCTL_READ;
544 if (DSP_F_WRITE(flags)) {
545 (void)snd_clone_release(i_dev);
546 PCM_RELEASE_QUICK(d);
550 rdevunit = dev2unit(i_dev);
552 fmt = dsp_cdevs[i].fmt;
553 spd = dsp_cdevs[i].spd;
563 * if we get here, the open request is valid- either:
564 * * we were previously not open
565 * * we were open for play xor record and the opener wants
566 * the non-open direction
568 if (DSP_F_READ(flags)) {
570 rderror = pcm_chnalloc(d, &rdch, PCMDIR_REC,
571 curproc->p_pid, curproc->p_comm, rdevunit);
573 if (rderror == 0 && chn_reset(rdch, fmt, spd) != 0)
576 if (volctl == DSP_CDEV_VOLCTL_READ)
581 pcm_chnrelease(rdch);
582 if (!DSP_F_DUPLEX(flags)) {
583 (void)snd_clone_release(i_dev);
584 PCM_RELEASE_QUICK(d);
589 } else if (volctl == DSP_CDEV_VOLCTL_READ) {
592 pcm_chnrelease(rdch);
596 rdch->flags |= CHN_F_EXCLUSIVE;
598 if (volctl == DSP_CDEV_VOLCTL_NONE)
599 chn_vpc_reset(rdch, SND_VOL_C_PCM, 0);
604 if (DSP_F_WRITE(flags)) {
606 wrerror = pcm_chnalloc(d, &wrch, PCMDIR_PLAY,
607 curproc->p_pid, curproc->p_comm, wdevunit);
609 if (wrerror == 0 && chn_reset(wrch, fmt, spd) != 0)
612 if (volctl == DSP_CDEV_VOLCTL_WRITE)
617 pcm_chnrelease(wrch);
618 if (!DSP_F_DUPLEX(flags)) {
621 * Lock, deref and release previously
622 * created record channel
625 pcm_chnref(rdch, -1);
626 pcm_chnrelease(rdch);
628 (void)snd_clone_release(i_dev);
629 PCM_RELEASE_QUICK(d);
634 } else if (volctl == DSP_CDEV_VOLCTL_WRITE) {
637 pcm_chnrelease(wrch);
641 wrch->flags |= CHN_F_EXCLUSIVE;
643 if (volctl == DSP_CDEV_VOLCTL_NONE)
644 chn_vpc_reset(wrch, SND_VOL_C_PCM, 0);
653 * We're done. Allocate channels information for this cdev.
656 case DSP_CDEV_VOLCTL_READ:
657 KASSERT(wrch == NULL, ("wrch=%p not null!", wrch));
658 dsp_cdevinfo_alloc(i_dev, NULL, NULL, rdch);
660 case DSP_CDEV_VOLCTL_WRITE:
661 KASSERT(rdch == NULL, ("rdch=%p not null!", rdch));
662 dsp_cdevinfo_alloc(i_dev, NULL, NULL, wrch);
664 case DSP_CDEV_VOLCTL_NONE:
666 if (wrch == NULL && rdch == NULL) {
667 (void)snd_clone_release(i_dev);
677 dsp_cdevinfo_alloc(i_dev, rdch, wrch, NULL);
679 CHN_INSERT_HEAD(d, rdch, channels.pcm.opened);
681 CHN_INSERT_HEAD(d, wrch, channels.pcm.opened);
686 * Increase clone refcount for its automatic garbage collector.
688 (void)snd_clone_ref(i_dev);
699 dsp_close(struct dev_close_args *ap)
701 struct cdev *i_dev = ap->a_head.a_dev;
702 struct pcm_channel *rdch, *wrch, *volch;
703 struct snddev_info *d;
704 int sg_ids, rdref, wdref;
708 * subunit is a unique number for the cloned device, it
709 * does NOT represent the virtual device channel.
711 subunit = PCMSUBUNIT(i_dev);
713 d = dsp_get_info(i_dev);
714 if (!DSP_REGISTERED(d, i_dev))
723 rdch = PCM_RDCH(i_dev);
724 wrch = PCM_WRCH(i_dev);
725 volch = PCM_VOLCH(i_dev);
727 PCM_RDCH(i_dev) = NULL;
728 PCM_WRCH(i_dev) = NULL;
729 PCM_VOLCH(i_dev) = NULL;
737 else if (volch == wrch)
741 pcm_chnref(volch, -1);
747 CHN_REMOVE(d, rdch, channels.pcm.opened);
749 CHN_REMOVE(d, wrch, channels.pcm.opened);
751 if (rdch != NULL || wrch != NULL) {
755 * The channel itself need not be locked because:
756 * a) Adding a channel to a syncgroup happens only
757 * in dsp_ioctl(), which cannot run concurrently
759 * b) The syncmember pointer (sm) is protected by
760 * the global syncgroup list lock.
761 * c) A channel can't just disappear, invalidating
762 * pointers, unless it's closed/dereferenced
766 sg_ids = chn_syncdestroy(rdch);
769 free_unr(pcmsg_unrhdr, sg_ids);
772 pcm_chnref(rdch, rdref);
773 chn_abort(rdch); /* won't sleep */
774 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
775 CHN_F_DEAD | CHN_F_EXCLUSIVE);
776 chn_reset(rdch, 0, 0);
777 pcm_chnrelease(rdch);
781 * Please see block above.
784 sg_ids = chn_syncdestroy(wrch);
787 free_unr(pcmsg_unrhdr, sg_ids);
790 pcm_chnref(wrch, wdref);
791 chn_flush(wrch); /* may sleep */
792 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
793 CHN_F_DEAD | CHN_F_EXCLUSIVE);
794 chn_reset(wrch, 0, 0);
795 pcm_chnrelease(wrch);
800 dsp_cdevinfo_free(i_dev);
802 * Release clone busy state and unref it so the automatic
803 * garbage collector will get the hint and do the remaining
806 (void)snd_clone_release(i_dev);
809 * destroy_dev() might sleep, so release pcm lock
810 * here and rely on pcm cv serialization.
813 (void)snd_clone_unref(i_dev);
822 * PCMCHAN(i_dev) is not the actual channel but instead is
823 * simply the unique subunit we assigned to the clone device.
825 * We do not do this here but instead do it when the clone is
829 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(dsp), subunit);
836 dsp_io_ops(struct cdev *i_dev, struct uio *buf, int ioflags)
838 struct snddev_info *d;
839 struct pcm_channel **ch, *rdch, *wrch;
840 int (*chn_io)(struct pcm_channel *, struct uio *, int);
844 KASSERT(i_dev != NULL && buf != NULL &&
845 (buf->uio_rw == UIO_READ || buf->uio_rw == UIO_WRITE),
846 ("%s(): io train wreck!", __func__));
848 d = dsp_get_info(i_dev);
849 if (!DSP_REGISTERED(d, i_dev))
854 switch (buf->uio_rw) {
866 panic("invalid/corrupted uio direction: %d", buf->uio_rw);
872 runpid = buf->uio_td->td_proc->p_pid;
874 getchns(i_dev, &rdch, &wrch, prio);
876 if (*ch == NULL || !((*ch)->flags & CHN_F_BUSY)) {
881 if (((*ch)->flags & (CHN_F_MMAP | CHN_F_DEAD)) ||
882 (((*ch)->flags & CHN_F_RUNNING) && (*ch)->pid != runpid)) {
883 relchns(i_dev, rdch, wrch, prio);
886 } else if (!((*ch)->flags & CHN_F_RUNNING)) {
887 (*ch)->flags |= CHN_F_RUNNING;
892 * chn_read/write must give up channel lock in order to copy bytes
893 * from/to userland, so up the "in progress" counter to make sure
894 * someone else doesn't come along and muss up the buffer.
897 ret = chn_io(*ch, buf, ioflags);
900 CHN_BROADCAST(&(*ch)->cv);
902 relchns(i_dev, rdch, wrch, prio);
910 dsp_read(struct dev_read_args *ap)
912 struct cdev *i_dev = ap->a_head.a_dev;
913 struct uio *buf = ap->a_uio;
915 return (dsp_io_ops(i_dev, buf, ap->a_ioflag));
919 dsp_write(struct dev_write_args *ap)
921 struct cdev *i_dev = ap->a_head.a_dev;
922 struct uio *buf = ap->a_uio;
924 return (dsp_io_ops(i_dev, buf, ap->a_ioflag));
928 dsp_get_volume_channel(struct cdev *dev, struct pcm_channel **volch)
930 struct snddev_info *d;
931 struct pcm_channel *c;
934 KASSERT(dev != NULL && volch != NULL,
935 ("%s(): NULL query dev=%p volch=%p", __func__, dev, volch));
937 d = dsp_get_info(dev);
938 if (!PCM_REGISTERED(d)) {
949 if (!(c->feederflags & (1 << FEEDER_VOLUME)))
959 unit = dev2unit(dev);
961 CHN_FOREACH(c, d, channels.pcm) {
963 if (c->unit != unit) {
973 return ((c->feederflags & (1 << FEEDER_VOLUME)) ? 0 : -1);
983 dsp_ioctl_channel(struct cdev *dev, struct pcm_channel *volch, u_long cmd,
986 struct snddev_info *d;
987 struct pcm_channel *rdch, *wrch;
990 d = dsp_get_info(dev);
991 if (!PCM_REGISTERED(d) || !(dsp_get_flags(dev) & SD_F_VPC))
998 rdch = PCM_RDCH(dev);
999 wrch = PCM_WRCH(dev);
1001 /* No specific channel, look into cache */
1003 volch = PCM_VOLCH(dev);
1006 if (volch == NULL) {
1007 if (j == SOUND_MIXER_RECLEV && rdch != NULL)
1009 else if (j == SOUND_MIXER_PCM && wrch != NULL)
1013 devtype = PCMDEV(dev);
1015 /* Look super harder */
1016 if (volch == NULL &&
1017 (devtype == SND_DEV_DSPHW_PLAY || devtype == SND_DEV_DSPHW_VPLAY ||
1018 devtype == SND_DEV_DSPHW_REC || devtype == SND_DEV_DSPHW_VREC)) {
1019 ret = dsp_get_volume_channel(dev, &volch);
1026 /* Final validation */
1027 if (volch != NULL) {
1029 if (!(volch->feederflags & (1 << FEEDER_VOLUME))) {
1033 if (volch->direction == PCMDIR_PLAY)
1041 if (volch != NULL &&
1042 ((j == SOUND_MIXER_PCM && volch->direction == PCMDIR_PLAY) ||
1043 (j == SOUND_MIXER_RECLEV && volch->direction == PCMDIR_REC))) {
1044 if ((cmd & ~0xff) == MIXER_WRITE(0)) {
1045 int left, right, center;
1047 left = *(int *)arg & 0x7f;
1048 right = ((*(int *)arg) >> 8) & 0x7f;
1049 center = (left + right) >> 1;
1050 chn_setvolume_multi(volch, SND_VOL_C_PCM, left, right,
1052 } else if ((cmd & ~0xff) == MIXER_READ(0)) {
1053 *(int *)arg = CHN_GETVOLUME(volch,
1054 SND_VOL_C_PCM, SND_CHN_T_FL);
1055 *(int *)arg |= CHN_GETVOLUME(volch,
1056 SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
1059 } else if (rdch != NULL || wrch != NULL) {
1061 case SOUND_MIXER_DEVMASK:
1062 case SOUND_MIXER_CAPS:
1063 case SOUND_MIXER_STEREODEVS:
1064 if ((cmd & ~0xff) == MIXER_READ(0)) {
1067 *(int *)arg |= SOUND_MASK_RECLEV;
1069 *(int *)arg |= SOUND_MASK_PCM;
1073 case SOUND_MIXER_RECMASK:
1074 case SOUND_MIXER_RECSRC:
1075 if ((cmd & ~0xff) == MIXER_READ(0))
1091 dsp_ioctl(struct dev_ioctl_args *ap)
1093 struct cdev *i_dev = ap->a_head.a_dev;
1094 u_long cmd = ap->a_cmd;
1095 caddr_t arg = ap->a_data;
1096 struct pcm_channel *chn, *rdch, *wrch;
1097 struct snddev_info *d;
1099 int *arg_i, ret, tmp;
1101 d = dsp_get_info(i_dev);
1102 if (!DSP_REGISTERED(d, i_dev))
1112 if (IOCGROUP(cmd) == 'M') {
1113 if (cmd == OSS_GETVERSION) {
1114 *arg_i = SOUND_VERSION;
1118 ret = dsp_ioctl_channel(i_dev, PCM_VOLCH(i_dev), cmd, arg);
1124 if (d->mixer_dev != NULL) {
1125 PCM_ACQUIRE_QUICK(d);
1126 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1,
1128 PCM_RELEASE_QUICK(d);
1138 * Certain ioctls may be made on any type of device (audio, mixer,
1139 * and MIDI). Handle those special cases here.
1141 if (IOCGROUP(cmd) == 'X') {
1142 PCM_ACQUIRE_QUICK(d);
1144 case SNDCTL_SYSINFO:
1145 sound_oss_sysinfo((oss_sysinfo *)arg);
1147 case SNDCTL_CARDINFO:
1148 ret = sound_oss_card_info((oss_card_info *)arg);
1150 case SNDCTL_AUDIOINFO:
1151 case SNDCTL_AUDIOINFO_EX:
1152 case SNDCTL_ENGINEINFO:
1153 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
1155 case SNDCTL_MIXERINFO:
1156 ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
1161 PCM_RELEASE_QUICK(d);
1166 getchns(i_dev, &rdch, &wrch, 0);
1168 if (wrch != NULL && (wrch->flags & CHN_F_DEAD))
1170 if (rdch != NULL && (rdch->flags & CHN_F_DEAD))
1173 if (wrch == NULL && rdch == NULL) {
1181 * we start with the new ioctl interface.
1183 case AIONWRITE: /* how many bytes can write ? */
1187 if (wrch && wrch->bufhard.dl)
1188 while (chn_wrfeed(wrch) == 0);
1190 *arg_i = sndbuf_getfree(wrch->bufsoft);
1198 case AIOSSIZE: /* set the current blocksize */
1200 struct snd_size *p = (struct snd_size *)arg;
1204 PCM_ACQUIRE_QUICK(d);
1207 chn_setblocksize(wrch, 2, p->play_size);
1208 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1213 chn_setblocksize(rdch, 2, p->rec_size);
1214 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1217 PCM_RELEASE_QUICK(d);
1220 case AIOGSIZE: /* get the current blocksize */
1222 struct snd_size *p = (struct snd_size *)arg;
1226 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1231 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1240 snd_chan_param *p = (snd_chan_param *)arg;
1242 if (cmd == AIOSFMT &&
1243 ((p->play_format != 0 && p->play_rate == 0) ||
1244 (p->rec_format != 0 && p->rec_rate == 0))) {
1248 PCM_ACQUIRE_QUICK(d);
1251 if (cmd == AIOSFMT && p->play_format != 0) {
1253 SND_FORMAT(p->play_format,
1254 AFMT_CHANNEL(wrch->format),
1255 AFMT_EXTCHANNEL(wrch->format)));
1256 chn_setspeed(wrch, p->play_rate);
1258 p->play_rate = wrch->speed;
1259 p->play_format = AFMT_ENCODING(wrch->format);
1267 if (cmd == AIOSFMT && p->rec_format != 0) {
1269 SND_FORMAT(p->rec_format,
1270 AFMT_CHANNEL(rdch->format),
1271 AFMT_EXTCHANNEL(rdch->format)));
1272 chn_setspeed(rdch, p->rec_rate);
1274 p->rec_rate = rdch->speed;
1275 p->rec_format = AFMT_ENCODING(rdch->format);
1281 PCM_RELEASE_QUICK(d);
1285 case AIOGCAP: /* get capabilities */
1287 snd_capabilities *p = (snd_capabilities *)arg;
1288 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
1294 rcaps = chn_getcaps(rdch);
1298 pcaps = chn_getcaps(wrch);
1300 p->rate_min = max(rcaps? rcaps->minspeed : 0,
1301 pcaps? pcaps->minspeed : 0);
1302 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
1303 pcaps? pcaps->maxspeed : 1000000);
1304 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
1305 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
1306 /* XXX bad on sb16 */
1307 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
1308 (wrch? chn_getformats(wrch) : 0xffffffff);
1310 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
1311 pdev = d->mixer_dev;
1312 p->mixers = 1; /* default: one mixer */
1313 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
1314 p->left = p->right = 100;
1324 if (*arg_i == AIOSYNC_PLAY && wrch) {
1326 *arg_i = chn_abort(wrch);
1328 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
1330 *arg_i = chn_abort(rdch);
1333 kprintf("AIOSTOP: bad channel 0x%x\n", *arg_i);
1339 kprintf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
1340 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
1344 * here follow the standard ioctls (filio.h etc.)
1346 case FIONREAD: /* get # bytes to read */
1349 /* if (rdch && rdch->bufhard.dl)
1350 while (chn_rdfeed(rdch) == 0);
1352 *arg_i = sndbuf_getready(rdch->bufsoft);
1360 case FIOASYNC: /*set/clear async i/o */
1361 DEB( kprintf("FIOASYNC\n") ; )
1364 case FIONBIO: /* set/clear non-blocking i/o */
1365 DEB( kprintf("FIONBIO\n") ; )
1368 case SNDCTL_DSP_NONBLOCK: /* set non-blocking i/o */
1369 atomic_set_int(&ap->a_fp->f_flag, FNONBLOCK);
1373 * Finally, here is the linux-compatible ioctl interface
1375 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
1376 case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
1377 case SNDCTL_DSP_GETBLKSIZE:
1378 chn = wrch ? wrch : rdch;
1381 *arg_i = sndbuf_getblksz(chn->bufsoft);
1389 case SNDCTL_DSP_SETBLKSIZE:
1390 RANGE(*arg_i, 16, 65536);
1391 PCM_ACQUIRE_QUICK(d);
1394 chn_setblocksize(wrch, 2, *arg_i);
1399 chn_setblocksize(rdch, 2, *arg_i);
1402 PCM_RELEASE_QUICK(d);
1405 case SNDCTL_DSP_RESET:
1406 DEB(kprintf("dsp reset\n"));
1421 case SNDCTL_DSP_SYNC:
1422 DEB(kprintf("dsp sync\n"));
1423 /* chn_sync may sleep */
1431 case SNDCTL_DSP_SPEED:
1432 /* chn_setspeed may sleep */
1434 PCM_ACQUIRE_QUICK(d);
1437 ret = chn_setspeed(wrch, *arg_i);
1441 if (rdch && ret == 0) {
1443 ret = chn_setspeed(rdch, *arg_i);
1448 PCM_RELEASE_QUICK(d);
1452 case SOUND_PCM_READ_RATE:
1453 chn = wrch ? wrch : rdch;
1456 *arg_i = chn->speed;
1464 case SNDCTL_DSP_STEREO:
1466 *arg_i = (*arg_i)? 2 : 1;
1467 PCM_ACQUIRE_QUICK(d);
1470 ret = chn_setformat(wrch,
1471 SND_FORMAT(wrch->format, *arg_i, 0));
1472 tmp = (AFMT_CHANNEL(wrch->format) > 1)? 1 : 0;
1475 if (rdch && ret == 0) {
1477 ret = chn_setformat(rdch,
1478 SND_FORMAT(rdch->format, *arg_i, 0));
1480 tmp = (AFMT_CHANNEL(rdch->format) > 1)? 1 : 0;
1483 PCM_RELEASE_QUICK(d);
1487 case SOUND_PCM_WRITE_CHANNELS:
1488 /* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
1495 struct pcmchan_matrix *m;
1499 if (*arg_i > SND_CHN_MAX)
1500 *arg_i = SND_CHN_MAX;
1502 m = feeder_matrix_default_channel_map(*arg_i);
1508 PCM_ACQUIRE_QUICK(d);
1511 ret = chn_setformat(wrch,
1512 SND_FORMAT(wrch->format, *arg_i, ext));
1513 tmp = AFMT_CHANNEL(wrch->format);
1516 if (rdch && ret == 0) {
1518 ret = chn_setformat(rdch,
1519 SND_FORMAT(rdch->format, *arg_i, ext));
1521 tmp = AFMT_CHANNEL(rdch->format);
1524 PCM_RELEASE_QUICK(d);
1527 chn = wrch ? wrch : rdch;
1529 *arg_i = AFMT_CHANNEL(chn->format);
1534 case SOUND_PCM_READ_CHANNELS:
1535 chn = wrch ? wrch : rdch;
1538 *arg_i = AFMT_CHANNEL(chn->format);
1546 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
1547 chn = wrch ? wrch : rdch;
1550 *arg_i = chn_getformats(chn);
1558 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
1559 if (*arg_i != AFMT_QUERY) {
1561 PCM_ACQUIRE_QUICK(d);
1564 ret = chn_setformat(wrch, SND_FORMAT(*arg_i,
1565 AFMT_CHANNEL(wrch->format),
1566 AFMT_EXTCHANNEL(wrch->format)));
1570 if (rdch && ret == 0) {
1572 ret = chn_setformat(rdch, SND_FORMAT(*arg_i,
1573 AFMT_CHANNEL(rdch->format),
1574 AFMT_EXTCHANNEL(rdch->format)));
1579 PCM_RELEASE_QUICK(d);
1580 *arg_i = AFMT_ENCODING(tmp);
1582 chn = wrch ? wrch : rdch;
1584 *arg_i = AFMT_ENCODING(chn->format);
1589 case SNDCTL_DSP_SETFRAGMENT:
1590 DEB(kprintf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
1592 uint32_t fragln = (*arg_i) & 0x0000ffff;
1593 uint32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
1595 uint32_t r_maxfrags, r_fragsz;
1597 RANGE(fragln, 4, 16);
1598 fragsz = 1 << fragln;
1601 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1604 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
1605 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1607 DEB(kprintf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
1608 PCM_ACQUIRE_QUICK(d);
1611 ret = chn_setblocksize(rdch, maxfrags, fragsz);
1612 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
1613 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
1616 r_maxfrags = maxfrags;
1619 if (wrch && ret == 0) {
1621 ret = chn_setblocksize(wrch, maxfrags, fragsz);
1622 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
1623 fragsz = sndbuf_getblksz(wrch->bufsoft);
1625 } else { /* use whatever came from the read channel */
1626 maxfrags = r_maxfrags;
1629 PCM_RELEASE_QUICK(d);
1632 while (fragsz > 1) {
1636 *arg_i = (maxfrags << 16) | fragln;
1640 case SNDCTL_DSP_GETISPACE:
1641 /* return the size of data available in the input queue */
1643 audio_buf_info *a = (audio_buf_info *)arg;
1645 struct snd_dbuf *bs = rdch->bufsoft;
1648 a->bytes = sndbuf_getready(bs);
1649 a->fragments = a->bytes / sndbuf_getblksz(bs);
1650 a->fragstotal = sndbuf_getblkcnt(bs);
1651 a->fragsize = sndbuf_getblksz(bs);
1658 case SNDCTL_DSP_GETOSPACE:
1659 /* return space available in the output queue */
1661 audio_buf_info *a = (audio_buf_info *)arg;
1663 struct snd_dbuf *bs = wrch->bufsoft;
1666 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1667 a->bytes = sndbuf_getfree(bs);
1668 a->fragments = a->bytes / sndbuf_getblksz(bs);
1669 a->fragstotal = sndbuf_getblkcnt(bs);
1670 a->fragsize = sndbuf_getblksz(bs);
1677 case SNDCTL_DSP_GETIPTR:
1679 count_info *a = (count_info *)arg;
1681 struct snd_dbuf *bs = rdch->bufsoft;
1684 /* XXX abusive DMA update: chn_rdupdate(rdch); */
1685 a->bytes = sndbuf_gettotal(bs);
1686 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
1687 a->ptr = sndbuf_getfreeptr(bs);
1688 rdch->blocks = sndbuf_getblocks(bs);
1695 case SNDCTL_DSP_GETOPTR:
1697 count_info *a = (count_info *)arg;
1699 struct snd_dbuf *bs = wrch->bufsoft;
1702 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1703 a->bytes = sndbuf_gettotal(bs);
1704 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
1705 a->ptr = sndbuf_getreadyptr(bs);
1706 wrch->blocks = sndbuf_getblocks(bs);
1713 case SNDCTL_DSP_GETCAPS:
1715 *arg_i = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER;
1716 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1717 *arg_i |= PCM_CAP_DUPLEX;
1721 case SOUND_PCM_READ_BITS:
1722 chn = wrch ? wrch : rdch;
1725 if (chn->format & AFMT_8BIT)
1727 else if (chn->format & AFMT_16BIT)
1729 else if (chn->format & AFMT_24BIT)
1731 else if (chn->format & AFMT_32BIT)
1742 case SNDCTL_DSP_SETTRIGGER:
1745 rdch->flags &= ~CHN_F_NOTRIGGER;
1746 if (*arg_i & PCM_ENABLE_INPUT)
1751 rdch->flags |= CHN_F_NOTRIGGER;
1757 wrch->flags &= ~CHN_F_NOTRIGGER;
1758 if (*arg_i & PCM_ENABLE_OUTPUT)
1763 wrch->flags |= CHN_F_NOTRIGGER;
1769 case SNDCTL_DSP_GETTRIGGER:
1773 if (wrch->flags & CHN_F_TRIGGERED)
1774 *arg_i |= PCM_ENABLE_OUTPUT;
1779 if (rdch->flags & CHN_F_TRIGGERED)
1780 *arg_i |= PCM_ENABLE_INPUT;
1785 case SNDCTL_DSP_GETODELAY:
1787 struct snd_dbuf *bs = wrch->bufsoft;
1790 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1791 *arg_i = sndbuf_getready(bs);
1797 case SNDCTL_DSP_POST:
1800 wrch->flags &= ~CHN_F_NOTRIGGER;
1806 case SNDCTL_DSP_SETDUPLEX:
1808 * switch to full-duplex mode if card is in half-duplex
1809 * mode and is able to work in full-duplex mode
1812 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1813 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1818 * The following four ioctls are simple wrappers around mixer_ioctl
1819 * with no further processing. xcmd is short for "translated
1822 case SNDCTL_DSP_GETRECVOL:
1824 xcmd = SOUND_MIXER_READ_RECLEV;
1828 case SNDCTL_DSP_SETRECVOL:
1830 xcmd = SOUND_MIXER_WRITE_RECLEV;
1834 case SNDCTL_DSP_GETPLAYVOL:
1836 xcmd = SOUND_MIXER_READ_PCM;
1840 case SNDCTL_DSP_SETPLAYVOL:
1842 xcmd = SOUND_MIXER_WRITE_PCM;
1846 ret = dsp_ioctl_channel(i_dev, chn, xcmd, arg);
1852 if (d->mixer_dev != NULL) {
1853 PCM_ACQUIRE_QUICK(d);
1854 ret = mixer_ioctl_cmd(d->mixer_dev, xcmd, arg, -1,
1856 PCM_RELEASE_QUICK(d);
1862 case SNDCTL_DSP_GET_RECSRC_NAMES:
1863 case SNDCTL_DSP_GET_RECSRC:
1864 case SNDCTL_DSP_SET_RECSRC:
1865 if (d->mixer_dev != NULL) {
1866 PCM_ACQUIRE_QUICK(d);
1867 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1,
1869 PCM_RELEASE_QUICK(d);
1875 * The following 3 ioctls aren't very useful at the moment. For
1876 * now, only a single channel is associated with a cdev (/dev/dspN
1877 * instance), so there's only a single output routing to use (i.e.,
1878 * the wrch bound to this cdev).
1880 case SNDCTL_DSP_GET_PLAYTGT_NAMES:
1882 oss_mixer_enuminfo *ei;
1883 ei = (oss_mixer_enuminfo *)arg;
1886 ei->version = 0; /* static for now */
1887 ei->strindex[0] = 0;
1891 strlcpy(ei->strings, wrch->name,
1892 sizeof(ei->strings));
1895 ei->strings[0] = '\0';
1899 case SNDCTL_DSP_GET_PLAYTGT:
1900 case SNDCTL_DSP_SET_PLAYTGT: /* yes, they are the same for now */
1903 * OSSv4: "The value that was accepted by the device will
1904 * be returned back in the variable pointed by the
1913 case SNDCTL_DSP_SILENCE:
1915 * Flush the software (pre-feed) buffer, but try to minimize playback
1916 * interruption. (I.e., record unplayed samples with intent to
1917 * restore by SNDCTL_DSP_SKIP.) Intended for application "pause"
1923 struct snd_dbuf *bs;
1925 while (wrch->inprog != 0)
1926 cv_wait(&wrch->cv, wrch->lock);
1928 if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) {
1929 bs->sl = sndbuf_getready(bs);
1930 sndbuf_dispose(bs, bs->shadbuf, sndbuf_getready(bs));
1931 sndbuf_fillsilence(bs);
1938 case SNDCTL_DSP_SKIP:
1940 * OSSv4 docs: "This ioctl call discards all unplayed samples in the
1941 * playback buffer by moving the current write position immediately
1942 * before the point where the device is currently reading the samples."
1947 struct snd_dbuf *bs;
1949 while (wrch->inprog != 0)
1950 cv_wait(&wrch->cv, wrch->lock);
1952 if ((bs->shadbuf != NULL) && (bs->sl > 0)) {
1953 sndbuf_softreset(bs);
1954 sndbuf_acquire(bs, bs->shadbuf, bs->sl);
1962 case SNDCTL_DSP_CURRENT_OPTR:
1963 case SNDCTL_DSP_CURRENT_IPTR:
1965 * @note Changing formats resets the buffer counters, which differs
1966 * from the 4Front drivers. However, I don't expect this to be
1967 * much of a problem.
1969 * @note In a test where @c CURRENT_OPTR is called immediately after write
1970 * returns, this driver is about 32K samples behind whereas
1971 * 4Front's is about 8K samples behind. Should determine source
1972 * of discrepancy, even if only out of curiosity.
1974 * @todo Actually test SNDCTL_DSP_CURRENT_IPTR.
1976 chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch;
1980 struct snd_dbuf *bs;
1983 oss_count_t *oc = (oss_count_t *)arg;
1988 tmp = (sndbuf_getsize(b) + chn_getptr(chn) - sndbuf_gethwptr(b)) % sndbuf_getsize(b);
1989 oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getalign(b);
1990 oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getalign(b);
1992 oc->samples = sndbuf_gettotal(bs) / sndbuf_getalign(bs);
1993 oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getalign(bs);
1999 case SNDCTL_DSP_HALT_OUTPUT:
2000 case SNDCTL_DSP_HALT_INPUT:
2001 chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch;
2011 case SNDCTL_DSP_LOW_WATER:
2013 * Set the number of bytes required to attract attention by
2018 wrch->lw = (*arg_i > 1) ? *arg_i : 1;
2023 rdch->lw = (*arg_i > 1) ? *arg_i : 1;
2028 case SNDCTL_DSP_GETERROR:
2030 * OSSv4 docs: "All errors and counters will automatically be
2031 * cleared to zeroes after the call so each call will return only
2032 * the errors that occurred after the previous invocation. ... The
2033 * play_underruns and rec_overrun fields are the only useful fields
2034 * returned by OSS 4.0."
2037 audio_errinfo *ei = (audio_errinfo *)arg;
2039 bzero((void *)ei, sizeof(*ei));
2043 ei->play_underruns = wrch->xruns;
2049 ei->rec_overruns = rdch->xruns;
2056 case SNDCTL_DSP_SYNCGROUP:
2057 PCM_ACQUIRE_QUICK(d);
2058 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
2059 PCM_RELEASE_QUICK(d);
2062 case SNDCTL_DSP_SYNCSTART:
2063 PCM_ACQUIRE_QUICK(d);
2064 ret = dsp_oss_syncstart(*arg_i);
2065 PCM_RELEASE_QUICK(d);
2068 case SNDCTL_DSP_POLICY:
2069 PCM_ACQUIRE_QUICK(d);
2070 ret = dsp_oss_policy(wrch, rdch, *arg_i);
2071 PCM_RELEASE_QUICK(d);
2074 case SNDCTL_DSP_COOKEDMODE:
2075 PCM_ACQUIRE_QUICK(d);
2076 if (!(dsp_get_flags(i_dev) & SD_F_BITPERFECT))
2077 ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
2078 PCM_RELEASE_QUICK(d);
2080 case SNDCTL_DSP_GET_CHNORDER:
2081 PCM_ACQUIRE_QUICK(d);
2082 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
2083 PCM_RELEASE_QUICK(d);
2085 case SNDCTL_DSP_SET_CHNORDER:
2086 PCM_ACQUIRE_QUICK(d);
2087 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
2088 PCM_RELEASE_QUICK(d);
2090 case SNDCTL_DSP_GETCHANNELMASK: /* XXX vlc */
2091 PCM_ACQUIRE_QUICK(d);
2092 ret = dsp_oss_getchannelmask(wrch, rdch, (int *)arg);
2093 PCM_RELEASE_QUICK(d);
2095 case SNDCTL_DSP_BIND_CHANNEL: /* XXX what?!? */
2098 #ifdef OSSV4_EXPERIMENT
2100 * XXX The following ioctls are not yet supported and just return
2103 case SNDCTL_DSP_GETOPEAKS:
2104 case SNDCTL_DSP_GETIPEAKS:
2105 chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch;
2109 oss_peaks_t *op = (oss_peaks_t *)arg;
2113 ret = chn_getpeaks(chn, &lpeak, &rpeak);
2125 * XXX Once implemented, revisit this for proper cv protection
2128 case SNDCTL_GETLABEL:
2129 ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg);
2131 case SNDCTL_SETLABEL:
2132 ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg);
2134 case SNDCTL_GETSONG:
2135 ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg);
2137 case SNDCTL_SETSONG:
2138 ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg);
2140 case SNDCTL_SETNAME:
2141 ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg);
2145 * @note The S/PDIF interface ioctls, @c SNDCTL_DSP_READCTL and
2146 * @c SNDCTL_DSP_WRITECTL have been omitted at the suggestion of
2147 * 4Front Technologies.
2149 case SNDCTL_DSP_READCTL:
2150 case SNDCTL_DSP_WRITECTL:
2153 #endif /* !0 (explicitly omitted ioctls) */
2155 #endif /* !OSSV4_EXPERIMENT */
2156 case SNDCTL_DSP_MAPINBUF:
2157 case SNDCTL_DSP_MAPOUTBUF:
2158 case SNDCTL_DSP_SETSYNCRO:
2161 case SNDCTL_DSP_SUBDIVIDE:
2162 case SOUND_PCM_WRITE_FILTER:
2163 case SOUND_PCM_READ_FILTER:
2164 /* dunno what these do, don't sound important */
2167 DEB(kprintf("default ioctl fn 0x%08lx fail\n", cmd));
2177 static struct filterops dsp_read_filtops =
2178 { FILTEROP_ISFD, NULL, dsp_filter_detach, dsp_filter_read };
2179 static struct filterops dsp_write_filtops =
2180 { FILTEROP_ISFD, NULL, dsp_filter_detach, dsp_filter_write };
2183 /*dsp_poll(struct cdev *i_dev, int events, struct thread *td)*/
2184 dsp_kqfilter(struct dev_kqfilter_args *ap)
2186 struct knote *kn = ap->a_kn;
2187 struct klist *klist;
2188 struct cdev *i_dev = ap->a_head.a_dev;
2189 struct snddev_info *d;
2190 struct pcm_channel *wrch, *rdch;
2191 struct snd_dbuf *bs = NULL;
2194 d = dsp_get_info(i_dev);
2195 if (!DSP_REGISTERED(d, i_dev))
2204 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2206 switch (kn->kn_filter) {
2209 kn->kn_fop = &dsp_read_filtops;
2210 kn->kn_hook = (caddr_t)rdch;
2217 kn->kn_fop = &dsp_write_filtops;
2218 kn->kn_hook = (caddr_t)wrch;
2224 ap->a_result = EOPNOTSUPP;
2228 if (ap->a_result == 0) {
2229 klist = &sndbuf_getkq(bs)->ki_note;
2230 knote_insert(klist, kn);
2233 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2241 dsp_filter_detach(struct knote *kn)
2243 struct pcm_channel *ch = (struct pcm_channel *)kn->kn_hook;
2244 struct snd_dbuf *bs = ch->bufsoft;
2245 struct klist *klist;
2248 klist = &sndbuf_getkq(bs)->ki_note;
2249 knote_remove(klist, kn);
2254 dsp_filter_read(struct knote *kn, long hint)
2256 struct pcm_channel *rdch = (struct pcm_channel *)kn->kn_hook;
2257 struct thread *td = curthread;
2261 ready = chn_poll(rdch, 1, td);
2268 dsp_filter_write(struct knote *kn, long hint)
2270 struct pcm_channel *wrch = (struct pcm_channel *)kn->kn_hook;
2271 struct thread *td = curthread;
2275 ready = chn_poll(wrch, 1, td);
2282 dsp_mmap(struct dev_mmap_args *ap)
2284 vm_offset_t offset = ap->a_offset;
2286 /* XXX memattr is not honored */
2287 ap->a_result = vtophys(offset);
2292 dsp_mmap_single(struct dev_mmap_single_args *ap)
2294 struct cdev *i_dev = ap->a_head.a_dev;
2295 vm_ooffset_t *offset = ap->a_offset;
2296 vm_size_t size = ap->a_size;
2297 struct vm_object **object = ap->a_object;
2298 int nprot = ap->a_nprot;
2299 struct snddev_info *d;
2300 struct pcm_channel *wrch, *rdch, *c;
2303 * Reject PROT_EXEC by default. It just doesn't makes sense.
2304 * Unfortunately, we have to give up this one due to linux_mmap
2307 * http://lists.freebsd.org/pipermail/freebsd-emulation/2007-June/003698.html
2311 if ((nprot & PROT_EXEC) && (dsp_mmap_allow_prot_exec < 0 ||
2312 (dsp_mmap_allow_prot_exec == 0 &&
2313 SV_CURPROC_ABI() != SV_ABI_LINUX)))
2315 if ((nprot & PROT_EXEC) && dsp_mmap_allow_prot_exec < 1)
2320 * PROT_READ (alone) selects the input buffer.
2321 * PROT_WRITE (alone) selects the output buffer.
2322 * PROT_WRITE|PROT_READ together select the output buffer.
2324 if ((nprot & (PROT_READ | PROT_WRITE)) == 0)
2327 d = dsp_get_info(i_dev);
2328 if (!DSP_REGISTERED(d, i_dev))
2333 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2335 c = ((nprot & PROT_WRITE) != 0) ? wrch : rdch;
2336 if (c == NULL || (c->flags & CHN_F_MMAP_INVALID) ||
2337 (*offset + size) > sndbuf_getsize(c->bufsoft) ||
2338 (wrch != NULL && (wrch->flags & CHN_F_MMAP_INVALID)) ||
2339 (rdch != NULL && (rdch->flags & CHN_F_MMAP_INVALID))) {
2340 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2346 wrch->flags |= CHN_F_MMAP;
2348 rdch->flags |= CHN_F_MMAP;
2350 *offset = (uintptr_t)sndbuf_getbufofs(c->bufsoft, *offset);
2351 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2352 *object = dev_pager_alloc(i_dev, size, nprot, *offset);
2356 if (*object == NULL)
2362 * for i = 0 to channels of device N
2363 * if dspN.i isn't busy and in the right dir, create a dev_t and return it
2366 dsp_clone(struct dev_clone_args *ap)
2368 struct cdev *i_dev = ap->a_head.a_dev;
2369 struct snddev_info *d;
2370 struct snd_clone_entry *ce;
2371 struct pcm_channel *c;
2372 int i, unit, udcmask, cunit, devtype, devhw, devcmax, tumax;
2376 static struct cdev *dev = NULL;
2378 KASSERT(dsp_umax >= 0 && dsp_cmax >= 0, ("Uninitialized unit!"));
2381 * The default dsp device has a special unit which must be adjusted,
2382 * otherwise the unit number is already correct.
2384 unit = PCMUNIT(i_dev);
2385 if (unit == PCMUNIT_DEFAULT)
2388 ksnprintf(sname, sizeof(sname), "%s", ap->a_name);
2389 len = strlen(sname);
2390 while (len && sname[len-1] >= '0' && sname[len-1] <= '9')
2400 for (i = 0; i < sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0]); ++i) {
2401 if (strcmp(dsp_cdevs[i].name, sname) != 0)
2403 devtype = dsp_cdevs[i].type;
2404 devhw = dsp_cdevs[i].hw;
2405 devcmax = dsp_cdevs[i].max - 1;
2409 d = devclass_get_softc(pcm_devclass, unit);
2410 if (!PCM_REGISTERED(d) || d->clones == NULL) {
2414 /* XXX Need Giant magic entry ??? */
2417 if (snd_clone_disabled(d->clones)) {
2426 udcmask = snd_u2unit(unit) | snd_d2unit(devtype);
2429 KASSERT(devcmax <= dsp_cmax,
2430 ("overflow: devcmax=%d, dsp_cmax=%d", devcmax, dsp_cmax));
2431 if (cunit > devcmax) {
2432 PCM_RELEASE_QUICK(d);
2435 udcmask |= snd_c2unit(cunit);
2436 CHN_FOREACH(c, d, channels.pcm) {
2438 if (c->unit != udcmask) {
2443 udcmask &= ~snd_c2unit(cunit);
2445 * Temporarily increase clone maxunit to overcome
2446 * vchan flexibility.
2448 * # sysctl dev.pcm.0.play.vchans=256
2449 * dev.pcm.0.play.vchans: 1 -> 256
2450 * # cat /dev/zero > /dev/dsp0.vp255 &
2452 * # sysctl dev.pcm.0.play.vchans=0
2453 * dev.pcm.0.play.vchans: 256 -> 1
2455 * [1] + running cat /dev/zero > /dev/dsp0.vp255
2457 * # cat /dev/zero > /dev/dsp0.vp255
2458 * zsh: operation not supported: /dev/dsp0.vp255
2460 tumax = snd_clone_getmaxunit(d->clones);
2462 snd_clone_setmaxunit(d->clones, cunit);
2465 goto dsp_clone_alloc;
2468 * Ok, so we're requesting unallocated vchan, but still
2469 * within maximum vchan limit.
2471 if (((devtype == SND_DEV_DSPHW_VPLAY && d->pvchancount > 0) ||
2472 (devtype == SND_DEV_DSPHW_VREC && d->rvchancount > 0)) &&
2473 cunit < snd_maxautovchans) {
2474 udcmask &= ~snd_c2unit(cunit);
2475 tumax = snd_clone_getmaxunit(d->clones);
2477 snd_clone_setmaxunit(d->clones, cunit);
2480 goto dsp_clone_alloc;
2482 PCM_RELEASE_QUICK(d);
2487 * Allocate channel, create device if necessary.
2490 ce = snd_clone_alloc(d->clones, &dev, &cunit, udcmask);
2492 snd_clone_setmaxunit(d->clones, tumax);
2495 * NOTE: Subunit is a unique number unrelated to the
2498 udcmask |= snd_c2unit(cunit);
2499 int subunit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(dsp), 0);
2501 dev = make_only_dev(&dsp_ops,
2502 PCMMKMINOR(unit, devtype, subunit),
2503 UID_ROOT, GID_WHEEL, 0666, "%s%d.%d",
2504 sname, unit, subunit);
2505 snd_clone_register(ce, dev);
2509 * Use device already registered, we must add a ref to the
2517 PCM_RELEASE_QUICK(d);
2523 dsp_sysinit(void *p)
2525 if (dsp_ehtag != NULL)
2527 /* initialize unit numbering */
2529 dsp_umax = PCMMAXUNIT;
2530 dsp_cmax = PCMMAXCHAN;
2531 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
2533 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(dsp));
2537 dsp_sysuninit(void *p)
2539 if (dsp_ehtag == NULL)
2541 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
2545 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
2546 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
2549 dsp_unit2name(char *buf, size_t len, int unit)
2553 KASSERT(buf != NULL && len != 0,
2554 ("bogus buf=%p len=%ju", buf, (uintmax_t)len));
2556 dtype = snd_unit2d(unit);
2558 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2559 if (dtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
2561 ksnprintf(buf, len, "%s%d%s%d", dsp_cdevs[i].name,
2562 snd_unit2u(unit), dsp_cdevs[i].sep, snd_unit2c(unit));
2570 * @brief Handler for SNDCTL_AUDIOINFO.
2572 * Gathers information about the audio device specified in ai->dev. If
2573 * ai->dev == -1, then this function gathers information about the current
2574 * device. If the call comes in on a non-audio device and ai->dev == -1,
2577 * This routine is supposed to go practically straight to the hardware,
2578 * getting capabilities directly from the sound card driver, side-stepping
2579 * the intermediate channel interface.
2581 * Note, however, that the usefulness of this command is significantly
2582 * decreased when requesting info about any device other than the one serving
2583 * the request. While each snddev_channel refers to a specific device node,
2584 * the converse is *not* true. Currently, when a sound device node is opened,
2585 * the sound subsystem scans for an available audio channel (or channels, if
2586 * opened in read+write) and then assigns them to the si_drv[12] private
2587 * data fields. As a result, any information returned linking a channel to
2588 * a specific character device isn't necessarily accurate.
2591 * Calling threads must not hold any snddev_info or pcm_channel locks.
2593 * @param dev device on which the ioctl was issued
2594 * @param ai ioctl request data container
2597 * @retval EINVAL ai->dev specifies an invalid device
2599 * @todo Verify correctness of Doxygen tags. ;)
2602 dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
2604 struct pcmchan_caps *caps;
2605 struct pcm_channel *ch;
2606 struct snddev_info *d;
2608 int i, nchan, *rates, minch, maxch;
2609 char *devname, buf[CHN_NAMELEN];
2612 * If probing the device that received the ioctl, make sure it's a
2613 * DSP device. (Users may use this ioctl with /dev/mixer and
2616 if (ai->dev == -1 && i_dev->si_ops != &dsp_ops)
2622 bzero(buf, sizeof(buf));
2625 * Search for the requested audio device (channel). Start by
2626 * iterating over pcm devices.
2628 for (i = 0; pcm_devclass != NULL &&
2629 i < devclass_get_maxunit(pcm_devclass); i++) {
2630 d = devclass_get_softc(pcm_devclass, i);
2631 if (!PCM_REGISTERED(d))
2634 /* XXX Need Giant magic entry ??? */
2636 /* See the note in function docblock */
2637 PCM_UNLOCKASSERT(d);
2640 CHN_FOREACH(ch, d, channels.pcm) {
2641 CHN_UNLOCKASSERT(ch);
2643 if (ai->dev == -1) {
2644 if (DSP_REGISTERED(d, i_dev) &&
2645 (ch == PCM_RDCH(i_dev) || /* record ch */
2646 ch == PCM_WRCH(i_dev))) { /* playback ch */
2647 devname = dsp_unit2name(buf,
2648 sizeof(buf), ch->unit);
2650 } else if (ai->dev == nchan) {
2651 devname = dsp_unit2name(buf, sizeof(buf),
2654 if (devname != NULL)
2660 if (devname != NULL) {
2662 * At this point, the following synchronization stuff
2664 * - a specific PCM device is locked.
2665 * - a specific audio channel has been locked, so be
2666 * sure to unlock when exiting;
2669 caps = chn_getcaps(ch);
2672 * With all handles collected, zero out the user's
2673 * container and begin filling in its fields.
2675 bzero((void *)ai, sizeof(oss_audioinfo));
2678 strlcpy(ai->name, ch->name, sizeof(ai->name));
2680 if ((ch->flags & CHN_F_BUSY) == 0)
2683 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
2687 * @c cmd - OSSv4 docs: "Only supported under Linux at
2688 * this moment." Cop-out, I know, but I'll save
2689 * running around in the process table for later.
2690 * Is there a risk of leaking information?
2695 * These flags stolen from SNDCTL_DSP_GETCAPS handler.
2696 * Note, however, that a single channel operates in
2697 * only one direction, so PCM_CAP_DUPLEX is out.
2700 * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep
2701 * these in pcmchan::caps?
2703 ai->caps = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER |
2704 ((ch->direction == PCMDIR_PLAY) ? PCM_CAP_OUTPUT : PCM_CAP_INPUT);
2707 * Collect formats supported @b natively by the
2708 * device. Also determine min/max channels. (I.e.,
2709 * mono, stereo, or both?)
2711 * If any channel is stereo, maxch = 2;
2712 * if all channels are stereo, minch = 2, too;
2713 * if any channel is mono, minch = 1;
2714 * and if all channels are mono, maxch = 1.
2719 for (i = 0; caps->fmtlist[i]; i++) {
2720 fmts |= caps->fmtlist[i];
2721 if (AFMT_CHANNEL(caps->fmtlist[i]) > 1) {
2722 minch = (minch == 0) ? 2 : minch;
2726 maxch = (maxch == 0) ? 1 : maxch;
2730 if (ch->direction == PCMDIR_PLAY)
2731 ai->oformats = fmts;
2733 ai->iformats = fmts;
2737 * @c magic - OSSv4 docs: "Reserved for internal use
2741 * @c card_number - OSSv4 docs: "Number of the sound
2742 * card where this device belongs or -1 if this
2743 * information is not available. Applications
2744 * should normally not use this field for any
2747 ai->card_number = -1;
2749 * @todo @c song_name - depends first on
2750 * SNDCTL_[GS]ETSONG @todo @c label - depends
2751 * on SNDCTL_[GS]ETLABEL
2752 * @todo @c port_number - routing information?
2754 ai->port_number = -1;
2755 ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1;
2758 * @c real_device - OSSv4 docs: "Obsolete."
2760 ai->real_device = -1;
2761 strlcpy(ai->devnode, "/dev/", sizeof(ai->devnode));
2762 strlcat(ai->devnode, devname, sizeof(ai->devnode));
2763 ai->enabled = device_is_attached(d->dev) ? 1 : 0;
2766 * @c flags - OSSv4 docs: "Reserved for future use."
2769 * @c binding - OSSv4 docs: "Reserved for future use."
2771 * @todo @c handle - haven't decided how to generate
2772 * this yet; bus, vendor, device IDs?
2774 ai->min_rate = caps->minspeed;
2775 ai->max_rate = caps->maxspeed;
2777 ai->min_channels = minch;
2778 ai->max_channels = maxch;
2780 ai->nrates = chn_getrates(ch, &rates);
2781 if (ai->nrates > OSS_MAX_SAMPLE_RATES)
2782 ai->nrates = OSS_MAX_SAMPLE_RATES;
2784 for (i = 0; i < ai->nrates; i++)
2785 ai->rates[i] = rates[i];
2787 ai->next_play_engine = 0;
2788 ai->next_rec_engine = 0;
2795 if (devname != NULL)
2799 /* Exhausted the search -- nothing is locked, so return. */
2804 * @brief Assigns a PCM channel to a sync group.
2806 * Sync groups are used to enable audio operations on multiple devices
2807 * simultaneously. They may be used with any number of devices and may
2808 * span across applications. Devices are added to groups with
2809 * the SNDCTL_DSP_SYNCGROUP ioctl, and operations are triggered with the
2810 * SNDCTL_DSP_SYNCSTART ioctl.
2812 * If the @c id field of the @c group parameter is set to zero, then a new
2813 * sync group is created. Otherwise, wrch and rdch (if set) are added to
2814 * the group specified.
2816 * @todo As far as memory allocation, should we assume that things are
2817 * okay and allocate with M_WAITOK before acquiring channel locks,
2818 * freeing later if not?
2820 * @param wrch output channel associated w/ device (if any)
2821 * @param rdch input channel associated w/ device (if any)
2822 * @param group Sync group parameters
2825 * @retval non-zero error to be propagated upstream
2828 dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
2830 struct pcmchan_syncmember *smrd, *smwr;
2831 struct pcmchan_syncgroup *sg;
2840 * Free_unr() may sleep, so store released syncgroup IDs until after
2841 * all locks are released.
2843 sg_ids[0] = sg_ids[1] = sg_ids[2] = 0;
2848 * - Insert channel(s) into group's member list.
2849 * - Set CHN_F_NOTRIGGER on channel(s).
2850 * - Stop channel(s).
2854 * If device's channels are already mapped to a group, unmap them.
2858 sg_ids[0] = chn_syncdestroy(wrch);
2863 sg_ids[1] = chn_syncdestroy(rdch);
2867 * Verify that mode matches character device properites.
2868 * - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
2869 * - Bail if PCM_ENABLE_INPUT && rdch == NULL.
2871 if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) ||
2872 ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) {
2878 * An id of zero indicates the user wants to create a new
2881 if (group->id == 0) {
2882 sg = (struct pcmchan_syncgroup *)kmalloc(sizeof(*sg), M_DEVBUF, M_WAITOK | M_ZERO);
2884 SLIST_INIT(&sg->members);
2885 sg->id = alloc_unr(pcmsg_unrhdr);
2888 SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link);
2892 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2893 if (sg->id == group->id)
2900 /* Couldn't create or find a syncgroup. Fail. */
2905 * Allocate a syncmember, assign it and a channel together, and
2906 * insert into syncgroup.
2908 if (group->mode & PCM_ENABLE_INPUT) {
2909 smrd = (struct pcmchan_syncmember *)kmalloc(sizeof(*smrd), M_DEVBUF, M_WAITOK | M_ZERO);
2915 SLIST_INSERT_HEAD(&sg->members, smrd, link);
2920 rdch->flags |= CHN_F_NOTRIGGER;
2924 if (group->mode & PCM_ENABLE_OUTPUT) {
2925 smwr = (struct pcmchan_syncmember *)kmalloc(sizeof(*smwr), M_DEVBUF, M_WAITOK | M_ZERO);
2931 SLIST_INSERT_HEAD(&sg->members, smwr, link);
2936 wrch->flags |= CHN_F_NOTRIGGER;
2944 kfree(smrd, M_DEVBUF);
2945 if ((sg != NULL) && SLIST_EMPTY(&sg->members)) {
2947 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2948 kfree(sg, M_DEVBUF);
2965 free_unr(pcmsg_unrhdr, sg_ids[0]);
2967 free_unr(pcmsg_unrhdr, sg_ids[1]);
2969 free_unr(pcmsg_unrhdr, sg_ids[2]);
2975 * @brief Launch a sync group into action
2977 * Sync groups are established via SNDCTL_DSP_SYNCGROUP. This function
2978 * iterates over all members, triggering them along the way.
2980 * @note Caller must not hold any channel locks.
2982 * @param sg_id sync group identifier
2985 * @retval non-zero error worthy of propagating upstream to user
2988 dsp_oss_syncstart(int sg_id)
2990 struct pcmchan_syncmember *sm, *sm_tmp;
2991 struct pcmchan_syncgroup *sg;
2992 struct pcm_channel *c;
2995 /* Get the synclists lock */
3002 /* Search for syncgroup by ID */
3003 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
3004 if (sg->id == sg_id)
3008 /* Return EINVAL if not found */
3014 /* Any removals resulting in an empty group should've handled this */
3015 KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup"));
3018 * Attempt to lock all member channels - if any are already
3019 * locked, unlock those acquired, sleep for a bit, and try
3022 SLIST_FOREACH(sm, &sg->members, link) {
3023 if (CHN_TRYLOCK(sm->ch) == 0) {
3024 int timo = hz * 5/1000;
3028 /* Release all locked channels so far, retry */
3029 SLIST_FOREACH(sm_tmp, &sg->members, link) {
3030 /* sm is the member already locked */
3033 CHN_UNLOCK(sm_tmp->ch);
3036 /** @todo Is PRIBIO correct/ */
3037 ret = lksleep(sm, &snd_pcm_syncgroups_mtx,
3038 PCATCH, "pcmsg", timo);
3039 if (ret == EINTR || ret == ERESTART)
3043 ret = 0; /* Assumes ret == EAGAIN... */
3046 } while (needlocks && ret == 0);
3048 /* Proceed only if no errors encountered. */
3050 /* Launch channels */
3051 while ((sm = SLIST_FIRST(&sg->members)) != NULL) {
3052 SLIST_REMOVE_HEAD(&sg->members, link);
3057 c->flags &= ~CHN_F_NOTRIGGER;
3060 kfree(sm, M_DEVBUF);
3063 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
3064 kfree(sg, M_DEVBUF);
3070 * Free_unr() may sleep, so be sure to give up the syncgroup lock
3074 free_unr(pcmsg_unrhdr, sg_id);
3080 * @brief Handler for SNDCTL_DSP_POLICY
3082 * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment
3083 * size and count like with SNDCTL_DSP_SETFRAGMENT. Instead of the user
3084 * specifying those two parameters, s/he simply selects a number from 0..10
3085 * which corresponds to a buffer size. Smaller numbers request smaller
3086 * buffers with lower latencies (at greater overhead from more frequent
3087 * interrupts), while greater numbers behave in the opposite manner.
3089 * The 4Front spec states that a value of 5 should be the default. However,
3090 * this implementation deviates slightly by using a linear scale without
3091 * consulting drivers. I.e., even though drivers may have different default
3092 * buffer sizes, a policy argument of 5 will have the same result across
3095 * See http://manuals.opensound.com/developer/SNDCTL_DSP_POLICY.html for
3098 * @todo When SNDCTL_DSP_COOKEDMODE is supported, it'll be necessary to
3099 * work with hardware drivers directly.
3101 * @note PCM channel arguments must not be locked by caller.
3103 * @param wrch Pointer to opened playback channel (optional; may be NULL)
3104 * @param rdch " recording channel (optional; may be NULL)
3105 * @param policy Integer from [0:10]
3107 * @retval 0 constant (for now)
3110 dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
3114 if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX)
3117 /* Default: success */
3122 ret = chn_setlatency(rdch, policy);
3126 if (wrch && ret == 0) {
3128 ret = chn_setlatency(wrch, policy);
3139 * @brief Enable or disable "cooked" mode
3141 * This is a handler for @c SNDCTL_DSP_COOKEDMODE. When in cooked mode, which
3142 * is the default, the sound system handles rate and format conversions
3143 * automatically (ex: user writing 11025Hz/8 bit/unsigned but card only
3144 * operates with 44100Hz/16bit/signed samples).
3146 * Disabling cooked mode is intended for applications wanting to mmap()
3147 * a sound card's buffer space directly, bypassing the FreeBSD 2-stage
3148 * feeder architecture, presumably to gain as much control over audio
3149 * hardware as possible.
3151 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_COOKEDMODE.html
3154 * @param wrch playback channel (optional; may be NULL)
3155 * @param rdch recording channel (optional; may be NULL)
3156 * @param enabled 0 = raw mode, 1 = cooked mode
3158 * @retval EINVAL Operation not yet supported.
3161 dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
3165 * XXX I just don't get it. Why don't they call it
3166 * "BITPERFECT" ~ SNDCTL_DSP_BITPERFECT !?!?.
3167 * This is just plain so confusing, incoherent,
3168 * <insert any non-printable characters here>.
3170 if (!(enabled == 1 || enabled == 0))
3174 * I won't give in. I'm inverting its logic here and now.
3175 * Brag all you want, but "BITPERFECT" should be the better
3178 enabled ^= 0x00000001;
3182 wrch->flags &= ~CHN_F_BITPERFECT;
3183 wrch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3189 rdch->flags &= ~CHN_F_BITPERFECT;
3190 rdch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3198 * @brief Retrieve channel interleaving order
3200 * This is the handler for @c SNDCTL_DSP_GET_CHNORDER.
3202 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html
3205 * @note As the ioctl definition is still under construction, FreeBSD
3206 * does not currently support SNDCTL_DSP_GET_CHNORDER.
3208 * @param wrch playback channel (optional; may be NULL)
3209 * @param rdch recording channel (optional; may be NULL)
3210 * @param map channel map (result will be stored there)
3212 * @retval EINVAL Operation not yet supported.
3215 dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3217 struct pcm_channel *ch;
3220 ch = (wrch != NULL) ? wrch : rdch;
3223 ret = chn_oss_getorder(ch, map);
3232 * @brief Specify channel interleaving order
3234 * This is the handler for @c SNDCTL_DSP_SET_CHNORDER.
3236 * @note As the ioctl definition is still under construction, FreeBSD
3237 * does not currently support @c SNDCTL_DSP_SET_CHNORDER.
3239 * @param wrch playback channel (optional; may be NULL)
3240 * @param rdch recording channel (optional; may be NULL)
3241 * @param map channel map
3243 * @retval EINVAL Operation not yet supported.
3246 dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3254 ret = chn_oss_setorder(wrch, map);
3258 if (ret == 0 && rdch != NULL) {
3260 ret = chn_oss_setorder(rdch, map);
3268 dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch,
3271 struct pcm_channel *ch;
3276 ch = (wrch != NULL) ? wrch : rdch;
3280 ret = chn_oss_getmask(ch, &chnmask);
3291 #ifdef OSSV4_EXPERIMENT
3293 * @brief Retrieve an audio device's label
3295 * This is a handler for the @c SNDCTL_GETLABEL ioctl.
3297 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3300 * From Hannu@4Front: "For example ossxmix (just like some HW mixer
3301 * consoles) can show variable "labels" for certain controls. By default
3302 * the application name (say quake) is shown as the label but
3303 * applications may change the labels themselves."
3305 * @note As the ioctl definition is still under construction, FreeBSD
3306 * does not currently support @c SNDCTL_GETLABEL.
3308 * @param wrch playback channel (optional; may be NULL)
3309 * @param rdch recording channel (optional; may be NULL)
3310 * @param label label gets copied here
3312 * @retval EINVAL Operation not yet supported.
3315 dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3321 * @brief Specify an audio device's label
3323 * This is a handler for the @c SNDCTL_SETLABEL ioctl. Please see the
3324 * comments for @c dsp_oss_getlabel immediately above.
3326 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3329 * @note As the ioctl definition is still under construction, FreeBSD
3330 * does not currently support SNDCTL_SETLABEL.
3332 * @param wrch playback channel (optional; may be NULL)
3333 * @param rdch recording channel (optional; may be NULL)
3334 * @param label label gets copied from here
3336 * @retval EINVAL Operation not yet supported.
3339 dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3345 * @brief Retrieve name of currently played song
3347 * This is a handler for the @c SNDCTL_GETSONG ioctl. Audio players could
3348 * tell the system the name of the currently playing song, which would be
3349 * visible in @c /dev/sndstat.
3351 * See @c http://manuals.opensound.com/developer/SNDCTL_GETSONG.html
3354 * @note As the ioctl definition is still under construction, FreeBSD
3355 * does not currently support SNDCTL_GETSONG.
3357 * @param wrch playback channel (optional; may be NULL)
3358 * @param rdch recording channel (optional; may be NULL)
3359 * @param song song name gets copied here
3361 * @retval EINVAL Operation not yet supported.
3364 dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3370 * @brief Retrieve name of currently played song
3372 * This is a handler for the @c SNDCTL_SETSONG ioctl. Audio players could
3373 * tell the system the name of the currently playing song, which would be
3374 * visible in @c /dev/sndstat.
3376 * See @c http://manuals.opensound.com/developer/SNDCTL_SETSONG.html
3379 * @note As the ioctl definition is still under construction, FreeBSD
3380 * does not currently support SNDCTL_SETSONG.
3382 * @param wrch playback channel (optional; may be NULL)
3383 * @param rdch recording channel (optional; may be NULL)
3384 * @param song song name gets copied from here
3386 * @retval EINVAL Operation not yet supported.
3389 dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3395 * @brief Rename a device
3397 * This is a handler for the @c SNDCTL_SETNAME ioctl.
3399 * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for
3402 * From Hannu@4Front: "This call is used to change the device name
3403 * reported in /dev/sndstat and ossinfo. So instead of using some generic
3404 * 'OSS loopback audio (MIDI) driver' the device may be given a meaningfull
3405 * name depending on the current context (for example 'OSS virtual wave table
3406 * synth' or 'VoIP link to London')."
3408 * @note As the ioctl definition is still under construction, FreeBSD
3409 * does not currently support SNDCTL_SETNAME.
3411 * @param wrch playback channel (optional; may be NULL)
3412 * @param rdch recording channel (optional; may be NULL)
3413 * @param name new device name gets copied from here
3415 * @retval EINVAL Operation not yet supported.
3418 dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
3422 #endif /* !OSSV4_EXPERIMENT */