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.. */
457 if (i_dev == NULL || td == NULL)
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;
550 /* No matching devtype? */
551 if (fmt == 0 || spd == 0)
552 panic("impossible devtype %d", devtype);
560 * if we get here, the open request is valid- either:
561 * * we were previously not open
562 * * we were open for play xor record and the opener wants
563 * the non-open direction
565 if (DSP_F_READ(flags)) {
567 rderror = pcm_chnalloc(d, &rdch, PCMDIR_REC,
568 td->td_proc->p_pid, td->td_proc->p_comm, rdevunit);
570 if (rderror == 0 && chn_reset(rdch, fmt, spd) != 0)
573 if (volctl == DSP_CDEV_VOLCTL_READ)
578 pcm_chnrelease(rdch);
579 if (!DSP_F_DUPLEX(flags)) {
580 (void)snd_clone_release(i_dev);
581 PCM_RELEASE_QUICK(d);
586 } else if (volctl == DSP_CDEV_VOLCTL_READ) {
589 pcm_chnrelease(rdch);
592 if (flags & O_NONBLOCK)
593 rdch->flags |= CHN_F_NBIO;
595 rdch->flags |= CHN_F_EXCLUSIVE;
597 if (volctl == DSP_CDEV_VOLCTL_NONE)
598 chn_vpc_reset(rdch, SND_VOL_C_PCM, 0);
603 if (DSP_F_WRITE(flags)) {
605 wrerror = pcm_chnalloc(d, &wrch, PCMDIR_PLAY,
606 td->td_proc->p_pid, td->td_proc->p_comm, wdevunit);
608 if (wrerror == 0 && chn_reset(wrch, fmt, spd) != 0)
611 if (volctl == DSP_CDEV_VOLCTL_WRITE)
616 pcm_chnrelease(wrch);
617 if (!DSP_F_DUPLEX(flags)) {
620 * Lock, deref and release previously
621 * created record channel
624 pcm_chnref(rdch, -1);
625 pcm_chnrelease(rdch);
627 (void)snd_clone_release(i_dev);
628 PCM_RELEASE_QUICK(d);
633 } else if (volctl == DSP_CDEV_VOLCTL_WRITE) {
636 pcm_chnrelease(wrch);
639 if (flags & O_NONBLOCK)
640 wrch->flags |= CHN_F_NBIO;
642 wrch->flags |= CHN_F_EXCLUSIVE;
644 if (volctl == DSP_CDEV_VOLCTL_NONE)
645 chn_vpc_reset(wrch, SND_VOL_C_PCM, 0);
654 * We're done. Allocate channels information for this cdev.
657 case DSP_CDEV_VOLCTL_READ:
658 KASSERT(wrch == NULL, ("wrch=%p not null!", wrch));
659 dsp_cdevinfo_alloc(i_dev, NULL, NULL, rdch);
661 case DSP_CDEV_VOLCTL_WRITE:
662 KASSERT(rdch == NULL, ("rdch=%p not null!", rdch));
663 dsp_cdevinfo_alloc(i_dev, NULL, NULL, wrch);
665 case DSP_CDEV_VOLCTL_NONE:
667 if (wrch == NULL && rdch == NULL) {
668 (void)snd_clone_release(i_dev);
678 dsp_cdevinfo_alloc(i_dev, rdch, wrch, NULL);
680 CHN_INSERT_HEAD(d, rdch, channels.pcm.opened);
682 CHN_INSERT_HEAD(d, wrch, channels.pcm.opened);
687 * Increase clone refcount for its automatic garbage collector.
689 (void)snd_clone_ref(i_dev);
700 dsp_close(struct dev_close_args *ap)
702 struct cdev *i_dev = ap->a_head.a_dev;
703 struct pcm_channel *rdch, *wrch, *volch;
704 struct snddev_info *d;
705 int sg_ids, rdref, wdref;
707 d = dsp_get_info(i_dev);
708 if (!DSP_REGISTERED(d, i_dev))
717 rdch = PCM_RDCH(i_dev);
718 wrch = PCM_WRCH(i_dev);
719 volch = PCM_VOLCH(i_dev);
721 PCM_RDCH(i_dev) = NULL;
722 PCM_WRCH(i_dev) = NULL;
723 PCM_VOLCH(i_dev) = NULL;
731 else if (volch == wrch)
735 pcm_chnref(volch, -1);
741 CHN_REMOVE(d, rdch, channels.pcm.opened);
743 CHN_REMOVE(d, wrch, channels.pcm.opened);
745 if (rdch != NULL || wrch != NULL) {
749 * The channel itself need not be locked because:
750 * a) Adding a channel to a syncgroup happens only
751 * in dsp_ioctl(), which cannot run concurrently
753 * b) The syncmember pointer (sm) is protected by
754 * the global syncgroup list lock.
755 * c) A channel can't just disappear, invalidating
756 * pointers, unless it's closed/dereferenced
760 sg_ids = chn_syncdestroy(rdch);
763 free_unr(pcmsg_unrhdr, sg_ids);
766 pcm_chnref(rdch, rdref);
767 chn_abort(rdch); /* won't sleep */
768 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
769 CHN_F_DEAD | CHN_F_EXCLUSIVE);
770 chn_reset(rdch, 0, 0);
771 pcm_chnrelease(rdch);
775 * Please see block above.
778 sg_ids = chn_syncdestroy(wrch);
781 free_unr(pcmsg_unrhdr, sg_ids);
784 pcm_chnref(wrch, wdref);
785 chn_flush(wrch); /* may sleep */
786 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
787 CHN_F_DEAD | CHN_F_EXCLUSIVE);
788 chn_reset(wrch, 0, 0);
789 pcm_chnrelease(wrch);
794 dsp_cdevinfo_free(i_dev);
796 * Release clone busy state and unref it so the automatic
797 * garbage collector will get the hint and do the remaining
800 (void)snd_clone_release(i_dev);
803 * destroy_dev() might sleep, so release pcm lock
804 * here and rely on pcm cv serialization.
807 (void)snd_clone_unref(i_dev);
819 dsp_io_ops(struct cdev *i_dev, struct uio *buf)
821 struct snddev_info *d;
822 struct pcm_channel **ch, *rdch, *wrch;
823 int (*chn_io)(struct pcm_channel *, struct uio *);
827 KASSERT(i_dev != NULL && buf != NULL &&
828 (buf->uio_rw == UIO_READ || buf->uio_rw == UIO_WRITE),
829 ("%s(): io train wreck!", __func__));
831 d = dsp_get_info(i_dev);
832 if (!DSP_REGISTERED(d, i_dev))
837 switch (buf->uio_rw) {
849 panic("invalid/corrupted uio direction: %d", buf->uio_rw);
855 runpid = buf->uio_td->td_proc->p_pid;
857 getchns(i_dev, &rdch, &wrch, prio);
859 if (*ch == NULL || !((*ch)->flags & CHN_F_BUSY)) {
864 if (((*ch)->flags & (CHN_F_MMAP | CHN_F_DEAD)) ||
865 (((*ch)->flags & CHN_F_RUNNING) && (*ch)->pid != runpid)) {
866 relchns(i_dev, rdch, wrch, prio);
869 } else if (!((*ch)->flags & CHN_F_RUNNING)) {
870 (*ch)->flags |= CHN_F_RUNNING;
875 * chn_read/write must give up channel lock in order to copy bytes
876 * from/to userland, so up the "in progress" counter to make sure
877 * someone else doesn't come along and muss up the buffer.
880 ret = chn_io(*ch, buf);
883 CHN_BROADCAST(&(*ch)->cv);
885 relchns(i_dev, rdch, wrch, prio);
893 dsp_read(struct dev_read_args *ap)
895 struct cdev *i_dev = ap->a_head.a_dev;
896 struct uio *buf = ap->a_uio;
898 return (dsp_io_ops(i_dev, buf));
902 dsp_write(struct dev_write_args *ap)
904 struct cdev *i_dev = ap->a_head.a_dev;
905 struct uio *buf = ap->a_uio;
907 return (dsp_io_ops(i_dev, buf));
911 dsp_get_volume_channel(struct cdev *dev, struct pcm_channel **volch)
913 struct snddev_info *d;
914 struct pcm_channel *c;
917 KASSERT(dev != NULL && volch != NULL,
918 ("%s(): NULL query dev=%p volch=%p", __func__, dev, volch));
920 d = dsp_get_info(dev);
921 if (!PCM_REGISTERED(d)) {
932 if (!(c->feederflags & (1 << FEEDER_VOLUME)))
942 unit = dev2unit(dev);
944 CHN_FOREACH(c, d, channels.pcm) {
946 if (c->unit != unit) {
956 return ((c->feederflags & (1 << FEEDER_VOLUME)) ? 0 : -1);
966 dsp_ioctl_channel(struct cdev *dev, struct pcm_channel *volch, u_long cmd,
969 struct snddev_info *d;
970 struct pcm_channel *rdch, *wrch;
973 d = dsp_get_info(dev);
974 if (!PCM_REGISTERED(d) || !(dsp_get_flags(dev) & SD_F_VPC))
981 rdch = PCM_RDCH(dev);
982 wrch = PCM_WRCH(dev);
984 /* No specific channel, look into cache */
986 volch = PCM_VOLCH(dev);
990 if (j == SOUND_MIXER_RECLEV && rdch != NULL)
992 else if (j == SOUND_MIXER_PCM && wrch != NULL)
996 devtype = PCMDEV(dev);
998 /* Look super harder */
1000 (devtype == SND_DEV_DSPHW_PLAY || devtype == SND_DEV_DSPHW_VPLAY ||
1001 devtype == SND_DEV_DSPHW_REC || devtype == SND_DEV_DSPHW_VREC)) {
1002 ret = dsp_get_volume_channel(dev, &volch);
1009 /* Final validation */
1010 if (volch != NULL) {
1012 if (!(volch->feederflags & (1 << FEEDER_VOLUME))) {
1016 if (volch->direction == PCMDIR_PLAY)
1024 if (volch != NULL &&
1025 ((j == SOUND_MIXER_PCM && volch->direction == PCMDIR_PLAY) ||
1026 (j == SOUND_MIXER_RECLEV && volch->direction == PCMDIR_REC))) {
1027 if ((cmd & ~0xff) == MIXER_WRITE(0)) {
1028 int left, right, center;
1030 left = *(int *)arg & 0x7f;
1031 right = ((*(int *)arg) >> 8) & 0x7f;
1032 center = (left + right) >> 1;
1033 chn_setvolume_multi(volch, SND_VOL_C_PCM, left, right,
1035 } else if ((cmd & ~0xff) == MIXER_READ(0)) {
1036 *(int *)arg = CHN_GETVOLUME(volch,
1037 SND_VOL_C_PCM, SND_CHN_T_FL);
1038 *(int *)arg |= CHN_GETVOLUME(volch,
1039 SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
1042 } else if (rdch != NULL || wrch != NULL) {
1044 case SOUND_MIXER_DEVMASK:
1045 case SOUND_MIXER_CAPS:
1046 case SOUND_MIXER_STEREODEVS:
1047 if ((cmd & ~0xff) == MIXER_READ(0)) {
1050 *(int *)arg |= SOUND_MASK_RECLEV;
1052 *(int *)arg |= SOUND_MASK_PCM;
1056 case SOUND_MIXER_RECMASK:
1057 case SOUND_MIXER_RECSRC:
1058 if ((cmd & ~0xff) == MIXER_READ(0))
1074 dsp_ioctl(struct dev_ioctl_args *ap)
1076 struct cdev *i_dev = ap->a_head.a_dev;
1077 u_long cmd = ap->a_cmd;
1078 caddr_t arg = ap->a_data;
1079 struct pcm_channel *chn, *rdch, *wrch;
1080 struct snddev_info *d;
1082 int *arg_i, ret, tmp;
1084 d = dsp_get_info(i_dev);
1085 if (!DSP_REGISTERED(d, i_dev))
1095 if (IOCGROUP(cmd) == 'M') {
1096 if (cmd == OSS_GETVERSION) {
1097 *arg_i = SOUND_VERSION;
1101 ret = dsp_ioctl_channel(i_dev, PCM_VOLCH(i_dev), cmd, arg);
1107 if (d->mixer_dev != NULL) {
1108 PCM_ACQUIRE_QUICK(d);
1109 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1111 PCM_RELEASE_QUICK(d);
1121 * Certain ioctls may be made on any type of device (audio, mixer,
1122 * and MIDI). Handle those special cases here.
1124 if (IOCGROUP(cmd) == 'X') {
1125 PCM_ACQUIRE_QUICK(d);
1127 case SNDCTL_SYSINFO:
1128 sound_oss_sysinfo((oss_sysinfo *)arg);
1130 case SNDCTL_CARDINFO:
1131 ret = sound_oss_card_info((oss_card_info *)arg);
1133 case SNDCTL_AUDIOINFO:
1134 case SNDCTL_AUDIOINFO_EX:
1135 case SNDCTL_ENGINEINFO:
1136 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
1138 case SNDCTL_MIXERINFO:
1139 ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
1144 PCM_RELEASE_QUICK(d);
1149 getchns(i_dev, &rdch, &wrch, 0);
1151 if (wrch != NULL && (wrch->flags & CHN_F_DEAD))
1153 if (rdch != NULL && (rdch->flags & CHN_F_DEAD))
1156 if (wrch == NULL && rdch == NULL) {
1164 * we start with the new ioctl interface.
1166 case AIONWRITE: /* how many bytes can write ? */
1170 if (wrch && wrch->bufhard.dl)
1171 while (chn_wrfeed(wrch) == 0);
1173 *arg_i = sndbuf_getfree(wrch->bufsoft);
1181 case AIOSSIZE: /* set the current blocksize */
1183 struct snd_size *p = (struct snd_size *)arg;
1187 PCM_ACQUIRE_QUICK(d);
1190 chn_setblocksize(wrch, 2, p->play_size);
1191 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1196 chn_setblocksize(rdch, 2, p->rec_size);
1197 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1200 PCM_RELEASE_QUICK(d);
1203 case AIOGSIZE: /* get the current blocksize */
1205 struct snd_size *p = (struct snd_size *)arg;
1209 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1214 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1223 snd_chan_param *p = (snd_chan_param *)arg;
1225 if (cmd == AIOSFMT &&
1226 ((p->play_format != 0 && p->play_rate == 0) ||
1227 (p->rec_format != 0 && p->rec_rate == 0))) {
1231 PCM_ACQUIRE_QUICK(d);
1234 if (cmd == AIOSFMT && p->play_format != 0) {
1236 SND_FORMAT(p->play_format,
1237 AFMT_CHANNEL(wrch->format),
1238 AFMT_EXTCHANNEL(wrch->format)));
1239 chn_setspeed(wrch, p->play_rate);
1241 p->play_rate = wrch->speed;
1242 p->play_format = AFMT_ENCODING(wrch->format);
1250 if (cmd == AIOSFMT && p->rec_format != 0) {
1252 SND_FORMAT(p->rec_format,
1253 AFMT_CHANNEL(rdch->format),
1254 AFMT_EXTCHANNEL(rdch->format)));
1255 chn_setspeed(rdch, p->rec_rate);
1257 p->rec_rate = rdch->speed;
1258 p->rec_format = AFMT_ENCODING(rdch->format);
1264 PCM_RELEASE_QUICK(d);
1268 case AIOGCAP: /* get capabilities */
1270 snd_capabilities *p = (snd_capabilities *)arg;
1271 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
1277 rcaps = chn_getcaps(rdch);
1281 pcaps = chn_getcaps(wrch);
1283 p->rate_min = max(rcaps? rcaps->minspeed : 0,
1284 pcaps? pcaps->minspeed : 0);
1285 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
1286 pcaps? pcaps->maxspeed : 1000000);
1287 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
1288 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
1289 /* XXX bad on sb16 */
1290 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
1291 (wrch? chn_getformats(wrch) : 0xffffffff);
1293 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
1294 pdev = d->mixer_dev;
1295 p->mixers = 1; /* default: one mixer */
1296 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
1297 p->left = p->right = 100;
1307 if (*arg_i == AIOSYNC_PLAY && wrch) {
1309 *arg_i = chn_abort(wrch);
1311 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
1313 *arg_i = chn_abort(rdch);
1316 kprintf("AIOSTOP: bad channel 0x%x\n", *arg_i);
1322 kprintf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
1323 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
1327 * here follow the standard ioctls (filio.h etc.)
1329 case FIONREAD: /* get # bytes to read */
1332 /* if (rdch && rdch->bufhard.dl)
1333 while (chn_rdfeed(rdch) == 0);
1335 *arg_i = sndbuf_getready(rdch->bufsoft);
1343 case FIOASYNC: /*set/clear async i/o */
1344 DEB( kprintf("FIOASYNC\n") ; )
1347 case SNDCTL_DSP_NONBLOCK: /* set non-blocking i/o */
1348 case FIONBIO: /* set/clear non-blocking i/o */
1351 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1352 rdch->flags |= CHN_F_NBIO;
1354 rdch->flags &= ~CHN_F_NBIO;
1359 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1360 wrch->flags |= CHN_F_NBIO;
1362 wrch->flags &= ~CHN_F_NBIO;
1368 * Finally, here is the linux-compatible ioctl interface
1370 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
1371 case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
1372 case SNDCTL_DSP_GETBLKSIZE:
1373 chn = wrch ? wrch : rdch;
1376 *arg_i = sndbuf_getblksz(chn->bufsoft);
1384 case SNDCTL_DSP_SETBLKSIZE:
1385 RANGE(*arg_i, 16, 65536);
1386 PCM_ACQUIRE_QUICK(d);
1389 chn_setblocksize(wrch, 2, *arg_i);
1394 chn_setblocksize(rdch, 2, *arg_i);
1397 PCM_RELEASE_QUICK(d);
1400 case SNDCTL_DSP_RESET:
1401 DEB(kprintf("dsp reset\n"));
1416 case SNDCTL_DSP_SYNC:
1417 DEB(kprintf("dsp sync\n"));
1418 /* chn_sync may sleep */
1426 case SNDCTL_DSP_SPEED:
1427 /* chn_setspeed may sleep */
1429 PCM_ACQUIRE_QUICK(d);
1432 ret = chn_setspeed(wrch, *arg_i);
1436 if (rdch && ret == 0) {
1438 ret = chn_setspeed(rdch, *arg_i);
1443 PCM_RELEASE_QUICK(d);
1447 case SOUND_PCM_READ_RATE:
1448 chn = wrch ? wrch : rdch;
1451 *arg_i = chn->speed;
1459 case SNDCTL_DSP_STEREO:
1461 *arg_i = (*arg_i)? 2 : 1;
1462 PCM_ACQUIRE_QUICK(d);
1465 ret = chn_setformat(wrch,
1466 SND_FORMAT(wrch->format, *arg_i, 0));
1467 tmp = (AFMT_CHANNEL(wrch->format) > 1)? 1 : 0;
1470 if (rdch && ret == 0) {
1472 ret = chn_setformat(rdch,
1473 SND_FORMAT(rdch->format, *arg_i, 0));
1475 tmp = (AFMT_CHANNEL(rdch->format) > 1)? 1 : 0;
1478 PCM_RELEASE_QUICK(d);
1482 case SOUND_PCM_WRITE_CHANNELS:
1483 /* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
1490 struct pcmchan_matrix *m;
1494 if (*arg_i > SND_CHN_MAX)
1495 *arg_i = SND_CHN_MAX;
1497 m = feeder_matrix_default_channel_map(*arg_i);
1503 PCM_ACQUIRE_QUICK(d);
1506 ret = chn_setformat(wrch,
1507 SND_FORMAT(wrch->format, *arg_i, ext));
1508 tmp = AFMT_CHANNEL(wrch->format);
1511 if (rdch && ret == 0) {
1513 ret = chn_setformat(rdch,
1514 SND_FORMAT(rdch->format, *arg_i, ext));
1516 tmp = AFMT_CHANNEL(rdch->format);
1519 PCM_RELEASE_QUICK(d);
1522 chn = wrch ? wrch : rdch;
1524 *arg_i = AFMT_CHANNEL(chn->format);
1529 case SOUND_PCM_READ_CHANNELS:
1530 chn = wrch ? wrch : rdch;
1533 *arg_i = AFMT_CHANNEL(chn->format);
1541 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
1542 chn = wrch ? wrch : rdch;
1545 *arg_i = chn_getformats(chn);
1553 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
1554 if (*arg_i != AFMT_QUERY) {
1556 PCM_ACQUIRE_QUICK(d);
1559 ret = chn_setformat(wrch, SND_FORMAT(*arg_i,
1560 AFMT_CHANNEL(wrch->format),
1561 AFMT_EXTCHANNEL(wrch->format)));
1565 if (rdch && ret == 0) {
1567 ret = chn_setformat(rdch, SND_FORMAT(*arg_i,
1568 AFMT_CHANNEL(rdch->format),
1569 AFMT_EXTCHANNEL(rdch->format)));
1574 PCM_RELEASE_QUICK(d);
1575 *arg_i = AFMT_ENCODING(tmp);
1577 chn = wrch ? wrch : rdch;
1579 *arg_i = AFMT_ENCODING(chn->format);
1584 case SNDCTL_DSP_SETFRAGMENT:
1585 DEB(kprintf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
1587 uint32_t fragln = (*arg_i) & 0x0000ffff;
1588 uint32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
1590 uint32_t r_maxfrags, r_fragsz;
1592 RANGE(fragln, 4, 16);
1593 fragsz = 1 << fragln;
1596 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1599 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
1600 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1602 DEB(kprintf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
1603 PCM_ACQUIRE_QUICK(d);
1606 ret = chn_setblocksize(rdch, maxfrags, fragsz);
1607 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
1608 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
1611 r_maxfrags = maxfrags;
1614 if (wrch && ret == 0) {
1616 ret = chn_setblocksize(wrch, maxfrags, fragsz);
1617 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
1618 fragsz = sndbuf_getblksz(wrch->bufsoft);
1620 } else { /* use whatever came from the read channel */
1621 maxfrags = r_maxfrags;
1624 PCM_RELEASE_QUICK(d);
1627 while (fragsz > 1) {
1631 *arg_i = (maxfrags << 16) | fragln;
1635 case SNDCTL_DSP_GETISPACE:
1636 /* return the size of data available in the input queue */
1638 audio_buf_info *a = (audio_buf_info *)arg;
1640 struct snd_dbuf *bs = rdch->bufsoft;
1643 a->bytes = sndbuf_getready(bs);
1644 a->fragments = a->bytes / sndbuf_getblksz(bs);
1645 a->fragstotal = sndbuf_getblkcnt(bs);
1646 a->fragsize = sndbuf_getblksz(bs);
1653 case SNDCTL_DSP_GETOSPACE:
1654 /* return space available in the output queue */
1656 audio_buf_info *a = (audio_buf_info *)arg;
1658 struct snd_dbuf *bs = wrch->bufsoft;
1661 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1662 a->bytes = sndbuf_getfree(bs);
1663 a->fragments = a->bytes / sndbuf_getblksz(bs);
1664 a->fragstotal = sndbuf_getblkcnt(bs);
1665 a->fragsize = sndbuf_getblksz(bs);
1672 case SNDCTL_DSP_GETIPTR:
1674 count_info *a = (count_info *)arg;
1676 struct snd_dbuf *bs = rdch->bufsoft;
1679 /* XXX abusive DMA update: chn_rdupdate(rdch); */
1680 a->bytes = sndbuf_gettotal(bs);
1681 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
1682 a->ptr = sndbuf_getfreeptr(bs);
1683 rdch->blocks = sndbuf_getblocks(bs);
1690 case SNDCTL_DSP_GETOPTR:
1692 count_info *a = (count_info *)arg;
1694 struct snd_dbuf *bs = wrch->bufsoft;
1697 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1698 a->bytes = sndbuf_gettotal(bs);
1699 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
1700 a->ptr = sndbuf_getreadyptr(bs);
1701 wrch->blocks = sndbuf_getblocks(bs);
1708 case SNDCTL_DSP_GETCAPS:
1710 *arg_i = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER;
1711 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1712 *arg_i |= PCM_CAP_DUPLEX;
1716 case SOUND_PCM_READ_BITS:
1717 chn = wrch ? wrch : rdch;
1720 if (chn->format & AFMT_8BIT)
1722 else if (chn->format & AFMT_16BIT)
1724 else if (chn->format & AFMT_24BIT)
1726 else if (chn->format & AFMT_32BIT)
1737 case SNDCTL_DSP_SETTRIGGER:
1740 rdch->flags &= ~CHN_F_NOTRIGGER;
1741 if (*arg_i & PCM_ENABLE_INPUT)
1746 rdch->flags |= CHN_F_NOTRIGGER;
1752 wrch->flags &= ~CHN_F_NOTRIGGER;
1753 if (*arg_i & PCM_ENABLE_OUTPUT)
1758 wrch->flags |= CHN_F_NOTRIGGER;
1764 case SNDCTL_DSP_GETTRIGGER:
1768 if (wrch->flags & CHN_F_TRIGGERED)
1769 *arg_i |= PCM_ENABLE_OUTPUT;
1774 if (rdch->flags & CHN_F_TRIGGERED)
1775 *arg_i |= PCM_ENABLE_INPUT;
1780 case SNDCTL_DSP_GETODELAY:
1782 struct snd_dbuf *bs = wrch->bufsoft;
1785 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1786 *arg_i = sndbuf_getready(bs);
1792 case SNDCTL_DSP_POST:
1795 wrch->flags &= ~CHN_F_NOTRIGGER;
1801 case SNDCTL_DSP_SETDUPLEX:
1803 * switch to full-duplex mode if card is in half-duplex
1804 * mode and is able to work in full-duplex mode
1807 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1808 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1813 * The following four ioctls are simple wrappers around mixer_ioctl
1814 * with no further processing. xcmd is short for "translated
1817 case SNDCTL_DSP_GETRECVOL:
1819 xcmd = SOUND_MIXER_READ_RECLEV;
1823 case SNDCTL_DSP_SETRECVOL:
1825 xcmd = SOUND_MIXER_WRITE_RECLEV;
1829 case SNDCTL_DSP_GETPLAYVOL:
1831 xcmd = SOUND_MIXER_READ_PCM;
1835 case SNDCTL_DSP_SETPLAYVOL:
1837 xcmd = SOUND_MIXER_WRITE_PCM;
1841 ret = dsp_ioctl_channel(i_dev, chn, xcmd, arg);
1847 if (d->mixer_dev != NULL) {
1848 PCM_ACQUIRE_QUICK(d);
1849 ret = mixer_ioctl_cmd(d->mixer_dev, xcmd, arg, -1, td,
1851 PCM_RELEASE_QUICK(d);
1857 case SNDCTL_DSP_GET_RECSRC_NAMES:
1858 case SNDCTL_DSP_GET_RECSRC:
1859 case SNDCTL_DSP_SET_RECSRC:
1860 if (d->mixer_dev != NULL) {
1861 PCM_ACQUIRE_QUICK(d);
1862 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1864 PCM_RELEASE_QUICK(d);
1870 * The following 3 ioctls aren't very useful at the moment. For
1871 * now, only a single channel is associated with a cdev (/dev/dspN
1872 * instance), so there's only a single output routing to use (i.e.,
1873 * the wrch bound to this cdev).
1875 case SNDCTL_DSP_GET_PLAYTGT_NAMES:
1877 oss_mixer_enuminfo *ei;
1878 ei = (oss_mixer_enuminfo *)arg;
1881 ei->version = 0; /* static for now */
1882 ei->strindex[0] = 0;
1886 strlcpy(ei->strings, wrch->name,
1887 sizeof(ei->strings));
1890 ei->strings[0] = '\0';
1894 case SNDCTL_DSP_GET_PLAYTGT:
1895 case SNDCTL_DSP_SET_PLAYTGT: /* yes, they are the same for now */
1898 * OSSv4: "The value that was accepted by the device will
1899 * be returned back in the variable pointed by the
1908 case SNDCTL_DSP_SILENCE:
1910 * Flush the software (pre-feed) buffer, but try to minimize playback
1911 * interruption. (I.e., record unplayed samples with intent to
1912 * restore by SNDCTL_DSP_SKIP.) Intended for application "pause"
1918 struct snd_dbuf *bs;
1920 while (wrch->inprog != 0)
1921 cv_wait(&wrch->cv, wrch->lock);
1923 if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) {
1924 bs->sl = sndbuf_getready(bs);
1925 sndbuf_dispose(bs, bs->shadbuf, sndbuf_getready(bs));
1926 sndbuf_fillsilence(bs);
1933 case SNDCTL_DSP_SKIP:
1935 * OSSv4 docs: "This ioctl call discards all unplayed samples in the
1936 * playback buffer by moving the current write position immediately
1937 * before the point where the device is currently reading the samples."
1942 struct snd_dbuf *bs;
1944 while (wrch->inprog != 0)
1945 cv_wait(&wrch->cv, wrch->lock);
1947 if ((bs->shadbuf != NULL) && (bs->sl > 0)) {
1948 sndbuf_softreset(bs);
1949 sndbuf_acquire(bs, bs->shadbuf, bs->sl);
1957 case SNDCTL_DSP_CURRENT_OPTR:
1958 case SNDCTL_DSP_CURRENT_IPTR:
1960 * @note Changing formats resets the buffer counters, which differs
1961 * from the 4Front drivers. However, I don't expect this to be
1962 * much of a problem.
1964 * @note In a test where @c CURRENT_OPTR is called immediately after write
1965 * returns, this driver is about 32K samples behind whereas
1966 * 4Front's is about 8K samples behind. Should determine source
1967 * of discrepancy, even if only out of curiosity.
1969 * @todo Actually test SNDCTL_DSP_CURRENT_IPTR.
1971 chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch;
1975 struct snd_dbuf *bs;
1978 oss_count_t *oc = (oss_count_t *)arg;
1983 tmp = (sndbuf_getsize(b) + chn_getptr(chn) - sndbuf_gethwptr(b)) % sndbuf_getsize(b);
1984 oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getalign(b);
1985 oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getalign(b);
1987 oc->samples = sndbuf_gettotal(bs) / sndbuf_getalign(bs);
1988 oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getalign(bs);
1994 case SNDCTL_DSP_HALT_OUTPUT:
1995 case SNDCTL_DSP_HALT_INPUT:
1996 chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch;
2006 case SNDCTL_DSP_LOW_WATER:
2008 * Set the number of bytes required to attract attention by
2013 wrch->lw = (*arg_i > 1) ? *arg_i : 1;
2018 rdch->lw = (*arg_i > 1) ? *arg_i : 1;
2023 case SNDCTL_DSP_GETERROR:
2025 * OSSv4 docs: "All errors and counters will automatically be
2026 * cleared to zeroes after the call so each call will return only
2027 * the errors that occurred after the previous invocation. ... The
2028 * play_underruns and rec_overrun fields are the only useful fields
2029 * returned by OSS 4.0."
2032 audio_errinfo *ei = (audio_errinfo *)arg;
2034 bzero((void *)ei, sizeof(*ei));
2038 ei->play_underruns = wrch->xruns;
2044 ei->rec_overruns = rdch->xruns;
2051 case SNDCTL_DSP_SYNCGROUP:
2052 PCM_ACQUIRE_QUICK(d);
2053 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
2054 PCM_RELEASE_QUICK(d);
2057 case SNDCTL_DSP_SYNCSTART:
2058 PCM_ACQUIRE_QUICK(d);
2059 ret = dsp_oss_syncstart(*arg_i);
2060 PCM_RELEASE_QUICK(d);
2063 case SNDCTL_DSP_POLICY:
2064 PCM_ACQUIRE_QUICK(d);
2065 ret = dsp_oss_policy(wrch, rdch, *arg_i);
2066 PCM_RELEASE_QUICK(d);
2069 case SNDCTL_DSP_COOKEDMODE:
2070 PCM_ACQUIRE_QUICK(d);
2071 if (!(dsp_get_flags(i_dev) & SD_F_BITPERFECT))
2072 ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
2073 PCM_RELEASE_QUICK(d);
2075 case SNDCTL_DSP_GET_CHNORDER:
2076 PCM_ACQUIRE_QUICK(d);
2077 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
2078 PCM_RELEASE_QUICK(d);
2080 case SNDCTL_DSP_SET_CHNORDER:
2081 PCM_ACQUIRE_QUICK(d);
2082 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
2083 PCM_RELEASE_QUICK(d);
2085 case SNDCTL_DSP_GETCHANNELMASK: /* XXX vlc */
2086 PCM_ACQUIRE_QUICK(d);
2087 ret = dsp_oss_getchannelmask(wrch, rdch, (int *)arg);
2088 PCM_RELEASE_QUICK(d);
2090 case SNDCTL_DSP_BIND_CHANNEL: /* XXX what?!? */
2093 #ifdef OSSV4_EXPERIMENT
2095 * XXX The following ioctls are not yet supported and just return
2098 case SNDCTL_DSP_GETOPEAKS:
2099 case SNDCTL_DSP_GETIPEAKS:
2100 chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch;
2104 oss_peaks_t *op = (oss_peaks_t *)arg;
2108 ret = chn_getpeaks(chn, &lpeak, &rpeak);
2120 * XXX Once implemented, revisit this for proper cv protection
2123 case SNDCTL_GETLABEL:
2124 ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg);
2126 case SNDCTL_SETLABEL:
2127 ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg);
2129 case SNDCTL_GETSONG:
2130 ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg);
2132 case SNDCTL_SETSONG:
2133 ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg);
2135 case SNDCTL_SETNAME:
2136 ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg);
2140 * @note The S/PDIF interface ioctls, @c SNDCTL_DSP_READCTL and
2141 * @c SNDCTL_DSP_WRITECTL have been omitted at the suggestion of
2142 * 4Front Technologies.
2144 case SNDCTL_DSP_READCTL:
2145 case SNDCTL_DSP_WRITECTL:
2148 #endif /* !0 (explicitly omitted ioctls) */
2150 #endif /* !OSSV4_EXPERIMENT */
2151 case SNDCTL_DSP_MAPINBUF:
2152 case SNDCTL_DSP_MAPOUTBUF:
2153 case SNDCTL_DSP_SETSYNCRO:
2156 case SNDCTL_DSP_SUBDIVIDE:
2157 case SOUND_PCM_WRITE_FILTER:
2158 case SOUND_PCM_READ_FILTER:
2159 /* dunno what these do, don't sound important */
2162 DEB(kprintf("default ioctl fn 0x%08lx fail\n", cmd));
2172 static struct filterops dsp_read_filtops =
2173 { FILTEROP_ISFD, NULL, dsp_filter_detach, dsp_filter_read };
2174 static struct filterops dsp_write_filtops =
2175 { FILTEROP_ISFD, NULL, dsp_filter_detach, dsp_filter_write };
2178 /*dsp_poll(struct cdev *i_dev, int events, struct thread *td)*/
2179 dsp_kqfilter(struct dev_kqfilter_args *ap)
2181 struct knote *kn = ap->a_kn;
2182 struct klist *klist;
2183 struct cdev *i_dev = ap->a_head.a_dev;
2184 struct snddev_info *d;
2185 struct pcm_channel *wrch, *rdch;
2186 struct snd_dbuf *bs = NULL;
2189 d = dsp_get_info(i_dev);
2190 if (!DSP_REGISTERED(d, i_dev))
2199 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2201 switch (kn->kn_filter) {
2204 kn->kn_fop = &dsp_read_filtops;
2205 kn->kn_hook = (caddr_t)rdch;
2212 kn->kn_fop = &dsp_write_filtops;
2213 kn->kn_hook = (caddr_t)wrch;
2219 ap->a_result = EOPNOTSUPP;
2223 if (ap->a_result == 0) {
2224 klist = &sndbuf_getkq(bs)->ki_note;
2225 knote_insert(klist, kn);
2228 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2236 dsp_filter_detach(struct knote *kn)
2238 struct pcm_channel *ch = (struct pcm_channel *)kn->kn_hook;
2239 struct snd_dbuf *bs = ch->bufsoft;
2240 struct klist *klist;
2243 klist = &sndbuf_getkq(bs)->ki_note;
2244 knote_remove(klist, kn);
2249 dsp_filter_read(struct knote *kn, long hint)
2251 struct pcm_channel *rdch = (struct pcm_channel *)kn->kn_hook;
2252 struct thread *td = curthread;
2256 ready = chn_poll(rdch, 1, td);
2263 dsp_filter_write(struct knote *kn, long hint)
2265 struct pcm_channel *wrch = (struct pcm_channel *)kn->kn_hook;
2266 struct thread *td = curthread;
2270 ready = chn_poll(wrch, 1, td);
2277 dsp_mmap(struct dev_mmap_args *ap)
2279 vm_offset_t offset = ap->a_offset;
2281 /* XXX memattr is not honored */
2282 ap->a_result = vtophys(offset);
2287 dsp_mmap_single(struct dev_mmap_single_args *ap)
2289 struct cdev *i_dev = ap->a_head.a_dev;
2290 vm_ooffset_t *offset = ap->a_offset;
2291 vm_size_t size = ap->a_size;
2292 struct vm_object **object = ap->a_object;
2293 int nprot = ap->a_nprot;
2294 struct snddev_info *d;
2295 struct pcm_channel *wrch, *rdch, *c;
2298 * Reject PROT_EXEC by default. It just doesn't makes sense.
2299 * Unfortunately, we have to give up this one due to linux_mmap
2302 * http://lists.freebsd.org/pipermail/freebsd-emulation/2007-June/003698.html
2306 if ((nprot & PROT_EXEC) && (dsp_mmap_allow_prot_exec < 0 ||
2307 (dsp_mmap_allow_prot_exec == 0 &&
2308 SV_CURPROC_ABI() != SV_ABI_LINUX)))
2310 if ((nprot & PROT_EXEC) && dsp_mmap_allow_prot_exec < 1)
2315 * PROT_READ (alone) selects the input buffer.
2316 * PROT_WRITE (alone) selects the output buffer.
2317 * PROT_WRITE|PROT_READ together select the output buffer.
2319 if ((nprot & (PROT_READ | PROT_WRITE)) == 0)
2322 d = dsp_get_info(i_dev);
2323 if (!DSP_REGISTERED(d, i_dev))
2328 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2330 c = ((nprot & PROT_WRITE) != 0) ? wrch : rdch;
2331 if (c == NULL || (c->flags & CHN_F_MMAP_INVALID) ||
2332 (*offset + size) > sndbuf_getsize(c->bufsoft) ||
2333 (wrch != NULL && (wrch->flags & CHN_F_MMAP_INVALID)) ||
2334 (rdch != NULL && (rdch->flags & CHN_F_MMAP_INVALID))) {
2335 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2341 wrch->flags |= CHN_F_MMAP;
2343 rdch->flags |= CHN_F_MMAP;
2345 *offset = (uintptr_t)sndbuf_getbufofs(c->bufsoft, *offset);
2346 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2347 *object = dev_pager_alloc(i_dev, size, nprot, *offset);
2351 if (*object == NULL)
2356 /* So much for dev_stdclone() */
2358 dsp_stdclone(const char *name, char *namep, char *sep, int use_sep, int *u, int *c)
2362 len = strlen(namep);
2364 if (bcmp(name, namep, len) != 0)
2369 if (isdigit(*name) == 0)
2374 if (*name == '0' && !(name[1] == '\0' || bcmp(name + 1, sep, len) == 0))
2377 for (*u = 0; isdigit(*name) != 0; name++) {
2385 return ((use_sep == 0) ? 0 : ENODEV);
2387 if (bcmp(name, sep, len) != 0 || isdigit(name[len]) == 0)
2392 if (*name == '0' && name[1] != '\0')
2395 for (*c = 0; isdigit(*name) != 0; name++) {
2409 dsp_clone(void *arg,
2410 char *name, int namelen, struct cdev **dev)
2412 struct snddev_info *d;
2413 struct snd_clone_entry *ce;
2414 struct pcm_channel *c;
2415 int i, unit, udcmask, cunit, devtype, devhw, devcmax, tumax;
2416 char *devname, *devcmp, *devsep;
2418 KASSERT(dsp_umax >= 0 && dsp_cmax >= 0, ("Uninitialized unit!"));
2432 for (i = 0; unit == -1 &&
2433 i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2434 devtype = dsp_cdevs[i].type;
2435 devcmp = dsp_cdevs[i].name;
2436 devsep = dsp_cdevs[i].sep;
2437 devname = dsp_cdevs[i].alias;
2438 if (devname == NULL)
2440 devhw = dsp_cdevs[i].hw;
2441 devcmax = dsp_cdevs[i].max - 1;
2442 if (strcmp(name, devcmp) == 0)
2444 else if (dsp_stdclone(name, devcmp, devsep,
2445 dsp_cdevs[i].use_sep, &unit, &cunit) != 0) {
2451 d = devclass_get_softc(pcm_devclass, unit);
2452 if (!PCM_REGISTERED(d) || d->clones == NULL)
2455 /* XXX Need Giant magic entry ??? */
2458 if (snd_clone_disabled(d->clones)) {
2467 udcmask = snd_u2unit(unit) | snd_d2unit(devtype);
2470 KASSERT(devcmax <= dsp_cmax,
2471 ("overflow: devcmax=%d, dsp_cmax=%d", devcmax, dsp_cmax));
2472 if (cunit > devcmax) {
2473 PCM_RELEASE_QUICK(d);
2476 udcmask |= snd_c2unit(cunit);
2477 CHN_FOREACH(c, d, channels.pcm) {
2479 if (c->unit != udcmask) {
2484 udcmask &= ~snd_c2unit(cunit);
2486 * Temporarily increase clone maxunit to overcome
2487 * vchan flexibility.
2489 * # sysctl dev.pcm.0.play.vchans=256
2490 * dev.pcm.0.play.vchans: 1 -> 256
2491 * # cat /dev/zero > /dev/dsp0.vp255 &
2493 * # sysctl dev.pcm.0.play.vchans=0
2494 * dev.pcm.0.play.vchans: 256 -> 1
2496 * [1] + running cat /dev/zero > /dev/dsp0.vp255
2498 * # cat /dev/zero > /dev/dsp0.vp255
2499 * zsh: operation not supported: /dev/dsp0.vp255
2501 tumax = snd_clone_getmaxunit(d->clones);
2503 snd_clone_setmaxunit(d->clones, cunit);
2506 goto dsp_clone_alloc;
2509 * Ok, so we're requesting unallocated vchan, but still
2510 * within maximum vchan limit.
2512 if (((devtype == SND_DEV_DSPHW_VPLAY && d->pvchancount > 0) ||
2513 (devtype == SND_DEV_DSPHW_VREC && d->rvchancount > 0)) &&
2514 cunit < snd_maxautovchans) {
2515 udcmask &= ~snd_c2unit(cunit);
2516 tumax = snd_clone_getmaxunit(d->clones);
2518 snd_clone_setmaxunit(d->clones, cunit);
2521 goto dsp_clone_alloc;
2523 PCM_RELEASE_QUICK(d);
2528 ce = snd_clone_alloc(d->clones, dev, &cunit, udcmask);
2530 snd_clone_setmaxunit(d->clones, tumax);
2532 udcmask |= snd_c2unit(cunit);
2533 dev = make_only_dev(&dsp_ops, PCMMINOR(udcmask),
2534 UID_ROOT, GID_WHEEL, 0666, "%s%d%s%d",
2535 devname, unit, devsep, cunit);
2536 snd_clone_register(ce, *dev);
2539 PCM_RELEASE_QUICK(d);
2548 dsp_sysinit(void *p)
2550 if (dsp_ehtag != NULL)
2552 /* initialize unit numbering */
2554 dsp_umax = PCMMAXUNIT;
2555 dsp_cmax = PCMMAXCHAN;
2556 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
2560 dsp_sysuninit(void *p)
2562 if (dsp_ehtag == NULL)
2564 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
2568 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
2569 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
2572 dsp_unit2name(char *buf, size_t len, int unit)
2576 KASSERT(buf != NULL && len != 0,
2577 ("bogus buf=%p len=%ju", buf, (uintmax_t)len));
2579 dtype = snd_unit2d(unit);
2581 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2582 if (dtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
2584 ksnprintf(buf, len, "%s%d%s%d", dsp_cdevs[i].name,
2585 snd_unit2u(unit), dsp_cdevs[i].sep, snd_unit2c(unit));
2593 * @brief Handler for SNDCTL_AUDIOINFO.
2595 * Gathers information about the audio device specified in ai->dev. If
2596 * ai->dev == -1, then this function gathers information about the current
2597 * device. If the call comes in on a non-audio device and ai->dev == -1,
2600 * This routine is supposed to go practically straight to the hardware,
2601 * getting capabilities directly from the sound card driver, side-stepping
2602 * the intermediate channel interface.
2604 * Note, however, that the usefulness of this command is significantly
2605 * decreased when requesting info about any device other than the one serving
2606 * the request. While each snddev_channel refers to a specific device node,
2607 * the converse is *not* true. Currently, when a sound device node is opened,
2608 * the sound subsystem scans for an available audio channel (or channels, if
2609 * opened in read+write) and then assigns them to the si_drv[12] private
2610 * data fields. As a result, any information returned linking a channel to
2611 * a specific character device isn't necessarily accurate.
2614 * Calling threads must not hold any snddev_info or pcm_channel locks.
2616 * @param dev device on which the ioctl was issued
2617 * @param ai ioctl request data container
2620 * @retval EINVAL ai->dev specifies an invalid device
2622 * @todo Verify correctness of Doxygen tags. ;)
2625 dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
2627 struct pcmchan_caps *caps;
2628 struct pcm_channel *ch;
2629 struct snddev_info *d;
2631 int i, nchan, *rates, minch, maxch;
2632 char *devname, buf[CHN_NAMELEN];
2635 * If probing the device that received the ioctl, make sure it's a
2636 * DSP device. (Users may use this ioctl with /dev/mixer and
2639 if (ai->dev == -1 && i_dev->si_ops != &dsp_ops)
2645 bzero(buf, sizeof(buf));
2648 * Search for the requested audio device (channel). Start by
2649 * iterating over pcm devices.
2651 for (i = 0; pcm_devclass != NULL &&
2652 i < devclass_get_maxunit(pcm_devclass); i++) {
2653 d = devclass_get_softc(pcm_devclass, i);
2654 if (!PCM_REGISTERED(d))
2657 /* XXX Need Giant magic entry ??? */
2659 /* See the note in function docblock */
2660 PCM_UNLOCKASSERT(d);
2663 CHN_FOREACH(ch, d, channels.pcm) {
2664 CHN_UNLOCKASSERT(ch);
2666 if (ai->dev == -1) {
2667 if (DSP_REGISTERED(d, i_dev) &&
2668 (ch == PCM_RDCH(i_dev) || /* record ch */
2669 ch == PCM_WRCH(i_dev))) { /* playback ch */
2670 devname = dsp_unit2name(buf,
2671 sizeof(buf), ch->unit);
2673 } else if (ai->dev == nchan) {
2674 devname = dsp_unit2name(buf, sizeof(buf),
2677 if (devname != NULL)
2683 if (devname != NULL) {
2685 * At this point, the following synchronization stuff
2687 * - a specific PCM device is locked.
2688 * - a specific audio channel has been locked, so be
2689 * sure to unlock when exiting;
2692 caps = chn_getcaps(ch);
2695 * With all handles collected, zero out the user's
2696 * container and begin filling in its fields.
2698 bzero((void *)ai, sizeof(oss_audioinfo));
2701 strlcpy(ai->name, ch->name, sizeof(ai->name));
2703 if ((ch->flags & CHN_F_BUSY) == 0)
2706 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
2710 * @c cmd - OSSv4 docs: "Only supported under Linux at
2711 * this moment." Cop-out, I know, but I'll save
2712 * running around in the process table for later.
2713 * Is there a risk of leaking information?
2718 * These flags stolen from SNDCTL_DSP_GETCAPS handler.
2719 * Note, however, that a single channel operates in
2720 * only one direction, so PCM_CAP_DUPLEX is out.
2723 * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep
2724 * these in pcmchan::caps?
2726 ai->caps = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER |
2727 ((ch->direction == PCMDIR_PLAY) ? PCM_CAP_OUTPUT : PCM_CAP_INPUT);
2730 * Collect formats supported @b natively by the
2731 * device. Also determine min/max channels. (I.e.,
2732 * mono, stereo, or both?)
2734 * If any channel is stereo, maxch = 2;
2735 * if all channels are stereo, minch = 2, too;
2736 * if any channel is mono, minch = 1;
2737 * and if all channels are mono, maxch = 1.
2742 for (i = 0; caps->fmtlist[i]; i++) {
2743 fmts |= caps->fmtlist[i];
2744 if (AFMT_CHANNEL(caps->fmtlist[i]) > 1) {
2745 minch = (minch == 0) ? 2 : minch;
2749 maxch = (maxch == 0) ? 1 : maxch;
2753 if (ch->direction == PCMDIR_PLAY)
2754 ai->oformats = fmts;
2756 ai->iformats = fmts;
2760 * @c magic - OSSv4 docs: "Reserved for internal use
2764 * @c card_number - OSSv4 docs: "Number of the sound
2765 * card where this device belongs or -1 if this
2766 * information is not available. Applications
2767 * should normally not use this field for any
2770 ai->card_number = -1;
2772 * @todo @c song_name - depends first on
2773 * SNDCTL_[GS]ETSONG @todo @c label - depends
2774 * on SNDCTL_[GS]ETLABEL
2775 * @todo @c port_number - routing information?
2777 ai->port_number = -1;
2778 ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1;
2781 * @c real_device - OSSv4 docs: "Obsolete."
2783 ai->real_device = -1;
2784 strlcpy(ai->devnode, "/dev/", sizeof(ai->devnode));
2785 strlcat(ai->devnode, devname, sizeof(ai->devnode));
2786 ai->enabled = device_is_attached(d->dev) ? 1 : 0;
2789 * @c flags - OSSv4 docs: "Reserved for future use."
2792 * @c binding - OSSv4 docs: "Reserved for future use."
2794 * @todo @c handle - haven't decided how to generate
2795 * this yet; bus, vendor, device IDs?
2797 ai->min_rate = caps->minspeed;
2798 ai->max_rate = caps->maxspeed;
2800 ai->min_channels = minch;
2801 ai->max_channels = maxch;
2803 ai->nrates = chn_getrates(ch, &rates);
2804 if (ai->nrates > OSS_MAX_SAMPLE_RATES)
2805 ai->nrates = OSS_MAX_SAMPLE_RATES;
2807 for (i = 0; i < ai->nrates; i++)
2808 ai->rates[i] = rates[i];
2810 ai->next_play_engine = 0;
2811 ai->next_rec_engine = 0;
2818 if (devname != NULL)
2822 /* Exhausted the search -- nothing is locked, so return. */
2827 * @brief Assigns a PCM channel to a sync group.
2829 * Sync groups are used to enable audio operations on multiple devices
2830 * simultaneously. They may be used with any number of devices and may
2831 * span across applications. Devices are added to groups with
2832 * the SNDCTL_DSP_SYNCGROUP ioctl, and operations are triggered with the
2833 * SNDCTL_DSP_SYNCSTART ioctl.
2835 * If the @c id field of the @c group parameter is set to zero, then a new
2836 * sync group is created. Otherwise, wrch and rdch (if set) are added to
2837 * the group specified.
2839 * @todo As far as memory allocation, should we assume that things are
2840 * okay and allocate with M_WAITOK before acquiring channel locks,
2841 * freeing later if not?
2843 * @param wrch output channel associated w/ device (if any)
2844 * @param rdch input channel associated w/ device (if any)
2845 * @param group Sync group parameters
2848 * @retval non-zero error to be propagated upstream
2851 dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
2853 struct pcmchan_syncmember *smrd, *smwr;
2854 struct pcmchan_syncgroup *sg;
2863 * Free_unr() may sleep, so store released syncgroup IDs until after
2864 * all locks are released.
2866 sg_ids[0] = sg_ids[1] = sg_ids[2] = 0;
2871 * - Insert channel(s) into group's member list.
2872 * - Set CHN_F_NOTRIGGER on channel(s).
2873 * - Stop channel(s).
2877 * If device's channels are already mapped to a group, unmap them.
2881 sg_ids[0] = chn_syncdestroy(wrch);
2886 sg_ids[1] = chn_syncdestroy(rdch);
2890 * Verify that mode matches character device properites.
2891 * - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
2892 * - Bail if PCM_ENABLE_INPUT && rdch == NULL.
2894 if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) ||
2895 ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) {
2901 * An id of zero indicates the user wants to create a new
2904 if (group->id == 0) {
2905 sg = (struct pcmchan_syncgroup *)kmalloc(sizeof(*sg), M_DEVBUF, M_NOWAIT);
2907 SLIST_INIT(&sg->members);
2908 sg->id = alloc_unr(pcmsg_unrhdr);
2911 SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link);
2915 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2916 if (sg->id == group->id)
2923 /* Couldn't create or find a syncgroup. Fail. */
2928 * Allocate a syncmember, assign it and a channel together, and
2929 * insert into syncgroup.
2931 if (group->mode & PCM_ENABLE_INPUT) {
2932 smrd = (struct pcmchan_syncmember *)kmalloc(sizeof(*smrd), M_DEVBUF, M_NOWAIT);
2938 SLIST_INSERT_HEAD(&sg->members, smrd, link);
2943 rdch->flags |= CHN_F_NOTRIGGER;
2947 if (group->mode & PCM_ENABLE_OUTPUT) {
2948 smwr = (struct pcmchan_syncmember *)kmalloc(sizeof(*smwr), M_DEVBUF, M_NOWAIT);
2954 SLIST_INSERT_HEAD(&sg->members, smwr, link);
2959 wrch->flags |= CHN_F_NOTRIGGER;
2967 kfree(smrd, M_DEVBUF);
2968 if ((sg != NULL) && SLIST_EMPTY(&sg->members)) {
2970 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2971 kfree(sg, M_DEVBUF);
2988 free_unr(pcmsg_unrhdr, sg_ids[0]);
2990 free_unr(pcmsg_unrhdr, sg_ids[1]);
2992 free_unr(pcmsg_unrhdr, sg_ids[2]);
2998 * @brief Launch a sync group into action
3000 * Sync groups are established via SNDCTL_DSP_SYNCGROUP. This function
3001 * iterates over all members, triggering them along the way.
3003 * @note Caller must not hold any channel locks.
3005 * @param sg_id sync group identifier
3008 * @retval non-zero error worthy of propagating upstream to user
3011 dsp_oss_syncstart(int sg_id)
3013 struct pcmchan_syncmember *sm, *sm_tmp;
3014 struct pcmchan_syncgroup *sg;
3015 struct pcm_channel *c;
3018 /* Get the synclists lock */
3025 /* Search for syncgroup by ID */
3026 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
3027 if (sg->id == sg_id)
3031 /* Return EINVAL if not found */
3037 /* Any removals resulting in an empty group should've handled this */
3038 KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup"));
3041 * Attempt to lock all member channels - if any are already
3042 * locked, unlock those acquired, sleep for a bit, and try
3045 SLIST_FOREACH(sm, &sg->members, link) {
3046 if (CHN_TRYLOCK(sm->ch) == 0) {
3047 int timo = hz * 5/1000;
3051 /* Release all locked channels so far, retry */
3052 SLIST_FOREACH(sm_tmp, &sg->members, link) {
3053 /* sm is the member already locked */
3056 CHN_UNLOCK(sm_tmp->ch);
3059 /** @todo Is PRIBIO correct/ */
3060 ret = lksleep(sm, &snd_pcm_syncgroups_mtx,
3061 PCATCH, "pcmsg", timo);
3062 if (ret == EINTR || ret == ERESTART)
3066 ret = 0; /* Assumes ret == EAGAIN... */
3069 } while (needlocks && ret == 0);
3071 /* Proceed only if no errors encountered. */
3073 /* Launch channels */
3074 while ((sm = SLIST_FIRST(&sg->members)) != NULL) {
3075 SLIST_REMOVE_HEAD(&sg->members, link);
3080 c->flags &= ~CHN_F_NOTRIGGER;
3083 kfree(sm, M_DEVBUF);
3086 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
3087 kfree(sg, M_DEVBUF);
3093 * Free_unr() may sleep, so be sure to give up the syncgroup lock
3097 free_unr(pcmsg_unrhdr, sg_id);
3103 * @brief Handler for SNDCTL_DSP_POLICY
3105 * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment
3106 * size and count like with SNDCTL_DSP_SETFRAGMENT. Instead of the user
3107 * specifying those two parameters, s/he simply selects a number from 0..10
3108 * which corresponds to a buffer size. Smaller numbers request smaller
3109 * buffers with lower latencies (at greater overhead from more frequent
3110 * interrupts), while greater numbers behave in the opposite manner.
3112 * The 4Front spec states that a value of 5 should be the default. However,
3113 * this implementation deviates slightly by using a linear scale without
3114 * consulting drivers. I.e., even though drivers may have different default
3115 * buffer sizes, a policy argument of 5 will have the same result across
3118 * See http://manuals.opensound.com/developer/SNDCTL_DSP_POLICY.html for
3121 * @todo When SNDCTL_DSP_COOKEDMODE is supported, it'll be necessary to
3122 * work with hardware drivers directly.
3124 * @note PCM channel arguments must not be locked by caller.
3126 * @param wrch Pointer to opened playback channel (optional; may be NULL)
3127 * @param rdch " recording channel (optional; may be NULL)
3128 * @param policy Integer from [0:10]
3130 * @retval 0 constant (for now)
3133 dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
3137 if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX)
3140 /* Default: success */
3145 ret = chn_setlatency(rdch, policy);
3149 if (wrch && ret == 0) {
3151 ret = chn_setlatency(wrch, policy);
3162 * @brief Enable or disable "cooked" mode
3164 * This is a handler for @c SNDCTL_DSP_COOKEDMODE. When in cooked mode, which
3165 * is the default, the sound system handles rate and format conversions
3166 * automatically (ex: user writing 11025Hz/8 bit/unsigned but card only
3167 * operates with 44100Hz/16bit/signed samples).
3169 * Disabling cooked mode is intended for applications wanting to mmap()
3170 * a sound card's buffer space directly, bypassing the FreeBSD 2-stage
3171 * feeder architecture, presumably to gain as much control over audio
3172 * hardware as possible.
3174 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_COOKEDMODE.html
3177 * @param wrch playback channel (optional; may be NULL)
3178 * @param rdch recording channel (optional; may be NULL)
3179 * @param enabled 0 = raw mode, 1 = cooked mode
3181 * @retval EINVAL Operation not yet supported.
3184 dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
3188 * XXX I just don't get it. Why don't they call it
3189 * "BITPERFECT" ~ SNDCTL_DSP_BITPERFECT !?!?.
3190 * This is just plain so confusing, incoherent,
3191 * <insert any non-printable characters here>.
3193 if (!(enabled == 1 || enabled == 0))
3197 * I won't give in. I'm inverting its logic here and now.
3198 * Brag all you want, but "BITPERFECT" should be the better
3201 enabled ^= 0x00000001;
3205 wrch->flags &= ~CHN_F_BITPERFECT;
3206 wrch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3212 rdch->flags &= ~CHN_F_BITPERFECT;
3213 rdch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3221 * @brief Retrieve channel interleaving order
3223 * This is the handler for @c SNDCTL_DSP_GET_CHNORDER.
3225 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html
3228 * @note As the ioctl definition is still under construction, FreeBSD
3229 * does not currently support SNDCTL_DSP_GET_CHNORDER.
3231 * @param wrch playback channel (optional; may be NULL)
3232 * @param rdch recording channel (optional; may be NULL)
3233 * @param map channel map (result will be stored there)
3235 * @retval EINVAL Operation not yet supported.
3238 dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3240 struct pcm_channel *ch;
3243 ch = (wrch != NULL) ? wrch : rdch;
3246 ret = chn_oss_getorder(ch, map);
3255 * @brief Specify channel interleaving order
3257 * This is the handler for @c SNDCTL_DSP_SET_CHNORDER.
3259 * @note As the ioctl definition is still under construction, FreeBSD
3260 * does not currently support @c SNDCTL_DSP_SET_CHNORDER.
3262 * @param wrch playback channel (optional; may be NULL)
3263 * @param rdch recording channel (optional; may be NULL)
3264 * @param map channel map
3266 * @retval EINVAL Operation not yet supported.
3269 dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3277 ret = chn_oss_setorder(wrch, map);
3281 if (ret == 0 && rdch != NULL) {
3283 ret = chn_oss_setorder(rdch, map);
3291 dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch,
3294 struct pcm_channel *ch;
3299 ch = (wrch != NULL) ? wrch : rdch;
3303 ret = chn_oss_getmask(ch, &chnmask);
3314 #ifdef OSSV4_EXPERIMENT
3316 * @brief Retrieve an audio device's label
3318 * This is a handler for the @c SNDCTL_GETLABEL ioctl.
3320 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3323 * From Hannu@4Front: "For example ossxmix (just like some HW mixer
3324 * consoles) can show variable "labels" for certain controls. By default
3325 * the application name (say quake) is shown as the label but
3326 * applications may change the labels themselves."
3328 * @note As the ioctl definition is still under construction, FreeBSD
3329 * does not currently support @c SNDCTL_GETLABEL.
3331 * @param wrch playback channel (optional; may be NULL)
3332 * @param rdch recording channel (optional; may be NULL)
3333 * @param label label gets copied here
3335 * @retval EINVAL Operation not yet supported.
3338 dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3344 * @brief Specify an audio device's label
3346 * This is a handler for the @c SNDCTL_SETLABEL ioctl. Please see the
3347 * comments for @c dsp_oss_getlabel immediately above.
3349 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3352 * @note As the ioctl definition is still under construction, FreeBSD
3353 * does not currently support SNDCTL_SETLABEL.
3355 * @param wrch playback channel (optional; may be NULL)
3356 * @param rdch recording channel (optional; may be NULL)
3357 * @param label label gets copied from here
3359 * @retval EINVAL Operation not yet supported.
3362 dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3368 * @brief Retrieve name of currently played song
3370 * This is a handler for the @c SNDCTL_GETSONG ioctl. Audio players could
3371 * tell the system the name of the currently playing song, which would be
3372 * visible in @c /dev/sndstat.
3374 * See @c http://manuals.opensound.com/developer/SNDCTL_GETSONG.html
3377 * @note As the ioctl definition is still under construction, FreeBSD
3378 * does not currently support SNDCTL_GETSONG.
3380 * @param wrch playback channel (optional; may be NULL)
3381 * @param rdch recording channel (optional; may be NULL)
3382 * @param song song name gets copied here
3384 * @retval EINVAL Operation not yet supported.
3387 dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3393 * @brief Retrieve name of currently played song
3395 * This is a handler for the @c SNDCTL_SETSONG ioctl. Audio players could
3396 * tell the system the name of the currently playing song, which would be
3397 * visible in @c /dev/sndstat.
3399 * See @c http://manuals.opensound.com/developer/SNDCTL_SETSONG.html
3402 * @note As the ioctl definition is still under construction, FreeBSD
3403 * does not currently support SNDCTL_SETSONG.
3405 * @param wrch playback channel (optional; may be NULL)
3406 * @param rdch recording channel (optional; may be NULL)
3407 * @param song song name gets copied from here
3409 * @retval EINVAL Operation not yet supported.
3412 dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3418 * @brief Rename a device
3420 * This is a handler for the @c SNDCTL_SETNAME ioctl.
3422 * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for
3425 * From Hannu@4Front: "This call is used to change the device name
3426 * reported in /dev/sndstat and ossinfo. So instead of using some generic
3427 * 'OSS loopback audio (MIDI) driver' the device may be given a meaningfull
3428 * name depending on the current context (for example 'OSS virtual wave table
3429 * synth' or 'VoIP link to London')."
3431 * @note As the ioctl definition is still under construction, FreeBSD
3432 * does not currently support SNDCTL_SETNAME.
3434 * @param wrch playback channel (optional; may be NULL)
3435 * @param rdch recording channel (optional; may be NULL)
3436 * @param name new device name gets copied from here
3438 * @retval EINVAL Operation not yet supported.
3441 dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
3445 #endif /* !OSSV4_EXPERIMENT */