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/device.h>
36 #include <sys/eventhandler.h>
38 #include <sys/sysent.h>
41 #include <vm/vm_object.h>
42 #include <vm/vm_page.h>
43 #include <vm/vm_pager.h>
45 SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/dsp.c 274035 2014-11-03 11:11:45Z bapt $");
47 static int dsp_mmap_allow_prot_exec = 0;
48 SYSCTL_INT(_hw_snd, OID_AUTO, compat_linux_mmap, CTLFLAG_RW,
49 &dsp_mmap_allow_prot_exec, 0,
50 "linux mmap compatibility (-1=force disable 0=auto 1=force enable)");
53 struct pcm_channel *rdch, *wrch;
54 struct pcm_channel *volch;
56 TAILQ_ENTRY(dsp_cdevinfo) link;
59 #define PCM_RDCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->rdch)
60 #define PCM_WRCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->wrch)
61 #define PCM_VOLCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->volch)
62 #define PCM_SIMPLEX(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->simplex)
64 #define DSP_CDEVINFO_CACHESIZE 8
66 #define DSP_REGISTERED(x, y) (PCM_REGISTERED(x) && \
67 (y) != NULL && (y)->si_drv1 != NULL)
71 static d_open_t dsp_open;
72 static d_close_t dsp_close;
73 static d_read_t dsp_read;
74 static d_write_t dsp_write;
75 static d_ioctl_t dsp_ioctl;
76 static d_kqfilter_t dsp_kqfilter;
77 static d_mmap_t dsp_mmap;
78 static d_mmap_single_t dsp_mmap_single;
80 static void dsp_filter_detach(struct knote *);
81 static int dsp_filter_read(struct knote *, long);
82 static int dsp_filter_write(struct knote *, long);
84 struct dev_ops dsp_ops = {
90 .d_kqfilter = dsp_kqfilter,
92 .d_mmap_single = dsp_mmap_single,
95 static eventhandler_tag dsp_ehtag = NULL;
96 static int dsp_umax = -1;
97 static int dsp_cmax = -1;
99 static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group);
100 static int dsp_oss_syncstart(int sg_id);
101 static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy);
102 static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled);
103 static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
104 static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
105 static int dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch, int *mask);
106 #ifdef OSSV4_EXPERIMENT
107 static int dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
108 static int dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
109 static int dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
110 static int dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
111 static int dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name);
114 static struct snddev_info *
115 dsp_get_info(struct cdev *dev)
117 return (devclass_get_softc(pcm_devclass, PCMUNIT(dev)));
121 dsp_get_flags(struct cdev *dev)
125 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
127 return ((bdev != NULL) ? pcm_getflags(bdev) : 0xffffffff);
131 dsp_set_flags(struct cdev *dev, uint32_t flags)
135 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
138 pcm_setflags(bdev, flags);
142 * return the channels associated with an open device instance.
143 * lock channels specified.
146 getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch,
149 struct snddev_info *d;
150 struct pcm_channel *ch;
153 if (PCM_SIMPLEX(dev) != 0) {
154 d = dsp_get_info(dev);
155 if (!PCM_REGISTERED(d))
161 * Note: order is important -
162 * pcm flags -> prio query flags -> wild guess
165 flags = dsp_get_flags(dev);
166 if (flags & SD_F_PRIO_WR) {
168 PCM_RDCH(dev) = NULL;
169 } else if (flags & SD_F_PRIO_RD) {
171 PCM_WRCH(dev) = NULL;
172 } else if (prio & SD_F_PRIO_WR) {
174 PCM_RDCH(dev) = NULL;
175 flags |= SD_F_PRIO_WR;
176 } else if (prio & SD_F_PRIO_RD) {
178 PCM_WRCH(dev) = NULL;
179 flags |= SD_F_PRIO_RD;
180 } else if (PCM_WRCH(dev) != NULL) {
182 PCM_RDCH(dev) = NULL;
183 flags |= SD_F_PRIO_WR;
184 } else if (PCM_RDCH(dev) != NULL) {
186 PCM_WRCH(dev) = NULL;
187 flags |= SD_F_PRIO_RD;
189 PCM_SIMPLEX(dev) = 0;
190 dsp_set_flags(dev, flags);
200 *rdch = PCM_RDCH(dev);
201 *wrch = PCM_WRCH(dev);
203 if (*rdch != NULL && (prio & SD_F_PRIO_RD))
205 if (*wrch != NULL && (prio & SD_F_PRIO_WR))
211 /* unlock specified channels */
213 relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch,
216 if (wrch != NULL && (prio & SD_F_PRIO_WR))
218 if (rdch != NULL && (prio & SD_F_PRIO_RD))
223 dsp_cdevinfo_alloc(struct cdev *dev,
224 struct pcm_channel *rdch, struct pcm_channel *wrch,
225 struct pcm_channel *volch)
227 struct snddev_info *d;
228 struct dsp_cdevinfo *cdi;
231 d = dsp_get_info(dev);
233 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 == NULL &&
234 ((rdch == NULL && wrch == NULL) || rdch != wrch),
235 ("bogus %s(), what are you trying to accomplish here?", __func__));
239 simplex = (dsp_get_flags(dev) & SD_F_SIMPLEX) ? 1 : 0;
242 * Scan for free instance entry and put it into the end of list.
243 * Create new one if necessary.
245 TAILQ_FOREACH(cdi, &d->dsp_cdevinfo_pool, link) {
251 cdi->simplex = simplex;
253 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
254 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
259 cdi = kmalloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
264 cdi->simplex = simplex;
266 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
271 dsp_cdevinfo_free(struct cdev *dev)
273 struct snddev_info *d;
274 struct dsp_cdevinfo *cdi, *tmp;
278 d = dsp_get_info(dev);
280 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 != NULL &&
281 PCM_RDCH(dev) == NULL && PCM_WRCH(dev) == NULL &&
282 PCM_VOLCH(dev) == NULL,
283 ("bogus %s(), what are you trying to accomplish here?", __func__));
296 * Once it is free, move it back to the beginning of list for
297 * faster new entry allocation.
299 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
300 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
303 * Scan the list, cache free entries up to DSP_CDEVINFO_CACHESIZE.
304 * Reset simplex flags.
306 flags = dsp_get_flags(dev) & ~SD_F_PRIO_SET;
307 i = DSP_CDEVINFO_CACHESIZE;
308 TAILQ_FOREACH_MUTABLE(cdi, &d->dsp_cdevinfo_pool, link, tmp) {
309 if (cdi->busy != 0) {
310 if (cdi->simplex == 0) {
311 if (cdi->rdch != NULL)
312 flags |= SD_F_PRIO_RD;
313 if (cdi->wrch != NULL)
314 flags |= SD_F_PRIO_WR;
318 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
319 kfree(cdi, M_DEVBUF);
324 dsp_set_flags(dev, flags);
328 dsp_cdevinfo_init(struct snddev_info *d)
330 struct dsp_cdevinfo *cdi;
333 KASSERT(d != NULL, ("NULL snddev_info"));
337 TAILQ_INIT(&d->dsp_cdevinfo_pool);
338 for (i = 0; i < DSP_CDEVINFO_CACHESIZE; i++) {
339 cdi = kmalloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
340 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
345 dsp_cdevinfo_flush(struct snddev_info *d)
347 struct dsp_cdevinfo *cdi, *tmp;
349 KASSERT(d != NULL, ("NULL snddev_info"));
353 cdi = TAILQ_FIRST(&d->dsp_cdevinfo_pool);
354 while (cdi != NULL) {
355 tmp = TAILQ_NEXT(cdi, link);
356 kfree(cdi, M_DEVBUF);
359 TAILQ_INIT(&d->dsp_cdevinfo_pool);
362 /* duplex / simplex cdev type */
364 DSP_CDEV_TYPE_RDONLY, /* simplex read-only (record) */
365 DSP_CDEV_TYPE_WRONLY, /* simplex write-only (play) */
366 DSP_CDEV_TYPE_RDWR /* duplex read, write, or both */
370 DSP_CDEV_VOLCTL_NONE,
371 DSP_CDEV_VOLCTL_READ,
372 DSP_CDEV_VOLCTL_WRITE
375 #define DSP_F_VALID(x) ((x) & (FREAD | FWRITE))
376 #define DSP_F_DUPLEX(x) (((x) & (FREAD | FWRITE)) == (FREAD | FWRITE))
377 #define DSP_F_SIMPLEX(x) (!DSP_F_DUPLEX(x))
378 #define DSP_F_READ(x) ((x) & FREAD)
379 #define DSP_F_WRITE(x) ((x) & FWRITE)
381 static const struct {
393 { SND_DEV_DSP, "dsp", ".", NULL, 0, 0, 0, 0,
394 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
395 DSP_CDEV_TYPE_RDWR },
396 { SND_DEV_AUDIO, "audio", ".", NULL, 0, 0, 0, 0,
397 SND_FORMAT(AFMT_MU_LAW, 1, 0), DSP_DEFAULT_SPEED,
398 DSP_CDEV_TYPE_RDWR },
399 { SND_DEV_DSP16, "dspW", ".", NULL, 0, 0, 0, 0,
400 SND_FORMAT(AFMT_S16_LE, 1, 0), DSP_DEFAULT_SPEED,
401 DSP_CDEV_TYPE_RDWR },
402 { SND_DEV_DSPHW_PLAY, "dsp", ".p", NULL, 1, 1, SND_MAXHWCHAN, 1,
403 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
404 { SND_DEV_DSPHW_VPLAY, "dsp", ".vp", NULL, 1, 1, SND_MAXVCHANS, 1,
405 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
406 { SND_DEV_DSPHW_REC, "dsp", ".r", NULL, 1, 1, SND_MAXHWCHAN, 1,
407 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
408 { SND_DEV_DSPHW_VREC, "dsp", ".vr", NULL, 1, 1, SND_MAXVCHANS, 1,
409 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
410 { SND_DEV_DSPHW_CD, "dspcd", ".", NULL, 0, 0, 0, 0,
411 SND_FORMAT(AFMT_S16_LE, 2, 0), 44100, DSP_CDEV_TYPE_RDWR },
412 /* Low priority, OSSv4 aliases. */
413 { SND_DEV_DSP, "dsp_ac3", ".", "dsp", 0, 0, 0, 0,
414 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
415 DSP_CDEV_TYPE_RDWR },
416 { SND_DEV_DSP, "dsp_mmap", ".", "dsp", 0, 0, 0, 0,
417 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
418 DSP_CDEV_TYPE_RDWR },
419 { SND_DEV_DSP, "dsp_multich", ".", "dsp", 0, 0, 0, 0,
420 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
421 DSP_CDEV_TYPE_RDWR },
422 { SND_DEV_DSP, "dsp_spdifout", ".", "dsp", 0, 0, 0, 0,
423 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
424 DSP_CDEV_TYPE_RDWR },
425 { SND_DEV_DSP, "dsp_spdifin", ".", "dsp", 0, 0, 0, 0,
426 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
427 DSP_CDEV_TYPE_RDWR },
430 #define DSP_FIXUP_ERROR() do { \
431 prio = dsp_get_flags(i_dev); \
432 if (!DSP_F_VALID(flags)) \
434 if (!DSP_F_DUPLEX(flags) && \
435 ((DSP_F_READ(flags) && d->reccount == 0) || \
436 (DSP_F_WRITE(flags) && d->playcount == 0))) \
438 else if (!DSP_F_DUPLEX(flags) && (prio & SD_F_SIMPLEX) && \
439 ((DSP_F_READ(flags) && (prio & SD_F_PRIO_WR)) || \
440 (DSP_F_WRITE(flags) && (prio & SD_F_PRIO_RD)))) \
442 else if (DSP_REGISTERED(d, i_dev)) \
447 dsp_open(struct dev_open_args *ap)
449 struct cdev *i_dev = ap->a_head.a_dev;
450 int flags = ap->a_oflags;
451 struct pcm_channel *rdch, *wrch;
452 struct snddev_info *d;
453 uint32_t fmt, spd, prio, volctl;
454 int i, error, rderror, wrerror, devtype, wdevunit, rdevunit;
456 /* Kind of impossible.. */
460 d = dsp_get_info(i_dev);
461 if (!PCM_REGISTERED(d))
466 /* Lock snddev so nobody else can monkey with it. */
471 * Try to acquire cloned device before someone else pick it.
472 * ENODEV means this is not a cloned droids.
474 error = snd_clone_acquire(i_dev);
475 if (!(error == 0 || error == ENODEV)) {
486 (void)snd_clone_release(i_dev);
493 * That is just enough. Acquire and unlock pcm lock so
494 * the other will just have to wait until we finish doing
500 devtype = PCMDEV(i_dev);
505 volctl = DSP_CDEV_VOLCTL_NONE;
507 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
508 if (devtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
511 * Volume control only valid for DSPHW devices,
512 * and it must be opened in opposite direction be it
513 * simplex or duplex. Anything else will be handled
516 if (dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY) {
517 if (dsp_cdevs[i].volctl != 0 &&
519 volctl = DSP_CDEV_VOLCTL_WRITE;
523 if (DSP_F_READ(flags)) {
524 (void)snd_clone_release(i_dev);
525 PCM_RELEASE_QUICK(d);
529 wdevunit = dev2unit(i_dev);
530 } else if (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY) {
531 if (dsp_cdevs[i].volctl != 0 &&
532 DSP_F_WRITE(flags)) {
533 volctl = DSP_CDEV_VOLCTL_READ;
537 if (DSP_F_WRITE(flags)) {
538 (void)snd_clone_release(i_dev);
539 PCM_RELEASE_QUICK(d);
543 rdevunit = dev2unit(i_dev);
545 fmt = dsp_cdevs[i].fmt;
546 spd = dsp_cdevs[i].spd;
556 * if we get here, the open request is valid- either:
557 * * we were previously not open
558 * * we were open for play xor record and the opener wants
559 * the non-open direction
561 if (DSP_F_READ(flags)) {
563 rderror = pcm_chnalloc(d, &rdch, PCMDIR_REC,
564 curproc->p_pid, curproc->p_comm, rdevunit);
566 if (rderror == 0 && chn_reset(rdch, fmt, spd) != 0)
569 if (volctl == DSP_CDEV_VOLCTL_READ)
574 pcm_chnrelease(rdch);
575 if (!DSP_F_DUPLEX(flags)) {
576 (void)snd_clone_release(i_dev);
577 PCM_RELEASE_QUICK(d);
582 } else if (volctl == DSP_CDEV_VOLCTL_READ) {
585 pcm_chnrelease(rdch);
588 if (flags & O_NONBLOCK)
589 rdch->flags |= CHN_F_NBIO;
591 rdch->flags |= CHN_F_EXCLUSIVE;
593 if (volctl == DSP_CDEV_VOLCTL_NONE)
594 chn_vpc_reset(rdch, SND_VOL_C_PCM, 0);
599 if (DSP_F_WRITE(flags)) {
601 wrerror = pcm_chnalloc(d, &wrch, PCMDIR_PLAY,
602 curproc->p_pid, curproc->p_comm, wdevunit);
604 if (wrerror == 0 && chn_reset(wrch, fmt, spd) != 0)
607 if (volctl == DSP_CDEV_VOLCTL_WRITE)
612 pcm_chnrelease(wrch);
613 if (!DSP_F_DUPLEX(flags)) {
616 * Lock, deref and release previously
617 * created record channel
620 pcm_chnref(rdch, -1);
621 pcm_chnrelease(rdch);
623 (void)snd_clone_release(i_dev);
624 PCM_RELEASE_QUICK(d);
629 } else if (volctl == DSP_CDEV_VOLCTL_WRITE) {
632 pcm_chnrelease(wrch);
635 if (flags & O_NONBLOCK)
636 wrch->flags |= CHN_F_NBIO;
638 wrch->flags |= CHN_F_EXCLUSIVE;
640 if (volctl == DSP_CDEV_VOLCTL_NONE)
641 chn_vpc_reset(wrch, SND_VOL_C_PCM, 0);
650 * We're done. Allocate channels information for this cdev.
653 case DSP_CDEV_VOLCTL_READ:
654 KASSERT(wrch == NULL, ("wrch=%p not null!", wrch));
655 dsp_cdevinfo_alloc(i_dev, NULL, NULL, rdch);
657 case DSP_CDEV_VOLCTL_WRITE:
658 KASSERT(rdch == NULL, ("rdch=%p not null!", rdch));
659 dsp_cdevinfo_alloc(i_dev, NULL, NULL, wrch);
661 case DSP_CDEV_VOLCTL_NONE:
663 if (wrch == NULL && rdch == NULL) {
664 (void)snd_clone_release(i_dev);
674 dsp_cdevinfo_alloc(i_dev, rdch, wrch, NULL);
676 CHN_INSERT_HEAD(d, rdch, channels.pcm.opened);
678 CHN_INSERT_HEAD(d, wrch, channels.pcm.opened);
683 * Increase clone refcount for its automatic garbage collector.
685 (void)snd_clone_ref(i_dev);
696 dsp_close(struct dev_close_args *ap)
698 struct cdev *i_dev = ap->a_head.a_dev;
699 struct pcm_channel *rdch, *wrch, *volch;
700 struct snddev_info *d;
701 int sg_ids, rdref, wdref;
703 d = dsp_get_info(i_dev);
704 if (!DSP_REGISTERED(d, i_dev))
713 rdch = PCM_RDCH(i_dev);
714 wrch = PCM_WRCH(i_dev);
715 volch = PCM_VOLCH(i_dev);
717 PCM_RDCH(i_dev) = NULL;
718 PCM_WRCH(i_dev) = NULL;
719 PCM_VOLCH(i_dev) = NULL;
727 else if (volch == wrch)
731 pcm_chnref(volch, -1);
737 CHN_REMOVE(d, rdch, channels.pcm.opened);
739 CHN_REMOVE(d, wrch, channels.pcm.opened);
741 if (rdch != NULL || wrch != NULL) {
745 * The channel itself need not be locked because:
746 * a) Adding a channel to a syncgroup happens only
747 * in dsp_ioctl(), which cannot run concurrently
749 * b) The syncmember pointer (sm) is protected by
750 * the global syncgroup list lock.
751 * c) A channel can't just disappear, invalidating
752 * pointers, unless it's closed/dereferenced
756 sg_ids = chn_syncdestroy(rdch);
759 free_unr(pcmsg_unrhdr, sg_ids);
762 pcm_chnref(rdch, rdref);
763 chn_abort(rdch); /* won't sleep */
764 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
765 CHN_F_DEAD | CHN_F_EXCLUSIVE);
766 chn_reset(rdch, 0, 0);
767 pcm_chnrelease(rdch);
771 * Please see block above.
774 sg_ids = chn_syncdestroy(wrch);
777 free_unr(pcmsg_unrhdr, sg_ids);
780 pcm_chnref(wrch, wdref);
781 chn_flush(wrch); /* may sleep */
782 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
783 CHN_F_DEAD | CHN_F_EXCLUSIVE);
784 chn_reset(wrch, 0, 0);
785 pcm_chnrelease(wrch);
790 dsp_cdevinfo_free(i_dev);
792 * Release clone busy state and unref it so the automatic
793 * garbage collector will get the hint and do the remaining
796 (void)snd_clone_release(i_dev);
799 * destroy_dev() might sleep, so release pcm lock
800 * here and rely on pcm cv serialization.
803 (void)snd_clone_unref(i_dev);
815 dsp_io_ops(struct cdev *i_dev, struct uio *buf)
817 struct snddev_info *d;
818 struct pcm_channel **ch, *rdch, *wrch;
819 int (*chn_io)(struct pcm_channel *, struct uio *);
823 KASSERT(i_dev != NULL && buf != NULL &&
824 (buf->uio_rw == UIO_READ || buf->uio_rw == UIO_WRITE),
825 ("%s(): io train wreck!", __func__));
827 d = dsp_get_info(i_dev);
828 if (!DSP_REGISTERED(d, i_dev))
833 switch (buf->uio_rw) {
845 panic("invalid/corrupted uio direction: %d", buf->uio_rw);
851 runpid = buf->uio_td->td_proc->p_pid;
853 getchns(i_dev, &rdch, &wrch, prio);
855 if (*ch == NULL || !((*ch)->flags & CHN_F_BUSY)) {
860 if (((*ch)->flags & (CHN_F_MMAP | CHN_F_DEAD)) ||
861 (((*ch)->flags & CHN_F_RUNNING) && (*ch)->pid != runpid)) {
862 relchns(i_dev, rdch, wrch, prio);
865 } else if (!((*ch)->flags & CHN_F_RUNNING)) {
866 (*ch)->flags |= CHN_F_RUNNING;
871 * chn_read/write must give up channel lock in order to copy bytes
872 * from/to userland, so up the "in progress" counter to make sure
873 * someone else doesn't come along and muss up the buffer.
876 ret = chn_io(*ch, buf);
879 CHN_BROADCAST(&(*ch)->cv);
881 relchns(i_dev, rdch, wrch, prio);
889 dsp_read(struct dev_read_args *ap)
891 struct cdev *i_dev = ap->a_head.a_dev;
892 struct uio *buf = ap->a_uio;
894 return (dsp_io_ops(i_dev, buf));
898 dsp_write(struct dev_write_args *ap)
900 struct cdev *i_dev = ap->a_head.a_dev;
901 struct uio *buf = ap->a_uio;
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;
969 d = dsp_get_info(dev);
970 if (!PCM_REGISTERED(d) || !(dsp_get_flags(dev) & SD_F_VPC))
977 rdch = PCM_RDCH(dev);
978 wrch = PCM_WRCH(dev);
980 /* No specific channel, look into cache */
982 volch = PCM_VOLCH(dev);
986 if (j == SOUND_MIXER_RECLEV && rdch != NULL)
988 else if (j == SOUND_MIXER_PCM && wrch != NULL)
992 devtype = PCMDEV(dev);
994 /* Look super harder */
996 (devtype == SND_DEV_DSPHW_PLAY || devtype == SND_DEV_DSPHW_VPLAY ||
997 devtype == SND_DEV_DSPHW_REC || devtype == SND_DEV_DSPHW_VREC)) {
998 ret = dsp_get_volume_channel(dev, &volch);
1005 /* Final validation */
1006 if (volch != NULL) {
1008 if (!(volch->feederflags & (1 << FEEDER_VOLUME))) {
1012 if (volch->direction == PCMDIR_PLAY)
1020 if (volch != NULL &&
1021 ((j == SOUND_MIXER_PCM && volch->direction == PCMDIR_PLAY) ||
1022 (j == SOUND_MIXER_RECLEV && volch->direction == PCMDIR_REC))) {
1023 if ((cmd & ~0xff) == MIXER_WRITE(0)) {
1024 int left, right, center;
1026 left = *(int *)arg & 0x7f;
1027 right = ((*(int *)arg) >> 8) & 0x7f;
1028 center = (left + right) >> 1;
1029 chn_setvolume_multi(volch, SND_VOL_C_PCM, left, right,
1031 } else if ((cmd & ~0xff) == MIXER_READ(0)) {
1032 *(int *)arg = CHN_GETVOLUME(volch,
1033 SND_VOL_C_PCM, SND_CHN_T_FL);
1034 *(int *)arg |= CHN_GETVOLUME(volch,
1035 SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
1038 } else if (rdch != NULL || wrch != NULL) {
1040 case SOUND_MIXER_DEVMASK:
1041 case SOUND_MIXER_CAPS:
1042 case SOUND_MIXER_STEREODEVS:
1043 if ((cmd & ~0xff) == MIXER_READ(0)) {
1046 *(int *)arg |= SOUND_MASK_RECLEV;
1048 *(int *)arg |= SOUND_MASK_PCM;
1052 case SOUND_MIXER_RECMASK:
1053 case SOUND_MIXER_RECSRC:
1054 if ((cmd & ~0xff) == MIXER_READ(0))
1070 dsp_ioctl(struct dev_ioctl_args *ap)
1072 struct cdev *i_dev = ap->a_head.a_dev;
1073 u_long cmd = ap->a_cmd;
1074 caddr_t arg = ap->a_data;
1075 struct pcm_channel *chn, *rdch, *wrch;
1076 struct snddev_info *d;
1078 int *arg_i, ret, tmp;
1080 d = dsp_get_info(i_dev);
1081 if (!DSP_REGISTERED(d, i_dev))
1091 if (IOCGROUP(cmd) == 'M') {
1092 if (cmd == OSS_GETVERSION) {
1093 *arg_i = SOUND_VERSION;
1097 ret = dsp_ioctl_channel(i_dev, PCM_VOLCH(i_dev), cmd, arg);
1103 if (d->mixer_dev != NULL) {
1104 PCM_ACQUIRE_QUICK(d);
1105 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1,
1107 PCM_RELEASE_QUICK(d);
1117 * Certain ioctls may be made on any type of device (audio, mixer,
1118 * and MIDI). Handle those special cases here.
1120 if (IOCGROUP(cmd) == 'X') {
1121 PCM_ACQUIRE_QUICK(d);
1123 case SNDCTL_SYSINFO:
1124 sound_oss_sysinfo((oss_sysinfo *)arg);
1126 case SNDCTL_CARDINFO:
1127 ret = sound_oss_card_info((oss_card_info *)arg);
1129 case SNDCTL_AUDIOINFO:
1130 case SNDCTL_AUDIOINFO_EX:
1131 case SNDCTL_ENGINEINFO:
1132 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
1134 case SNDCTL_MIXERINFO:
1135 ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
1140 PCM_RELEASE_QUICK(d);
1145 getchns(i_dev, &rdch, &wrch, 0);
1147 if (wrch != NULL && (wrch->flags & CHN_F_DEAD))
1149 if (rdch != NULL && (rdch->flags & CHN_F_DEAD))
1152 if (wrch == NULL && rdch == NULL) {
1160 * we start with the new ioctl interface.
1162 case AIONWRITE: /* how many bytes can write ? */
1166 if (wrch && wrch->bufhard.dl)
1167 while (chn_wrfeed(wrch) == 0);
1169 *arg_i = sndbuf_getfree(wrch->bufsoft);
1177 case AIOSSIZE: /* set the current blocksize */
1179 struct snd_size *p = (struct snd_size *)arg;
1183 PCM_ACQUIRE_QUICK(d);
1186 chn_setblocksize(wrch, 2, p->play_size);
1187 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1192 chn_setblocksize(rdch, 2, p->rec_size);
1193 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1196 PCM_RELEASE_QUICK(d);
1199 case AIOGSIZE: /* get the current blocksize */
1201 struct snd_size *p = (struct snd_size *)arg;
1205 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1210 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1219 snd_chan_param *p = (snd_chan_param *)arg;
1221 if (cmd == AIOSFMT &&
1222 ((p->play_format != 0 && p->play_rate == 0) ||
1223 (p->rec_format != 0 && p->rec_rate == 0))) {
1227 PCM_ACQUIRE_QUICK(d);
1230 if (cmd == AIOSFMT && p->play_format != 0) {
1232 SND_FORMAT(p->play_format,
1233 AFMT_CHANNEL(wrch->format),
1234 AFMT_EXTCHANNEL(wrch->format)));
1235 chn_setspeed(wrch, p->play_rate);
1237 p->play_rate = wrch->speed;
1238 p->play_format = AFMT_ENCODING(wrch->format);
1246 if (cmd == AIOSFMT && p->rec_format != 0) {
1248 SND_FORMAT(p->rec_format,
1249 AFMT_CHANNEL(rdch->format),
1250 AFMT_EXTCHANNEL(rdch->format)));
1251 chn_setspeed(rdch, p->rec_rate);
1253 p->rec_rate = rdch->speed;
1254 p->rec_format = AFMT_ENCODING(rdch->format);
1260 PCM_RELEASE_QUICK(d);
1264 case AIOGCAP: /* get capabilities */
1266 snd_capabilities *p = (snd_capabilities *)arg;
1267 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
1273 rcaps = chn_getcaps(rdch);
1277 pcaps = chn_getcaps(wrch);
1279 p->rate_min = max(rcaps? rcaps->minspeed : 0,
1280 pcaps? pcaps->minspeed : 0);
1281 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
1282 pcaps? pcaps->maxspeed : 1000000);
1283 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
1284 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
1285 /* XXX bad on sb16 */
1286 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
1287 (wrch? chn_getformats(wrch) : 0xffffffff);
1289 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
1290 pdev = d->mixer_dev;
1291 p->mixers = 1; /* default: one mixer */
1292 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
1293 p->left = p->right = 100;
1303 if (*arg_i == AIOSYNC_PLAY && wrch) {
1305 *arg_i = chn_abort(wrch);
1307 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
1309 *arg_i = chn_abort(rdch);
1312 kprintf("AIOSTOP: bad channel 0x%x\n", *arg_i);
1318 kprintf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
1319 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
1323 * here follow the standard ioctls (filio.h etc.)
1325 case FIONREAD: /* get # bytes to read */
1328 /* if (rdch && rdch->bufhard.dl)
1329 while (chn_rdfeed(rdch) == 0);
1331 *arg_i = sndbuf_getready(rdch->bufsoft);
1339 case FIOASYNC: /*set/clear async i/o */
1340 DEB( kprintf("FIOASYNC\n") ; )
1343 case SNDCTL_DSP_NONBLOCK: /* set non-blocking i/o */
1344 case FIONBIO: /* set/clear non-blocking i/o */
1347 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1348 rdch->flags |= CHN_F_NBIO;
1350 rdch->flags &= ~CHN_F_NBIO;
1355 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1356 wrch->flags |= CHN_F_NBIO;
1358 wrch->flags &= ~CHN_F_NBIO;
1364 * Finally, here is the linux-compatible ioctl interface
1366 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
1367 case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
1368 case SNDCTL_DSP_GETBLKSIZE:
1369 chn = wrch ? wrch : rdch;
1372 *arg_i = sndbuf_getblksz(chn->bufsoft);
1380 case SNDCTL_DSP_SETBLKSIZE:
1381 RANGE(*arg_i, 16, 65536);
1382 PCM_ACQUIRE_QUICK(d);
1385 chn_setblocksize(wrch, 2, *arg_i);
1390 chn_setblocksize(rdch, 2, *arg_i);
1393 PCM_RELEASE_QUICK(d);
1396 case SNDCTL_DSP_RESET:
1397 DEB(kprintf("dsp reset\n"));
1412 case SNDCTL_DSP_SYNC:
1413 DEB(kprintf("dsp sync\n"));
1414 /* chn_sync may sleep */
1422 case SNDCTL_DSP_SPEED:
1423 /* chn_setspeed may sleep */
1425 PCM_ACQUIRE_QUICK(d);
1428 ret = chn_setspeed(wrch, *arg_i);
1432 if (rdch && ret == 0) {
1434 ret = chn_setspeed(rdch, *arg_i);
1439 PCM_RELEASE_QUICK(d);
1443 case SOUND_PCM_READ_RATE:
1444 chn = wrch ? wrch : rdch;
1447 *arg_i = chn->speed;
1455 case SNDCTL_DSP_STEREO:
1457 *arg_i = (*arg_i)? 2 : 1;
1458 PCM_ACQUIRE_QUICK(d);
1461 ret = chn_setformat(wrch,
1462 SND_FORMAT(wrch->format, *arg_i, 0));
1463 tmp = (AFMT_CHANNEL(wrch->format) > 1)? 1 : 0;
1466 if (rdch && ret == 0) {
1468 ret = chn_setformat(rdch,
1469 SND_FORMAT(rdch->format, *arg_i, 0));
1471 tmp = (AFMT_CHANNEL(rdch->format) > 1)? 1 : 0;
1474 PCM_RELEASE_QUICK(d);
1478 case SOUND_PCM_WRITE_CHANNELS:
1479 /* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
1486 struct pcmchan_matrix *m;
1490 if (*arg_i > SND_CHN_MAX)
1491 *arg_i = SND_CHN_MAX;
1493 m = feeder_matrix_default_channel_map(*arg_i);
1499 PCM_ACQUIRE_QUICK(d);
1502 ret = chn_setformat(wrch,
1503 SND_FORMAT(wrch->format, *arg_i, ext));
1504 tmp = AFMT_CHANNEL(wrch->format);
1507 if (rdch && ret == 0) {
1509 ret = chn_setformat(rdch,
1510 SND_FORMAT(rdch->format, *arg_i, ext));
1512 tmp = AFMT_CHANNEL(rdch->format);
1515 PCM_RELEASE_QUICK(d);
1518 chn = wrch ? wrch : rdch;
1520 *arg_i = AFMT_CHANNEL(chn->format);
1525 case SOUND_PCM_READ_CHANNELS:
1526 chn = wrch ? wrch : rdch;
1529 *arg_i = AFMT_CHANNEL(chn->format);
1537 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
1538 chn = wrch ? wrch : rdch;
1541 *arg_i = chn_getformats(chn);
1549 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
1550 if (*arg_i != AFMT_QUERY) {
1552 PCM_ACQUIRE_QUICK(d);
1555 ret = chn_setformat(wrch, SND_FORMAT(*arg_i,
1556 AFMT_CHANNEL(wrch->format),
1557 AFMT_EXTCHANNEL(wrch->format)));
1561 if (rdch && ret == 0) {
1563 ret = chn_setformat(rdch, SND_FORMAT(*arg_i,
1564 AFMT_CHANNEL(rdch->format),
1565 AFMT_EXTCHANNEL(rdch->format)));
1570 PCM_RELEASE_QUICK(d);
1571 *arg_i = AFMT_ENCODING(tmp);
1573 chn = wrch ? wrch : rdch;
1575 *arg_i = AFMT_ENCODING(chn->format);
1580 case SNDCTL_DSP_SETFRAGMENT:
1581 DEB(kprintf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
1583 uint32_t fragln = (*arg_i) & 0x0000ffff;
1584 uint32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
1586 uint32_t r_maxfrags, r_fragsz;
1588 RANGE(fragln, 4, 16);
1589 fragsz = 1 << fragln;
1592 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1595 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
1596 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1598 DEB(kprintf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
1599 PCM_ACQUIRE_QUICK(d);
1602 ret = chn_setblocksize(rdch, maxfrags, fragsz);
1603 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
1604 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
1607 r_maxfrags = maxfrags;
1610 if (wrch && ret == 0) {
1612 ret = chn_setblocksize(wrch, maxfrags, fragsz);
1613 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
1614 fragsz = sndbuf_getblksz(wrch->bufsoft);
1616 } else { /* use whatever came from the read channel */
1617 maxfrags = r_maxfrags;
1620 PCM_RELEASE_QUICK(d);
1623 while (fragsz > 1) {
1627 *arg_i = (maxfrags << 16) | fragln;
1631 case SNDCTL_DSP_GETISPACE:
1632 /* return the size of data available in the input queue */
1634 audio_buf_info *a = (audio_buf_info *)arg;
1636 struct snd_dbuf *bs = rdch->bufsoft;
1639 a->bytes = sndbuf_getready(bs);
1640 a->fragments = a->bytes / sndbuf_getblksz(bs);
1641 a->fragstotal = sndbuf_getblkcnt(bs);
1642 a->fragsize = sndbuf_getblksz(bs);
1649 case SNDCTL_DSP_GETOSPACE:
1650 /* return space available in the output queue */
1652 audio_buf_info *a = (audio_buf_info *)arg;
1654 struct snd_dbuf *bs = wrch->bufsoft;
1657 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1658 a->bytes = sndbuf_getfree(bs);
1659 a->fragments = a->bytes / sndbuf_getblksz(bs);
1660 a->fragstotal = sndbuf_getblkcnt(bs);
1661 a->fragsize = sndbuf_getblksz(bs);
1668 case SNDCTL_DSP_GETIPTR:
1670 count_info *a = (count_info *)arg;
1672 struct snd_dbuf *bs = rdch->bufsoft;
1675 /* XXX abusive DMA update: chn_rdupdate(rdch); */
1676 a->bytes = sndbuf_gettotal(bs);
1677 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
1678 a->ptr = sndbuf_getfreeptr(bs);
1679 rdch->blocks = sndbuf_getblocks(bs);
1686 case SNDCTL_DSP_GETOPTR:
1688 count_info *a = (count_info *)arg;
1690 struct snd_dbuf *bs = wrch->bufsoft;
1693 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1694 a->bytes = sndbuf_gettotal(bs);
1695 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
1696 a->ptr = sndbuf_getreadyptr(bs);
1697 wrch->blocks = sndbuf_getblocks(bs);
1704 case SNDCTL_DSP_GETCAPS:
1706 *arg_i = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER;
1707 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1708 *arg_i |= PCM_CAP_DUPLEX;
1712 case SOUND_PCM_READ_BITS:
1713 chn = wrch ? wrch : rdch;
1716 if (chn->format & AFMT_8BIT)
1718 else if (chn->format & AFMT_16BIT)
1720 else if (chn->format & AFMT_24BIT)
1722 else if (chn->format & AFMT_32BIT)
1733 case SNDCTL_DSP_SETTRIGGER:
1736 rdch->flags &= ~CHN_F_NOTRIGGER;
1737 if (*arg_i & PCM_ENABLE_INPUT)
1742 rdch->flags |= CHN_F_NOTRIGGER;
1748 wrch->flags &= ~CHN_F_NOTRIGGER;
1749 if (*arg_i & PCM_ENABLE_OUTPUT)
1754 wrch->flags |= CHN_F_NOTRIGGER;
1760 case SNDCTL_DSP_GETTRIGGER:
1764 if (wrch->flags & CHN_F_TRIGGERED)
1765 *arg_i |= PCM_ENABLE_OUTPUT;
1770 if (rdch->flags & CHN_F_TRIGGERED)
1771 *arg_i |= PCM_ENABLE_INPUT;
1776 case SNDCTL_DSP_GETODELAY:
1778 struct snd_dbuf *bs = wrch->bufsoft;
1781 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1782 *arg_i = sndbuf_getready(bs);
1788 case SNDCTL_DSP_POST:
1791 wrch->flags &= ~CHN_F_NOTRIGGER;
1797 case SNDCTL_DSP_SETDUPLEX:
1799 * switch to full-duplex mode if card is in half-duplex
1800 * mode and is able to work in full-duplex mode
1803 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1804 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1809 * The following four ioctls are simple wrappers around mixer_ioctl
1810 * with no further processing. xcmd is short for "translated
1813 case SNDCTL_DSP_GETRECVOL:
1815 xcmd = SOUND_MIXER_READ_RECLEV;
1819 case SNDCTL_DSP_SETRECVOL:
1821 xcmd = SOUND_MIXER_WRITE_RECLEV;
1825 case SNDCTL_DSP_GETPLAYVOL:
1827 xcmd = SOUND_MIXER_READ_PCM;
1831 case SNDCTL_DSP_SETPLAYVOL:
1833 xcmd = SOUND_MIXER_WRITE_PCM;
1837 ret = dsp_ioctl_channel(i_dev, chn, xcmd, arg);
1843 if (d->mixer_dev != NULL) {
1844 PCM_ACQUIRE_QUICK(d);
1845 ret = mixer_ioctl_cmd(d->mixer_dev, xcmd, arg, -1,
1847 PCM_RELEASE_QUICK(d);
1853 case SNDCTL_DSP_GET_RECSRC_NAMES:
1854 case SNDCTL_DSP_GET_RECSRC:
1855 case SNDCTL_DSP_SET_RECSRC:
1856 if (d->mixer_dev != NULL) {
1857 PCM_ACQUIRE_QUICK(d);
1858 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1,
1860 PCM_RELEASE_QUICK(d);
1866 * The following 3 ioctls aren't very useful at the moment. For
1867 * now, only a single channel is associated with a cdev (/dev/dspN
1868 * instance), so there's only a single output routing to use (i.e.,
1869 * the wrch bound to this cdev).
1871 case SNDCTL_DSP_GET_PLAYTGT_NAMES:
1873 oss_mixer_enuminfo *ei;
1874 ei = (oss_mixer_enuminfo *)arg;
1877 ei->version = 0; /* static for now */
1878 ei->strindex[0] = 0;
1882 strlcpy(ei->strings, wrch->name,
1883 sizeof(ei->strings));
1886 ei->strings[0] = '\0';
1890 case SNDCTL_DSP_GET_PLAYTGT:
1891 case SNDCTL_DSP_SET_PLAYTGT: /* yes, they are the same for now */
1894 * OSSv4: "The value that was accepted by the device will
1895 * be returned back in the variable pointed by the
1904 case SNDCTL_DSP_SILENCE:
1906 * Flush the software (pre-feed) buffer, but try to minimize playback
1907 * interruption. (I.e., record unplayed samples with intent to
1908 * restore by SNDCTL_DSP_SKIP.) Intended for application "pause"
1914 struct snd_dbuf *bs;
1916 while (wrch->inprog != 0)
1917 cv_wait(&wrch->cv, wrch->lock);
1919 if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) {
1920 bs->sl = sndbuf_getready(bs);
1921 sndbuf_dispose(bs, bs->shadbuf, sndbuf_getready(bs));
1922 sndbuf_fillsilence(bs);
1929 case SNDCTL_DSP_SKIP:
1931 * OSSv4 docs: "This ioctl call discards all unplayed samples in the
1932 * playback buffer by moving the current write position immediately
1933 * before the point where the device is currently reading the samples."
1938 struct snd_dbuf *bs;
1940 while (wrch->inprog != 0)
1941 cv_wait(&wrch->cv, wrch->lock);
1943 if ((bs->shadbuf != NULL) && (bs->sl > 0)) {
1944 sndbuf_softreset(bs);
1945 sndbuf_acquire(bs, bs->shadbuf, bs->sl);
1953 case SNDCTL_DSP_CURRENT_OPTR:
1954 case SNDCTL_DSP_CURRENT_IPTR:
1956 * @note Changing formats resets the buffer counters, which differs
1957 * from the 4Front drivers. However, I don't expect this to be
1958 * much of a problem.
1960 * @note In a test where @c CURRENT_OPTR is called immediately after write
1961 * returns, this driver is about 32K samples behind whereas
1962 * 4Front's is about 8K samples behind. Should determine source
1963 * of discrepancy, even if only out of curiosity.
1965 * @todo Actually test SNDCTL_DSP_CURRENT_IPTR.
1967 chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch;
1971 struct snd_dbuf *bs;
1974 oss_count_t *oc = (oss_count_t *)arg;
1979 tmp = (sndbuf_getsize(b) + chn_getptr(chn) - sndbuf_gethwptr(b)) % sndbuf_getsize(b);
1980 oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getalign(b);
1981 oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getalign(b);
1983 oc->samples = sndbuf_gettotal(bs) / sndbuf_getalign(bs);
1984 oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getalign(bs);
1990 case SNDCTL_DSP_HALT_OUTPUT:
1991 case SNDCTL_DSP_HALT_INPUT:
1992 chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch;
2002 case SNDCTL_DSP_LOW_WATER:
2004 * Set the number of bytes required to attract attention by
2009 wrch->lw = (*arg_i > 1) ? *arg_i : 1;
2014 rdch->lw = (*arg_i > 1) ? *arg_i : 1;
2019 case SNDCTL_DSP_GETERROR:
2021 * OSSv4 docs: "All errors and counters will automatically be
2022 * cleared to zeroes after the call so each call will return only
2023 * the errors that occurred after the previous invocation. ... The
2024 * play_underruns and rec_overrun fields are the only useful fields
2025 * returned by OSS 4.0."
2028 audio_errinfo *ei = (audio_errinfo *)arg;
2030 bzero((void *)ei, sizeof(*ei));
2034 ei->play_underruns = wrch->xruns;
2040 ei->rec_overruns = rdch->xruns;
2047 case SNDCTL_DSP_SYNCGROUP:
2048 PCM_ACQUIRE_QUICK(d);
2049 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
2050 PCM_RELEASE_QUICK(d);
2053 case SNDCTL_DSP_SYNCSTART:
2054 PCM_ACQUIRE_QUICK(d);
2055 ret = dsp_oss_syncstart(*arg_i);
2056 PCM_RELEASE_QUICK(d);
2059 case SNDCTL_DSP_POLICY:
2060 PCM_ACQUIRE_QUICK(d);
2061 ret = dsp_oss_policy(wrch, rdch, *arg_i);
2062 PCM_RELEASE_QUICK(d);
2065 case SNDCTL_DSP_COOKEDMODE:
2066 PCM_ACQUIRE_QUICK(d);
2067 if (!(dsp_get_flags(i_dev) & SD_F_BITPERFECT))
2068 ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
2069 PCM_RELEASE_QUICK(d);
2071 case SNDCTL_DSP_GET_CHNORDER:
2072 PCM_ACQUIRE_QUICK(d);
2073 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
2074 PCM_RELEASE_QUICK(d);
2076 case SNDCTL_DSP_SET_CHNORDER:
2077 PCM_ACQUIRE_QUICK(d);
2078 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
2079 PCM_RELEASE_QUICK(d);
2081 case SNDCTL_DSP_GETCHANNELMASK: /* XXX vlc */
2082 PCM_ACQUIRE_QUICK(d);
2083 ret = dsp_oss_getchannelmask(wrch, rdch, (int *)arg);
2084 PCM_RELEASE_QUICK(d);
2086 case SNDCTL_DSP_BIND_CHANNEL: /* XXX what?!? */
2089 #ifdef OSSV4_EXPERIMENT
2091 * XXX The following ioctls are not yet supported and just return
2094 case SNDCTL_DSP_GETOPEAKS:
2095 case SNDCTL_DSP_GETIPEAKS:
2096 chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch;
2100 oss_peaks_t *op = (oss_peaks_t *)arg;
2104 ret = chn_getpeaks(chn, &lpeak, &rpeak);
2116 * XXX Once implemented, revisit this for proper cv protection
2119 case SNDCTL_GETLABEL:
2120 ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg);
2122 case SNDCTL_SETLABEL:
2123 ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg);
2125 case SNDCTL_GETSONG:
2126 ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg);
2128 case SNDCTL_SETSONG:
2129 ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg);
2131 case SNDCTL_SETNAME:
2132 ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg);
2136 * @note The S/PDIF interface ioctls, @c SNDCTL_DSP_READCTL and
2137 * @c SNDCTL_DSP_WRITECTL have been omitted at the suggestion of
2138 * 4Front Technologies.
2140 case SNDCTL_DSP_READCTL:
2141 case SNDCTL_DSP_WRITECTL:
2144 #endif /* !0 (explicitly omitted ioctls) */
2146 #endif /* !OSSV4_EXPERIMENT */
2147 case SNDCTL_DSP_MAPINBUF:
2148 case SNDCTL_DSP_MAPOUTBUF:
2149 case SNDCTL_DSP_SETSYNCRO:
2152 case SNDCTL_DSP_SUBDIVIDE:
2153 case SOUND_PCM_WRITE_FILTER:
2154 case SOUND_PCM_READ_FILTER:
2155 /* dunno what these do, don't sound important */
2158 DEB(kprintf("default ioctl fn 0x%08lx fail\n", cmd));
2168 static struct filterops dsp_read_filtops =
2169 { FILTEROP_ISFD, NULL, dsp_filter_detach, dsp_filter_read };
2170 static struct filterops dsp_write_filtops =
2171 { FILTEROP_ISFD, NULL, dsp_filter_detach, dsp_filter_write };
2174 /*dsp_poll(struct cdev *i_dev, int events, struct thread *td)*/
2175 dsp_kqfilter(struct dev_kqfilter_args *ap)
2177 struct knote *kn = ap->a_kn;
2178 struct klist *klist;
2179 struct cdev *i_dev = ap->a_head.a_dev;
2180 struct snddev_info *d;
2181 struct pcm_channel *wrch, *rdch;
2182 struct snd_dbuf *bs = NULL;
2185 d = dsp_get_info(i_dev);
2186 if (!DSP_REGISTERED(d, i_dev))
2195 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2197 switch (kn->kn_filter) {
2200 kn->kn_fop = &dsp_read_filtops;
2201 kn->kn_hook = (caddr_t)rdch;
2208 kn->kn_fop = &dsp_write_filtops;
2209 kn->kn_hook = (caddr_t)wrch;
2215 ap->a_result = EOPNOTSUPP;
2219 if (ap->a_result == 0) {
2220 klist = &sndbuf_getkq(bs)->ki_note;
2221 knote_insert(klist, kn);
2224 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2232 dsp_filter_detach(struct knote *kn)
2234 struct pcm_channel *ch = (struct pcm_channel *)kn->kn_hook;
2235 struct snd_dbuf *bs = ch->bufsoft;
2236 struct klist *klist;
2239 klist = &sndbuf_getkq(bs)->ki_note;
2240 knote_remove(klist, kn);
2245 dsp_filter_read(struct knote *kn, long hint)
2247 struct pcm_channel *rdch = (struct pcm_channel *)kn->kn_hook;
2248 struct thread *td = curthread;
2252 ready = chn_poll(rdch, 1, td);
2259 dsp_filter_write(struct knote *kn, long hint)
2261 struct pcm_channel *wrch = (struct pcm_channel *)kn->kn_hook;
2262 struct thread *td = curthread;
2266 ready = chn_poll(wrch, 1, td);
2273 dsp_mmap(struct dev_mmap_args *ap)
2275 vm_offset_t offset = ap->a_offset;
2277 /* XXX memattr is not honored */
2278 ap->a_result = vtophys(offset);
2283 dsp_mmap_single(struct dev_mmap_single_args *ap)
2285 struct cdev *i_dev = ap->a_head.a_dev;
2286 vm_ooffset_t *offset = ap->a_offset;
2287 vm_size_t size = ap->a_size;
2288 struct vm_object **object = ap->a_object;
2289 int nprot = ap->a_nprot;
2290 struct snddev_info *d;
2291 struct pcm_channel *wrch, *rdch, *c;
2294 * Reject PROT_EXEC by default. It just doesn't makes sense.
2295 * Unfortunately, we have to give up this one due to linux_mmap
2298 * http://lists.freebsd.org/pipermail/freebsd-emulation/2007-June/003698.html
2302 if ((nprot & PROT_EXEC) && (dsp_mmap_allow_prot_exec < 0 ||
2303 (dsp_mmap_allow_prot_exec == 0 &&
2304 SV_CURPROC_ABI() != SV_ABI_LINUX)))
2306 if ((nprot & PROT_EXEC) && dsp_mmap_allow_prot_exec < 1)
2311 * PROT_READ (alone) selects the input buffer.
2312 * PROT_WRITE (alone) selects the output buffer.
2313 * PROT_WRITE|PROT_READ together select the output buffer.
2315 if ((nprot & (PROT_READ | PROT_WRITE)) == 0)
2318 d = dsp_get_info(i_dev);
2319 if (!DSP_REGISTERED(d, i_dev))
2324 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2326 c = ((nprot & PROT_WRITE) != 0) ? wrch : rdch;
2327 if (c == NULL || (c->flags & CHN_F_MMAP_INVALID) ||
2328 (*offset + size) > sndbuf_getsize(c->bufsoft) ||
2329 (wrch != NULL && (wrch->flags & CHN_F_MMAP_INVALID)) ||
2330 (rdch != NULL && (rdch->flags & CHN_F_MMAP_INVALID))) {
2331 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2337 wrch->flags |= CHN_F_MMAP;
2339 rdch->flags |= CHN_F_MMAP;
2341 *offset = (uintptr_t)sndbuf_getbufofs(c->bufsoft, *offset);
2342 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2343 *object = dev_pager_alloc(i_dev, size, nprot, *offset);
2347 if (*object == NULL)
2352 /* So much for dev_stdclone() */
2354 dsp_stdclone(const char *name, char *namep, char *sep, int use_sep, int *u, int *c)
2358 len = strlen(namep);
2360 if (bcmp(name, namep, len) != 0)
2365 if (isdigit(*name) == 0)
2370 if (*name == '0' && !(name[1] == '\0' || bcmp(name + 1, sep, len) == 0))
2373 for (*u = 0; isdigit(*name) != 0; name++) {
2381 return ((use_sep == 0) ? 0 : ENODEV);
2383 if (bcmp(name, sep, len) != 0 || isdigit(name[len]) == 0)
2388 if (*name == '0' && name[1] != '\0')
2391 for (*c = 0; isdigit(*name) != 0; name++) {
2405 * for i = 0 to channels of device N
2406 * if dspN.i isn't busy and in the right dir, create a dev_t and return it
2409 dsp_clone(struct dev_clone_args *ap)
2411 struct cdev *i_dev = ap->a_head.a_dev;
2412 const char *name = ap->a_name;
2413 struct snddev_info *d;
2414 struct snd_clone_entry *ce;
2415 struct pcm_channel *c;
2416 int i, unit, udcmask, cunit, devtype, devhw, devcmax, tumax;
2417 char *devname, *devcmp, *devsep;
2419 static struct cdev *dev;
2421 KASSERT(dsp_umax >= 0 && dsp_cmax >= 0, ("Uninitialized unit!"));
2423 d = dsp_get_info(i_dev);
2437 for (i = 0; unit == -1 &&
2438 i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2439 devtype = dsp_cdevs[i].type;
2440 devcmp = dsp_cdevs[i].name;
2441 devsep = dsp_cdevs[i].sep;
2442 devname = dsp_cdevs[i].alias;
2443 if (devname == NULL)
2445 devhw = dsp_cdevs[i].hw;
2446 devcmax = dsp_cdevs[i].max - 1;
2447 if (strcmp(name, devcmp) == 0)
2449 else if (dsp_stdclone(name, devcmp, devsep,
2450 dsp_cdevs[i].use_sep, &unit, &cunit) != 0) {
2456 d = devclass_get_softc(pcm_devclass, unit);
2457 if (!PCM_REGISTERED(d) || d->clones == NULL) {
2461 /* XXX Need Giant magic entry ??? */
2464 if (snd_clone_disabled(d->clones)) {
2473 udcmask = snd_u2unit(unit) | snd_d2unit(devtype);
2476 KASSERT(devcmax <= dsp_cmax,
2477 ("overflow: devcmax=%d, dsp_cmax=%d", devcmax, dsp_cmax));
2478 if (cunit > devcmax) {
2479 PCM_RELEASE_QUICK(d);
2482 udcmask |= snd_c2unit(cunit);
2483 CHN_FOREACH(c, d, channels.pcm) {
2485 if (c->unit != udcmask) {
2490 udcmask &= ~snd_c2unit(cunit);
2492 * Temporarily increase clone maxunit to overcome
2493 * vchan flexibility.
2495 * # sysctl dev.pcm.0.play.vchans=256
2496 * dev.pcm.0.play.vchans: 1 -> 256
2497 * # cat /dev/zero > /dev/dsp0.vp255 &
2499 * # sysctl dev.pcm.0.play.vchans=0
2500 * dev.pcm.0.play.vchans: 256 -> 1
2502 * [1] + running cat /dev/zero > /dev/dsp0.vp255
2504 * # cat /dev/zero > /dev/dsp0.vp255
2505 * zsh: operation not supported: /dev/dsp0.vp255
2507 tumax = snd_clone_getmaxunit(d->clones);
2509 snd_clone_setmaxunit(d->clones, cunit);
2512 goto dsp_clone_alloc;
2515 * Ok, so we're requesting unallocated vchan, but still
2516 * within maximum vchan limit.
2518 if (((devtype == SND_DEV_DSPHW_VPLAY && d->pvchancount > 0) ||
2519 (devtype == SND_DEV_DSPHW_VREC && d->rvchancount > 0)) &&
2520 cunit < snd_maxautovchans) {
2521 udcmask &= ~snd_c2unit(cunit);
2522 tumax = snd_clone_getmaxunit(d->clones);
2524 snd_clone_setmaxunit(d->clones, cunit);
2527 goto dsp_clone_alloc;
2529 PCM_RELEASE_QUICK(d);
2534 ce = snd_clone_alloc(d->clones, &dev, &cunit, udcmask);
2536 snd_clone_setmaxunit(d->clones, tumax);
2538 udcmask |= snd_c2unit(cunit);
2539 dev = make_only_dev(&dsp_ops, PCMMINOR(udcmask),
2540 UID_ROOT, GID_WHEEL, 0666, "%s%d%s%d",
2541 devname, unit, devsep, cunit);
2542 snd_clone_register(ce, dev);
2546 PCM_RELEASE_QUICK(d);
2555 dsp_sysinit(void *p)
2557 if (dsp_ehtag != NULL)
2559 /* initialize unit numbering */
2561 dsp_umax = PCMMAXUNIT;
2562 dsp_cmax = PCMMAXCHAN;
2563 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
2567 dsp_sysuninit(void *p)
2569 if (dsp_ehtag == NULL)
2571 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
2575 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
2576 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
2579 dsp_unit2name(char *buf, size_t len, int unit)
2583 KASSERT(buf != NULL && len != 0,
2584 ("bogus buf=%p len=%ju", buf, (uintmax_t)len));
2586 dtype = snd_unit2d(unit);
2588 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2589 if (dtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
2591 ksnprintf(buf, len, "%s%d%s%d", dsp_cdevs[i].name,
2592 snd_unit2u(unit), dsp_cdevs[i].sep, snd_unit2c(unit));
2600 * @brief Handler for SNDCTL_AUDIOINFO.
2602 * Gathers information about the audio device specified in ai->dev. If
2603 * ai->dev == -1, then this function gathers information about the current
2604 * device. If the call comes in on a non-audio device and ai->dev == -1,
2607 * This routine is supposed to go practically straight to the hardware,
2608 * getting capabilities directly from the sound card driver, side-stepping
2609 * the intermediate channel interface.
2611 * Note, however, that the usefulness of this command is significantly
2612 * decreased when requesting info about any device other than the one serving
2613 * the request. While each snddev_channel refers to a specific device node,
2614 * the converse is *not* true. Currently, when a sound device node is opened,
2615 * the sound subsystem scans for an available audio channel (or channels, if
2616 * opened in read+write) and then assigns them to the si_drv[12] private
2617 * data fields. As a result, any information returned linking a channel to
2618 * a specific character device isn't necessarily accurate.
2621 * Calling threads must not hold any snddev_info or pcm_channel locks.
2623 * @param dev device on which the ioctl was issued
2624 * @param ai ioctl request data container
2627 * @retval EINVAL ai->dev specifies an invalid device
2629 * @todo Verify correctness of Doxygen tags. ;)
2632 dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
2634 struct pcmchan_caps *caps;
2635 struct pcm_channel *ch;
2636 struct snddev_info *d;
2638 int i, nchan, *rates, minch, maxch;
2639 char *devname, buf[CHN_NAMELEN];
2642 * If probing the device that received the ioctl, make sure it's a
2643 * DSP device. (Users may use this ioctl with /dev/mixer and
2646 if (ai->dev == -1 && i_dev->si_ops != &dsp_ops)
2652 bzero(buf, sizeof(buf));
2655 * Search for the requested audio device (channel). Start by
2656 * iterating over pcm devices.
2658 for (i = 0; pcm_devclass != NULL &&
2659 i < devclass_get_maxunit(pcm_devclass); i++) {
2660 d = devclass_get_softc(pcm_devclass, i);
2661 if (!PCM_REGISTERED(d))
2664 /* XXX Need Giant magic entry ??? */
2666 /* See the note in function docblock */
2667 PCM_UNLOCKASSERT(d);
2670 CHN_FOREACH(ch, d, channels.pcm) {
2671 CHN_UNLOCKASSERT(ch);
2673 if (ai->dev == -1) {
2674 if (DSP_REGISTERED(d, i_dev) &&
2675 (ch == PCM_RDCH(i_dev) || /* record ch */
2676 ch == PCM_WRCH(i_dev))) { /* playback ch */
2677 devname = dsp_unit2name(buf,
2678 sizeof(buf), ch->unit);
2680 } else if (ai->dev == nchan) {
2681 devname = dsp_unit2name(buf, sizeof(buf),
2684 if (devname != NULL)
2690 if (devname != NULL) {
2692 * At this point, the following synchronization stuff
2694 * - a specific PCM device is locked.
2695 * - a specific audio channel has been locked, so be
2696 * sure to unlock when exiting;
2699 caps = chn_getcaps(ch);
2702 * With all handles collected, zero out the user's
2703 * container and begin filling in its fields.
2705 bzero((void *)ai, sizeof(oss_audioinfo));
2708 strlcpy(ai->name, ch->name, sizeof(ai->name));
2710 if ((ch->flags & CHN_F_BUSY) == 0)
2713 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
2717 * @c cmd - OSSv4 docs: "Only supported under Linux at
2718 * this moment." Cop-out, I know, but I'll save
2719 * running around in the process table for later.
2720 * Is there a risk of leaking information?
2725 * These flags stolen from SNDCTL_DSP_GETCAPS handler.
2726 * Note, however, that a single channel operates in
2727 * only one direction, so PCM_CAP_DUPLEX is out.
2730 * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep
2731 * these in pcmchan::caps?
2733 ai->caps = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER |
2734 ((ch->direction == PCMDIR_PLAY) ? PCM_CAP_OUTPUT : PCM_CAP_INPUT);
2737 * Collect formats supported @b natively by the
2738 * device. Also determine min/max channels. (I.e.,
2739 * mono, stereo, or both?)
2741 * If any channel is stereo, maxch = 2;
2742 * if all channels are stereo, minch = 2, too;
2743 * if any channel is mono, minch = 1;
2744 * and if all channels are mono, maxch = 1.
2749 for (i = 0; caps->fmtlist[i]; i++) {
2750 fmts |= caps->fmtlist[i];
2751 if (AFMT_CHANNEL(caps->fmtlist[i]) > 1) {
2752 minch = (minch == 0) ? 2 : minch;
2756 maxch = (maxch == 0) ? 1 : maxch;
2760 if (ch->direction == PCMDIR_PLAY)
2761 ai->oformats = fmts;
2763 ai->iformats = fmts;
2767 * @c magic - OSSv4 docs: "Reserved for internal use
2771 * @c card_number - OSSv4 docs: "Number of the sound
2772 * card where this device belongs or -1 if this
2773 * information is not available. Applications
2774 * should normally not use this field for any
2777 ai->card_number = -1;
2779 * @todo @c song_name - depends first on
2780 * SNDCTL_[GS]ETSONG @todo @c label - depends
2781 * on SNDCTL_[GS]ETLABEL
2782 * @todo @c port_number - routing information?
2784 ai->port_number = -1;
2785 ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1;
2788 * @c real_device - OSSv4 docs: "Obsolete."
2790 ai->real_device = -1;
2791 strlcpy(ai->devnode, "/dev/", sizeof(ai->devnode));
2792 strlcat(ai->devnode, devname, sizeof(ai->devnode));
2793 ai->enabled = device_is_attached(d->dev) ? 1 : 0;
2796 * @c flags - OSSv4 docs: "Reserved for future use."
2799 * @c binding - OSSv4 docs: "Reserved for future use."
2801 * @todo @c handle - haven't decided how to generate
2802 * this yet; bus, vendor, device IDs?
2804 ai->min_rate = caps->minspeed;
2805 ai->max_rate = caps->maxspeed;
2807 ai->min_channels = minch;
2808 ai->max_channels = maxch;
2810 ai->nrates = chn_getrates(ch, &rates);
2811 if (ai->nrates > OSS_MAX_SAMPLE_RATES)
2812 ai->nrates = OSS_MAX_SAMPLE_RATES;
2814 for (i = 0; i < ai->nrates; i++)
2815 ai->rates[i] = rates[i];
2817 ai->next_play_engine = 0;
2818 ai->next_rec_engine = 0;
2825 if (devname != NULL)
2829 /* Exhausted the search -- nothing is locked, so return. */
2834 * @brief Assigns a PCM channel to a sync group.
2836 * Sync groups are used to enable audio operations on multiple devices
2837 * simultaneously. They may be used with any number of devices and may
2838 * span across applications. Devices are added to groups with
2839 * the SNDCTL_DSP_SYNCGROUP ioctl, and operations are triggered with the
2840 * SNDCTL_DSP_SYNCSTART ioctl.
2842 * If the @c id field of the @c group parameter is set to zero, then a new
2843 * sync group is created. Otherwise, wrch and rdch (if set) are added to
2844 * the group specified.
2846 * @todo As far as memory allocation, should we assume that things are
2847 * okay and allocate with M_WAITOK before acquiring channel locks,
2848 * freeing later if not?
2850 * @param wrch output channel associated w/ device (if any)
2851 * @param rdch input channel associated w/ device (if any)
2852 * @param group Sync group parameters
2855 * @retval non-zero error to be propagated upstream
2858 dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
2860 struct pcmchan_syncmember *smrd, *smwr;
2861 struct pcmchan_syncgroup *sg;
2870 * Free_unr() may sleep, so store released syncgroup IDs until after
2871 * all locks are released.
2873 sg_ids[0] = sg_ids[1] = sg_ids[2] = 0;
2878 * - Insert channel(s) into group's member list.
2879 * - Set CHN_F_NOTRIGGER on channel(s).
2880 * - Stop channel(s).
2884 * If device's channels are already mapped to a group, unmap them.
2888 sg_ids[0] = chn_syncdestroy(wrch);
2893 sg_ids[1] = chn_syncdestroy(rdch);
2897 * Verify that mode matches character device properites.
2898 * - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
2899 * - Bail if PCM_ENABLE_INPUT && rdch == NULL.
2901 if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) ||
2902 ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) {
2908 * An id of zero indicates the user wants to create a new
2911 if (group->id == 0) {
2912 sg = (struct pcmchan_syncgroup *)kmalloc(sizeof(*sg), M_DEVBUF, M_NOWAIT);
2914 SLIST_INIT(&sg->members);
2915 sg->id = alloc_unr(pcmsg_unrhdr);
2918 SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link);
2922 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2923 if (sg->id == group->id)
2930 /* Couldn't create or find a syncgroup. Fail. */
2935 * Allocate a syncmember, assign it and a channel together, and
2936 * insert into syncgroup.
2938 if (group->mode & PCM_ENABLE_INPUT) {
2939 smrd = (struct pcmchan_syncmember *)kmalloc(sizeof(*smrd), M_DEVBUF, M_NOWAIT);
2945 SLIST_INSERT_HEAD(&sg->members, smrd, link);
2950 rdch->flags |= CHN_F_NOTRIGGER;
2954 if (group->mode & PCM_ENABLE_OUTPUT) {
2955 smwr = (struct pcmchan_syncmember *)kmalloc(sizeof(*smwr), M_DEVBUF, M_NOWAIT);
2961 SLIST_INSERT_HEAD(&sg->members, smwr, link);
2966 wrch->flags |= CHN_F_NOTRIGGER;
2974 kfree(smrd, M_DEVBUF);
2975 if ((sg != NULL) && SLIST_EMPTY(&sg->members)) {
2977 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2978 kfree(sg, M_DEVBUF);
2995 free_unr(pcmsg_unrhdr, sg_ids[0]);
2997 free_unr(pcmsg_unrhdr, sg_ids[1]);
2999 free_unr(pcmsg_unrhdr, sg_ids[2]);
3005 * @brief Launch a sync group into action
3007 * Sync groups are established via SNDCTL_DSP_SYNCGROUP. This function
3008 * iterates over all members, triggering them along the way.
3010 * @note Caller must not hold any channel locks.
3012 * @param sg_id sync group identifier
3015 * @retval non-zero error worthy of propagating upstream to user
3018 dsp_oss_syncstart(int sg_id)
3020 struct pcmchan_syncmember *sm, *sm_tmp;
3021 struct pcmchan_syncgroup *sg;
3022 struct pcm_channel *c;
3025 /* Get the synclists lock */
3032 /* Search for syncgroup by ID */
3033 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
3034 if (sg->id == sg_id)
3038 /* Return EINVAL if not found */
3044 /* Any removals resulting in an empty group should've handled this */
3045 KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup"));
3048 * Attempt to lock all member channels - if any are already
3049 * locked, unlock those acquired, sleep for a bit, and try
3052 SLIST_FOREACH(sm, &sg->members, link) {
3053 if (CHN_TRYLOCK(sm->ch) == 0) {
3054 int timo = hz * 5/1000;
3058 /* Release all locked channels so far, retry */
3059 SLIST_FOREACH(sm_tmp, &sg->members, link) {
3060 /* sm is the member already locked */
3063 CHN_UNLOCK(sm_tmp->ch);
3066 /** @todo Is PRIBIO correct/ */
3067 ret = lksleep(sm, &snd_pcm_syncgroups_mtx,
3068 PCATCH, "pcmsg", timo);
3069 if (ret == EINTR || ret == ERESTART)
3073 ret = 0; /* Assumes ret == EAGAIN... */
3076 } while (needlocks && ret == 0);
3078 /* Proceed only if no errors encountered. */
3080 /* Launch channels */
3081 while ((sm = SLIST_FIRST(&sg->members)) != NULL) {
3082 SLIST_REMOVE_HEAD(&sg->members, link);
3087 c->flags &= ~CHN_F_NOTRIGGER;
3090 kfree(sm, M_DEVBUF);
3093 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
3094 kfree(sg, M_DEVBUF);
3100 * Free_unr() may sleep, so be sure to give up the syncgroup lock
3104 free_unr(pcmsg_unrhdr, sg_id);
3110 * @brief Handler for SNDCTL_DSP_POLICY
3112 * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment
3113 * size and count like with SNDCTL_DSP_SETFRAGMENT. Instead of the user
3114 * specifying those two parameters, s/he simply selects a number from 0..10
3115 * which corresponds to a buffer size. Smaller numbers request smaller
3116 * buffers with lower latencies (at greater overhead from more frequent
3117 * interrupts), while greater numbers behave in the opposite manner.
3119 * The 4Front spec states that a value of 5 should be the default. However,
3120 * this implementation deviates slightly by using a linear scale without
3121 * consulting drivers. I.e., even though drivers may have different default
3122 * buffer sizes, a policy argument of 5 will have the same result across
3125 * See http://manuals.opensound.com/developer/SNDCTL_DSP_POLICY.html for
3128 * @todo When SNDCTL_DSP_COOKEDMODE is supported, it'll be necessary to
3129 * work with hardware drivers directly.
3131 * @note PCM channel arguments must not be locked by caller.
3133 * @param wrch Pointer to opened playback channel (optional; may be NULL)
3134 * @param rdch " recording channel (optional; may be NULL)
3135 * @param policy Integer from [0:10]
3137 * @retval 0 constant (for now)
3140 dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
3144 if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX)
3147 /* Default: success */
3152 ret = chn_setlatency(rdch, policy);
3156 if (wrch && ret == 0) {
3158 ret = chn_setlatency(wrch, policy);
3169 * @brief Enable or disable "cooked" mode
3171 * This is a handler for @c SNDCTL_DSP_COOKEDMODE. When in cooked mode, which
3172 * is the default, the sound system handles rate and format conversions
3173 * automatically (ex: user writing 11025Hz/8 bit/unsigned but card only
3174 * operates with 44100Hz/16bit/signed samples).
3176 * Disabling cooked mode is intended for applications wanting to mmap()
3177 * a sound card's buffer space directly, bypassing the FreeBSD 2-stage
3178 * feeder architecture, presumably to gain as much control over audio
3179 * hardware as possible.
3181 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_COOKEDMODE.html
3184 * @param wrch playback channel (optional; may be NULL)
3185 * @param rdch recording channel (optional; may be NULL)
3186 * @param enabled 0 = raw mode, 1 = cooked mode
3188 * @retval EINVAL Operation not yet supported.
3191 dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
3195 * XXX I just don't get it. Why don't they call it
3196 * "BITPERFECT" ~ SNDCTL_DSP_BITPERFECT !?!?.
3197 * This is just plain so confusing, incoherent,
3198 * <insert any non-printable characters here>.
3200 if (!(enabled == 1 || enabled == 0))
3204 * I won't give in. I'm inverting its logic here and now.
3205 * Brag all you want, but "BITPERFECT" should be the better
3208 enabled ^= 0x00000001;
3212 wrch->flags &= ~CHN_F_BITPERFECT;
3213 wrch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3219 rdch->flags &= ~CHN_F_BITPERFECT;
3220 rdch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3228 * @brief Retrieve channel interleaving order
3230 * This is the handler for @c SNDCTL_DSP_GET_CHNORDER.
3232 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html
3235 * @note As the ioctl definition is still under construction, FreeBSD
3236 * does not currently support SNDCTL_DSP_GET_CHNORDER.
3238 * @param wrch playback channel (optional; may be NULL)
3239 * @param rdch recording channel (optional; may be NULL)
3240 * @param map channel map (result will be stored there)
3242 * @retval EINVAL Operation not yet supported.
3245 dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3247 struct pcm_channel *ch;
3250 ch = (wrch != NULL) ? wrch : rdch;
3253 ret = chn_oss_getorder(ch, map);
3262 * @brief Specify channel interleaving order
3264 * This is the handler for @c SNDCTL_DSP_SET_CHNORDER.
3266 * @note As the ioctl definition is still under construction, FreeBSD
3267 * does not currently support @c SNDCTL_DSP_SET_CHNORDER.
3269 * @param wrch playback channel (optional; may be NULL)
3270 * @param rdch recording channel (optional; may be NULL)
3271 * @param map channel map
3273 * @retval EINVAL Operation not yet supported.
3276 dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3284 ret = chn_oss_setorder(wrch, map);
3288 if (ret == 0 && rdch != NULL) {
3290 ret = chn_oss_setorder(rdch, map);
3298 dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch,
3301 struct pcm_channel *ch;
3306 ch = (wrch != NULL) ? wrch : rdch;
3310 ret = chn_oss_getmask(ch, &chnmask);
3321 #ifdef OSSV4_EXPERIMENT
3323 * @brief Retrieve an audio device's label
3325 * This is a handler for the @c SNDCTL_GETLABEL ioctl.
3327 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3330 * From Hannu@4Front: "For example ossxmix (just like some HW mixer
3331 * consoles) can show variable "labels" for certain controls. By default
3332 * the application name (say quake) is shown as the label but
3333 * applications may change the labels themselves."
3335 * @note As the ioctl definition is still under construction, FreeBSD
3336 * does not currently support @c SNDCTL_GETLABEL.
3338 * @param wrch playback channel (optional; may be NULL)
3339 * @param rdch recording channel (optional; may be NULL)
3340 * @param label label gets copied here
3342 * @retval EINVAL Operation not yet supported.
3345 dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3351 * @brief Specify an audio device's label
3353 * This is a handler for the @c SNDCTL_SETLABEL ioctl. Please see the
3354 * comments for @c dsp_oss_getlabel immediately above.
3356 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3359 * @note As the ioctl definition is still under construction, FreeBSD
3360 * does not currently support SNDCTL_SETLABEL.
3362 * @param wrch playback channel (optional; may be NULL)
3363 * @param rdch recording channel (optional; may be NULL)
3364 * @param label label gets copied from here
3366 * @retval EINVAL Operation not yet supported.
3369 dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3375 * @brief Retrieve name of currently played song
3377 * This is a handler for the @c SNDCTL_GETSONG ioctl. Audio players could
3378 * tell the system the name of the currently playing song, which would be
3379 * visible in @c /dev/sndstat.
3381 * See @c http://manuals.opensound.com/developer/SNDCTL_GETSONG.html
3384 * @note As the ioctl definition is still under construction, FreeBSD
3385 * does not currently support SNDCTL_GETSONG.
3387 * @param wrch playback channel (optional; may be NULL)
3388 * @param rdch recording channel (optional; may be NULL)
3389 * @param song song name gets copied here
3391 * @retval EINVAL Operation not yet supported.
3394 dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3400 * @brief Retrieve name of currently played song
3402 * This is a handler for the @c SNDCTL_SETSONG ioctl. Audio players could
3403 * tell the system the name of the currently playing song, which would be
3404 * visible in @c /dev/sndstat.
3406 * See @c http://manuals.opensound.com/developer/SNDCTL_SETSONG.html
3409 * @note As the ioctl definition is still under construction, FreeBSD
3410 * does not currently support SNDCTL_SETSONG.
3412 * @param wrch playback channel (optional; may be NULL)
3413 * @param rdch recording channel (optional; may be NULL)
3414 * @param song song name gets copied from here
3416 * @retval EINVAL Operation not yet supported.
3419 dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3425 * @brief Rename a device
3427 * This is a handler for the @c SNDCTL_SETNAME ioctl.
3429 * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for
3432 * From Hannu@4Front: "This call is used to change the device name
3433 * reported in /dev/sndstat and ossinfo. So instead of using some generic
3434 * 'OSS loopback audio (MIDI) driver' the device may be given a meaningfull
3435 * name depending on the current context (for example 'OSS virtual wave table
3436 * synth' or 'VoIP link to London')."
3438 * @note As the ioctl definition is still under construction, FreeBSD
3439 * does not currently support SNDCTL_SETNAME.
3441 * @param wrch playback channel (optional; may be NULL)
3442 * @param rdch recording channel (optional; may be NULL)
3443 * @param name new device name gets copied from here
3445 * @retval EINVAL Operation not yet supported.
3448 dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
3452 #endif /* !OSSV4_EXPERIMENT */