2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/param.h>
28 #include <sys/queue.h>
30 #include <dev/sound/pcm/sound.h>
32 SND_DECLARE_FILE("$FreeBSD$");
36 static d_open_t dsp_open;
37 static d_close_t dsp_close;
38 static d_read_t dsp_read;
39 static d_write_t dsp_write;
40 static d_ioctl_t dsp_ioctl;
41 static d_poll_t dsp_poll;
42 static d_mmap_t dsp_mmap;
44 struct cdevsw dsp_cdevsw = {
45 .d_version = D_VERSION,
46 .d_flags = D_NEEDGIANT,
58 static eventhandler_tag dsp_ehtag;
61 static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group);
62 static int dsp_oss_syncstart(int sg_id);
63 static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy);
64 #ifdef OSSV4_EXPERIMENT
65 static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled);
66 static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
67 static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
68 static int dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
69 static int dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
70 static int dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
71 static int dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
72 static int dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name);
75 static struct snddev_info *
76 dsp_get_info(struct cdev *dev)
78 struct snddev_info *d;
82 if (unit >= devclass_get_maxunit(pcm_devclass))
84 d = devclass_get_softc(pcm_devclass, unit);
90 dsp_get_flags(struct cdev *dev)
96 if (unit >= devclass_get_maxunit(pcm_devclass))
98 bdev = devclass_get_device(pcm_devclass, unit);
100 return pcm_getflags(bdev);
104 dsp_set_flags(struct cdev *dev, u_int32_t flags)
110 if (unit >= devclass_get_maxunit(pcm_devclass))
112 bdev = devclass_get_device(pcm_devclass, unit);
114 pcm_setflags(bdev, flags);
118 * return the channels associated with an open device instance.
119 * set the priority if the device is simplex and one direction (only) is
121 * lock channels specified.
124 getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch, u_int32_t prio)
126 struct snddev_info *d;
129 flags = dsp_get_flags(dev);
130 d = dsp_get_info(dev);
133 KASSERT((flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
134 ("getchns: read and write both prioritised"));
136 if ((flags & SD_F_PRIO_SET) == 0 && (prio != (SD_F_PRIO_RD | SD_F_PRIO_WR))) {
137 flags |= prio & (SD_F_PRIO_RD | SD_F_PRIO_WR);
138 dsp_set_flags(dev, flags);
141 *rdch = dev->si_drv1;
142 *wrch = dev->si_drv2;
143 if ((flags & SD_F_SIMPLEX) && (flags & SD_F_PRIO_SET)) {
145 if (*rdch && flags & SD_F_PRIO_WR) {
147 *rdch = pcm_getfakechan(d);
148 } else if (*wrch && flags & SD_F_PRIO_RD) {
150 *wrch = pcm_getfakechan(d);
154 pcm_getfakechan(d)->flags |= CHN_F_BUSY;
158 if (*rdch && *rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
160 if (*wrch && *wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
166 /* unlock specified channels */
168 relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch, u_int32_t prio)
170 struct snddev_info *d;
172 d = dsp_get_info(dev);
173 if (wrch && wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
175 if (rdch && rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
181 dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
183 struct pcm_channel *rdch, *wrch;
184 struct snddev_info *d;
190 if (i_dev == NULL || td == NULL)
193 if ((flags & (FREAD | FWRITE)) == 0)
196 d = dsp_get_info(i_dev);
197 devtype = PCMDEV(i_dev);
200 /* decide default format */
214 case SND_DEV_NORESET:
222 chnum = PCMCHAN(i_dev);
226 panic("impossible devtype %d", devtype);
229 /* lock snddev so nobody else can monkey with it */
232 rdch = i_dev->si_drv1;
233 wrch = i_dev->si_drv2;
235 if (rdch || wrch || ((dsp_get_flags(i_dev) & SD_F_SIMPLEX) &&
236 (flags & (FREAD | FWRITE)) == (FREAD | FWRITE))) {
237 /* simplex or not, better safe than sorry. */
243 * if we get here, the open request is valid- either:
244 * * we were previously not open
245 * * we were open for play xor record and the opener wants
246 * the non-open direction
251 error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, chnum);
252 if (error != 0 && error != EBUSY && chnum != -1 && (flags & FWRITE))
253 error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, -1);
255 if (error == 0 && (chn_reset(rdch, fmt) ||
256 (fmt && chn_setspeed(rdch, DSP_DEFAULT_SPEED))))
261 pcm_chnrelease(rdch);
265 if (flags & O_NONBLOCK)
266 rdch->flags |= CHN_F_NBIO;
272 if (flags & FWRITE) {
275 error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, chnum);
276 if (error != 0 && error != EBUSY && chnum != -1 && (flags & FREAD))
277 error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, -1);
279 if (error == 0 && (chn_reset(wrch, fmt) ||
280 (fmt && chn_setspeed(wrch, DSP_DEFAULT_SPEED))))
285 pcm_chnrelease(wrch);
288 * Lock, deref and release previously created record channel
291 pcm_chnref(rdch, -1);
292 pcm_chnrelease(rdch);
298 if (flags & O_NONBLOCK)
299 wrch->flags |= CHN_F_NBIO;
305 i_dev->si_drv1 = rdch;
306 i_dev->si_drv2 = wrch;
313 dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
315 struct pcm_channel *rdch, *wrch;
316 struct snddev_info *d;
319 d = dsp_get_info(i_dev);
321 rdch = i_dev->si_drv1;
322 wrch = i_dev->si_drv2;
326 * Free_unr() may sleep, so store released syncgroup IDs until after
327 * all locks are released.
329 sg_ids[0] = sg_ids[1] = 0;
335 * The channel itself need not be locked because:
336 * a) Adding a channel to a syncgroup happens only in dsp_ioctl(),
337 * which cannot run concurrently to dsp_close().
338 * b) The syncmember pointer (sm) is protected by the global
339 * syncgroup list lock.
340 * c) A channel can't just disappear, invalidating pointers,
341 * unless it's closed/dereferenced first.
344 sg_ids[0] = chn_syncdestroy(rdch);
348 refs += pcm_chnref(rdch, -1);
349 chn_abort(rdch); /* won't sleep */
350 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
352 pcm_chnrelease(rdch);
356 * Please see block above.
359 sg_ids[1] = chn_syncdestroy(wrch);
363 refs += pcm_chnref(wrch, -1);
365 * XXX: Maybe the right behaviour is to abort on non_block.
366 * It seems that mplayer flushes the audio queue by quickly
367 * closing and re-opening. In FBSD, there's a long pause
368 * while the audio queue flushes that I presume isn't there in
371 chn_flush(wrch); /* may sleep */
372 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
374 pcm_chnrelease(wrch);
379 i_dev->si_drv1 = NULL;
381 i_dev->si_drv2 = NULL;
383 * If there are no more references, release the channels.
385 if (refs == 0 && i_dev->si_drv1 == NULL &&
386 i_dev->si_drv2 == NULL) {
387 if (pcm_getfakechan(d))
388 pcm_getfakechan(d)->flags = 0;
389 /* What is this?!? */
390 dsp_set_flags(i_dev, dsp_get_flags(i_dev) & ~SD_F_TRANSIENT);
397 free_unr(pcmsg_unrhdr, sg_ids[0]);
399 free_unr(pcmsg_unrhdr, sg_ids[1]);
405 dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
407 struct pcm_channel *rdch, *wrch;
410 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD);
412 KASSERT(rdch, ("dsp_read: nonexistant channel"));
413 KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel"));
415 if (rdch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
416 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
419 if (!(rdch->flags & CHN_F_RUNNING))
420 rdch->flags |= CHN_F_RUNNING;
421 ret = chn_read(rdch, buf);
422 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
428 dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
430 struct pcm_channel *rdch, *wrch;
433 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_WR);
435 KASSERT(wrch, ("dsp_write: nonexistant channel"));
436 KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel"));
438 if (wrch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
439 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
442 if (!(wrch->flags & CHN_F_RUNNING))
443 wrch->flags |= CHN_F_RUNNING;
446 * Chn_write() must give up channel lock in order to copy bytes from
447 * userland, so up the "in progress" counter to make sure someone
448 * else doesn't come along and muss up the buffer.
451 ret = chn_write(wrch, buf);
453 cv_signal(&wrch->cv);
455 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
461 dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
463 struct pcm_channel *chn, *rdch, *wrch;
464 struct snddev_info *d;
466 int ret = 0, *arg_i = (int *)arg, tmp;
472 * this is an evil hack to allow broken apps to perform mixer ioctls
476 d = dsp_get_info(i_dev);
477 if (IOCGROUP(cmd) == 'M') {
479 * This is at least, a bug to bug compatible with OSS.
481 if (d->mixer_dev != NULL)
482 return mixer_ioctl(d->mixer_dev, cmd, arg, -1, td);
488 * Certain ioctls may be made on any type of device (audio, mixer,
489 * and MIDI). Handle those special cases here.
491 if (IOCGROUP(cmd) == 'X') {
494 sound_oss_sysinfo((oss_sysinfo *)arg);
496 case SNDCTL_AUDIOINFO:
497 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
499 case SNDCTL_MIXERINFO:
500 ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
509 getchns(i_dev, &rdch, &wrch, 0);
512 if (wrch && (wrch->flags & CHN_F_DEAD))
514 if (rdch && (rdch->flags & CHN_F_DEAD))
517 relchns(i_dev, rdch, wrch, 0);
528 * we start with the new ioctl interface.
530 case AIONWRITE: /* how many bytes can write ? */
534 if (wrch && wrch->bufhard.dl)
535 while (chn_wrfeed(wrch) == 0);
537 *arg_i = sndbuf_getfree(wrch->bufsoft);
545 case AIOSSIZE: /* set the current blocksize */
547 struct snd_size *p = (struct snd_size *)arg;
553 chn_setblocksize(wrch, 2, p->play_size);
554 p->play_size = sndbuf_getblksz(wrch->bufsoft);
559 chn_setblocksize(rdch, 2, p->rec_size);
560 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
565 case AIOGSIZE: /* get the current blocksize */
567 struct snd_size *p = (struct snd_size *)arg;
571 p->play_size = sndbuf_getblksz(wrch->bufsoft);
576 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
585 snd_chan_param *p = (snd_chan_param *)arg;
587 if (cmd == AIOSFMT &&
588 ((p->play_format != 0 && p->play_rate == 0) ||
589 (p->rec_format != 0 && p->rec_rate == 0))) {
595 if (cmd == AIOSFMT && p->play_format != 0) {
596 chn_setformat(wrch, p->play_format);
597 chn_setspeed(wrch, p->play_rate);
599 p->play_rate = wrch->speed;
600 p->play_format = wrch->format;
608 if (cmd == AIOSFMT && p->rec_format != 0) {
609 chn_setformat(rdch, p->rec_format);
610 chn_setspeed(rdch, p->rec_rate);
612 p->rec_rate = rdch->speed;
613 p->rec_format = rdch->format;
622 case AIOGCAP: /* get capabilities */
624 snd_capabilities *p = (snd_capabilities *)arg;
625 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
630 rcaps = chn_getcaps(rdch);
634 pcaps = chn_getcaps(wrch);
636 p->rate_min = max(rcaps? rcaps->minspeed : 0,
637 pcaps? pcaps->minspeed : 0);
638 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
639 pcaps? pcaps->maxspeed : 1000000);
640 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
641 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
642 /* XXX bad on sb16 */
643 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
644 (wrch? chn_getformats(wrch) : 0xffffffff);
646 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
648 p->mixers = 1; /* default: one mixer */
649 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
650 p->left = p->right = 100;
659 if (*arg_i == AIOSYNC_PLAY && wrch) {
661 *arg_i = chn_abort(wrch);
663 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
665 *arg_i = chn_abort(rdch);
668 printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
674 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
675 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
679 * here follow the standard ioctls (filio.h etc.)
681 case FIONREAD: /* get # bytes to read */
684 /* if (rdch && rdch->bufhard.dl)
685 while (chn_rdfeed(rdch) == 0);
687 *arg_i = sndbuf_getready(rdch->bufsoft);
695 case FIOASYNC: /*set/clear async i/o */
696 DEB( printf("FIOASYNC\n") ; )
699 case SNDCTL_DSP_NONBLOCK:
700 case FIONBIO: /* set/clear non-blocking i/o */
704 rdch->flags |= CHN_F_NBIO;
706 rdch->flags &= ~CHN_F_NBIO;
712 wrch->flags |= CHN_F_NBIO;
714 wrch->flags &= ~CHN_F_NBIO;
720 * Finally, here is the linux-compatible ioctl interface
722 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
723 case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
724 case SNDCTL_DSP_GETBLKSIZE:
725 chn = wrch ? wrch : rdch;
728 *arg_i = sndbuf_getblksz(chn->bufsoft);
736 case SNDCTL_DSP_SETBLKSIZE:
737 RANGE(*arg_i, 16, 65536);
740 chn_setblocksize(wrch, 2, *arg_i);
745 chn_setblocksize(rdch, 2, *arg_i);
750 case SNDCTL_DSP_RESET:
751 DEB(printf("dsp reset\n"));
766 case SNDCTL_DSP_SYNC:
767 DEB(printf("dsp sync\n"));
768 /* chn_sync may sleep */
771 chn_sync(wrch, sndbuf_getsize(wrch->bufsoft) - 4);
776 case SNDCTL_DSP_SPEED:
777 /* chn_setspeed may sleep */
781 ret = chn_setspeed(wrch, *arg_i);
785 if (rdch && ret == 0) {
787 ret = chn_setspeed(rdch, *arg_i);
795 case SOUND_PCM_READ_RATE:
796 chn = wrch ? wrch : rdch;
807 case SNDCTL_DSP_STEREO:
809 *arg_i = (*arg_i)? AFMT_STEREO : 0;
812 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
813 tmp = (wrch->format & AFMT_STEREO)? 1 : 0;
816 if (rdch && ret == 0) {
818 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
820 tmp = (rdch->format & AFMT_STEREO)? 1 : 0;
826 case SOUND_PCM_WRITE_CHANNELS:
827 /* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
830 *arg_i = (*arg_i != 1)? AFMT_STEREO : 0;
833 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
834 tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
837 if (rdch && ret == 0) {
839 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
841 tmp = (rdch->format & AFMT_STEREO)? 2 : 1;
846 chn = wrch ? wrch : rdch;
848 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
853 case SOUND_PCM_READ_CHANNELS:
854 chn = wrch ? wrch : rdch;
857 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
865 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
866 chn = wrch ? wrch : rdch;
869 *arg_i = chn_getformats(chn);
877 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
878 if ((*arg_i != AFMT_QUERY)) {
882 ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
883 tmp = wrch->format & ~AFMT_STEREO;
886 if (rdch && ret == 0) {
888 ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
890 tmp = rdch->format & ~AFMT_STEREO;
895 chn = wrch ? wrch : rdch;
897 *arg_i = chn->format & ~AFMT_STEREO;
902 case SNDCTL_DSP_SETFRAGMENT:
903 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
905 u_int32_t fragln = (*arg_i) & 0x0000ffff;
906 u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
908 u_int32_t r_maxfrags, r_fragsz;
910 RANGE(fragln, 4, 16);
911 fragsz = 1 << fragln;
914 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
917 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
918 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
920 DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
923 ret = chn_setblocksize(rdch, maxfrags, fragsz);
924 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
925 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
928 r_maxfrags = maxfrags;
931 if (wrch && ret == 0) {
933 ret = chn_setblocksize(wrch, maxfrags, fragsz);
934 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
935 fragsz = sndbuf_getblksz(wrch->bufsoft);
937 } else { /* use whatever came from the read channel */
938 maxfrags = r_maxfrags;
947 *arg_i = (maxfrags << 16) | fragln;
951 case SNDCTL_DSP_GETISPACE:
952 /* return the size of data available in the input queue */
954 audio_buf_info *a = (audio_buf_info *)arg;
956 struct snd_dbuf *bs = rdch->bufsoft;
959 a->bytes = sndbuf_getready(bs);
960 a->fragments = a->bytes / sndbuf_getblksz(bs);
961 a->fragstotal = sndbuf_getblkcnt(bs);
962 a->fragsize = sndbuf_getblksz(bs);
968 case SNDCTL_DSP_GETOSPACE:
969 /* return space available in the output queue */
971 audio_buf_info *a = (audio_buf_info *)arg;
973 struct snd_dbuf *bs = wrch->bufsoft;
976 /* XXX abusive DMA update: chn_wrupdate(wrch); */
977 a->bytes = sndbuf_getfree(bs);
978 a->fragments = a->bytes / sndbuf_getblksz(bs);
979 a->fragstotal = sndbuf_getblkcnt(bs);
980 a->fragsize = sndbuf_getblksz(bs);
986 case SNDCTL_DSP_GETIPTR:
988 count_info *a = (count_info *)arg;
990 struct snd_dbuf *bs = rdch->bufsoft;
993 /* XXX abusive DMA update: chn_rdupdate(rdch); */
994 a->bytes = sndbuf_gettotal(bs);
995 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
996 a->ptr = sndbuf_getreadyptr(bs);
997 rdch->blocks = sndbuf_getblocks(bs);
1004 case SNDCTL_DSP_GETOPTR:
1006 count_info *a = (count_info *)arg;
1008 struct snd_dbuf *bs = wrch->bufsoft;
1011 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1012 a->bytes = sndbuf_gettotal(bs);
1013 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
1014 a->ptr = sndbuf_getreadyptr(bs);
1015 wrch->blocks = sndbuf_getblocks(bs);
1022 case SNDCTL_DSP_GETCAPS:
1023 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
1024 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1025 *arg_i |= DSP_CAP_DUPLEX;
1028 case SOUND_PCM_READ_BITS:
1029 chn = wrch ? wrch : rdch;
1032 if (chn->format & AFMT_8BIT)
1034 else if (chn->format & AFMT_16BIT)
1036 else if (chn->format & AFMT_24BIT)
1038 else if (chn->format & AFMT_32BIT)
1049 case SNDCTL_DSP_SETTRIGGER:
1052 rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
1053 if (*arg_i & PCM_ENABLE_INPUT)
1056 rdch->flags |= CHN_F_NOTRIGGER;
1061 wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
1062 if (*arg_i & PCM_ENABLE_OUTPUT)
1065 wrch->flags |= CHN_F_NOTRIGGER;
1070 case SNDCTL_DSP_GETTRIGGER:
1074 if (wrch->flags & CHN_F_TRIGGERED)
1075 *arg_i |= PCM_ENABLE_OUTPUT;
1080 if (rdch->flags & CHN_F_TRIGGERED)
1081 *arg_i |= PCM_ENABLE_INPUT;
1086 case SNDCTL_DSP_GETODELAY:
1088 struct snd_dbuf *b = wrch->bufhard;
1089 struct snd_dbuf *bs = wrch->bufsoft;
1092 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1093 *arg_i = sndbuf_getready(b) + sndbuf_getready(bs);
1099 case SNDCTL_DSP_POST:
1102 wrch->flags &= ~CHN_F_NOTRIGGER;
1108 case SNDCTL_DSP_SETDUPLEX:
1110 * switch to full-duplex mode if card is in half-duplex
1111 * mode and is able to work in full-duplex mode
1113 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1114 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1118 * The following four ioctls are simple wrappers around mixer_ioctl
1119 * with no further processing. xcmd is short for "translated
1122 case SNDCTL_DSP_GETRECVOL:
1124 xcmd = SOUND_MIXER_READ_RECLEV;
1126 case SNDCTL_DSP_SETRECVOL:
1128 xcmd = SOUND_MIXER_WRITE_RECLEV;
1130 case SNDCTL_DSP_GETPLAYVOL:
1132 xcmd = SOUND_MIXER_READ_PCM;
1134 case SNDCTL_DSP_SETPLAYVOL:
1136 xcmd = SOUND_MIXER_WRITE_PCM;
1138 if (d->mixer_dev != NULL)
1139 ret = mixer_ioctl(d->mixer_dev, xcmd, arg, -1, td);
1144 case SNDCTL_DSP_GET_RECSRC_NAMES:
1145 case SNDCTL_DSP_GET_RECSRC:
1146 case SNDCTL_DSP_SET_RECSRC:
1147 if (d->mixer_dev != NULL)
1148 ret = mixer_ioctl(d->mixer_dev, cmd, arg, -1, td);
1154 * The following 3 ioctls aren't very useful at the moment. For
1155 * now, only a single channel is associated with a cdev (/dev/dspN
1156 * instance), so there's only a single output routing to use (i.e.,
1157 * the wrch bound to this cdev).
1159 case SNDCTL_DSP_GET_PLAYTGT_NAMES:
1161 oss_mixer_enuminfo *ei;
1162 ei = (oss_mixer_enuminfo *)arg;
1165 ei->version = 0; /* static for now */
1166 ei->strindex[0] = 0;
1170 strlcpy(ei->strings, wrch->name,
1171 sizeof(ei->strings));
1174 ei->strings[0] = '\0';
1178 case SNDCTL_DSP_GET_PLAYTGT:
1179 case SNDCTL_DSP_SET_PLAYTGT: /* yes, they are the same for now */
1182 * OSSv4: "The value that was accepted by the device will
1183 * be returned back in the variable pointed by the
1192 case SNDCTL_DSP_SILENCE:
1194 * Flush the software (pre-feed) buffer, but try to minimize playback
1195 * interruption. (I.e., record unplayed samples with intent to
1196 * restore by SNDCTL_DSP_SKIP.) Intended for application "pause"
1202 struct snd_dbuf *bs;
1204 while (wrch->inprog != 0)
1205 cv_wait(&wrch->cv, wrch->lock);
1207 if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) {
1208 bs->sl = sndbuf_getready(bs);
1209 sndbuf_dispose(bs, bs->shadbuf, sndbuf_getready(bs));
1210 sndbuf_fillsilence(bs);
1217 case SNDCTL_DSP_SKIP:
1219 * OSSv4 docs: "This ioctl call discards all unplayed samples in the
1220 * playback buffer by moving the current write position immediately
1221 * before the point where the device is currently reading the samples."
1226 struct snd_dbuf *bs;
1228 while (wrch->inprog != 0)
1229 cv_wait(&wrch->cv, wrch->lock);
1231 if ((bs->shadbuf != NULL) && (bs->sl > 0)) {
1232 sndbuf_softreset(bs);
1233 sndbuf_acquire(bs, bs->shadbuf, bs->sl);
1241 case SNDCTL_DSP_CURRENT_OPTR:
1242 case SNDCTL_DSP_CURRENT_IPTR:
1244 * @note Changing formats resets the buffer counters, which differs
1245 * from the 4Front drivers. However, I don't expect this to be
1246 * much of a problem.
1248 * @note In a test where @c CURRENT_OPTR is called immediately after write
1249 * returns, this driver is about 32K samples behind whereas
1250 * 4Front's is about 8K samples behind. Should determine source
1251 * of discrepancy, even if only out of curiosity.
1253 * @todo Actually test SNDCTL_DSP_CURRENT_IPTR.
1255 chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch;
1259 struct snd_dbuf *bs;
1262 oss_count_t *oc = (oss_count_t *)arg;
1267 tmp = (sndbuf_getsize(b) + chn_getptr(chn) - sndbuf_gethwptr(b)) % sndbuf_getsize(b);
1268 oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getbps(b);
1269 oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getbps(b);
1271 oc->samples = sndbuf_gettotal(bs) / sndbuf_getbps(bs);
1272 oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getbps(bs);
1278 case SNDCTL_DSP_HALT_OUTPUT:
1279 case SNDCTL_DSP_HALT_INPUT:
1280 chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch;
1290 case SNDCTL_DSP_LOW_WATER:
1292 * Set the number of bytes required to attract attention by
1297 wrch->lw = (*arg_i > 1) ? *arg_i : 1;
1302 rdch->lw = (*arg_i > 1) ? *arg_i : 1;
1307 case SNDCTL_DSP_GETERROR:
1309 * OSSv4 docs: "All errors and counters will automatically be
1310 * cleared to zeroes after the call so each call will return only
1311 * the errors that occurred after the previous invocation. ... The
1312 * play_underruns and rec_overrun fields are the only usefull fields
1313 * returned by OSS 4.0."
1316 audio_errinfo *ei = (audio_errinfo *)arg;
1318 bzero((void *)ei, sizeof(*ei));
1322 ei->play_underruns = wrch->xruns;
1328 ei->rec_overruns = rdch->xruns;
1335 case SNDCTL_DSP_SYNCGROUP:
1336 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
1339 case SNDCTL_DSP_SYNCSTART:
1340 ret = dsp_oss_syncstart(*arg_i);
1343 case SNDCTL_DSP_POLICY:
1344 ret = dsp_oss_policy(wrch, rdch, *arg_i);
1347 #ifdef OSSV4_EXPERIMENT
1349 * XXX The following ioctls are not yet supported and just return
1352 case SNDCTL_DSP_GETOPEAKS:
1353 case SNDCTL_DSP_GETIPEAKS:
1354 chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch;
1358 oss_peaks_t *op = (oss_peaks_t *)arg;
1362 ret = chn_getpeaks(chn, &lpeak, &rpeak);
1373 case SNDCTL_DSP_COOKEDMODE:
1374 ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
1376 case SNDCTL_DSP_GET_CHNORDER:
1377 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
1379 case SNDCTL_DSP_SET_CHNORDER:
1380 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
1382 case SNDCTL_GETLABEL:
1383 ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg);
1385 case SNDCTL_SETLABEL:
1386 ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg);
1388 case SNDCTL_GETSONG:
1389 ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg);
1391 case SNDCTL_SETSONG:
1392 ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg);
1394 case SNDCTL_SETNAME:
1395 ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg);
1399 * @note The SNDCTL_CARDINFO ioctl was omitted per 4Front developer
1400 * documentation. "The usability of this call is very limited. It's
1401 * provided only for completeness of the API. OSS API doesn't have
1402 * any concept of card. Any information returned by this ioctl calld
1403 * is reserved exclusively for the utility programs included in the
1404 * OSS package. Applications should not try to use for this
1405 * information in any ways."
1407 case SNDCTL_CARDINFO:
1411 * @note The S/PDIF interface ioctls, @c SNDCTL_DSP_READCTL and
1412 * @c SNDCTL_DSP_WRITECTL have been omitted at the suggestion of
1413 * 4Front Technologies.
1415 case SNDCTL_DSP_READCTL:
1416 case SNDCTL_DSP_WRITECTL:
1419 #endif /* !0 (explicitly omitted ioctls) */
1421 #endif /* !OSSV4_EXPERIMENT */
1422 case SNDCTL_DSP_MAPINBUF:
1423 case SNDCTL_DSP_MAPOUTBUF:
1424 case SNDCTL_DSP_SETSYNCRO:
1427 case SNDCTL_DSP_SUBDIVIDE:
1428 case SOUND_PCM_WRITE_FILTER:
1429 case SOUND_PCM_READ_FILTER:
1430 /* dunno what these do, don't sound important */
1433 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
1437 relchns(i_dev, rdch, wrch, 0);
1442 dsp_poll(struct cdev *i_dev, int events, struct thread *td)
1444 struct pcm_channel *wrch = NULL, *rdch = NULL;
1448 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1451 e = (events & (POLLOUT | POLLWRNORM));
1453 ret |= chn_poll(wrch, e, td);
1456 e = (events & (POLLIN | POLLRDNORM));
1458 ret |= chn_poll(rdch, e, td);
1460 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1466 dsp_mmap(struct cdev *i_dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
1468 struct pcm_channel *wrch = NULL, *rdch = NULL, *c;
1470 if (nprot & PROT_EXEC)
1473 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1476 * XXX the linux api uses the nprot to select read/write buffer
1477 * our vm system doesn't allow this, so force write buffer
1480 if (wrch && (nprot & PROT_WRITE)) {
1482 } else if (rdch && (nprot & PROT_READ)) {
1492 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1496 if (offset >= sndbuf_getsize(c->bufsoft)) {
1497 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1501 if (!(c->flags & CHN_F_MAPPED))
1502 c->flags |= CHN_F_MAPPED;
1504 *paddr = vtophys(sndbuf_getbufofs(c->bufsoft, offset));
1505 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1513 * Clone logic is this:
1514 * x E X = {dsp, dspW, audio}
1515 * x -> x${sysctl("hw.snd.unit")}
1517 * for i N = 1 to channels of device N
1518 * if xN.i isn't busy, return its dev_t
1521 dsp_clone(void *arg, struct ucred *cred, char *name, int namelen,
1525 struct snddev_info *pcm_dev;
1526 struct snddev_channel *pcm_chan;
1527 int i, unit, devtype;
1528 static int devtypes[3] = {SND_DEV_DSP, SND_DEV_DSP16, SND_DEV_AUDIO};
1529 static char *devnames[3] = {"dsp", "dspW", "audio"};
1533 if (pcm_devclass == NULL)
1538 for (i = 0; (i < 3) && (unit == -1); i++) {
1539 devtype = devtypes[i];
1540 if (strcmp(name, devnames[i]) == 0) {
1543 if (dev_stdclone(name, NULL, devnames[i], &unit) != 1)
1547 if (unit == -1 || unit >= devclass_get_maxunit(pcm_devclass))
1550 pcm_dev = devclass_get_softc(pcm_devclass, unit);
1552 if (pcm_dev == NULL)
1555 SLIST_FOREACH(pcm_chan, &pcm_dev->channels, link) {
1559 pdev = pcm_chan->dsp_devt;
1562 pdev = pcm_chan->dspW_devt;
1565 pdev = pcm_chan->audio_devt;
1568 panic("Unknown devtype %d", devtype);
1571 if ((pdev != NULL) && (pdev->si_drv1 == NULL) && (pdev->si_drv2 == NULL)) {
1580 dsp_sysinit(void *p)
1582 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
1586 dsp_sysuninit(void *p)
1588 if (dsp_ehtag != NULL)
1589 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
1592 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
1593 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
1597 * @brief Handler for SNDCTL_AUDIOINFO.
1599 * Gathers information about the audio device specified in ai->dev. If
1600 * ai->dev == -1, then this function gathers information about the current
1601 * device. If the call comes in on a non-audio device and ai->dev == -1,
1604 * This routine is supposed to go practically straight to the hardware,
1605 * getting capabilities directly from the sound card driver, side-stepping
1606 * the intermediate channel interface.
1608 * Note, however, that the usefulness of this command is significantly
1609 * decreased when requesting info about any device other than the one serving
1610 * the request. While each snddev_channel refers to a specific device node,
1611 * the converse is *not* true. Currently, when a sound device node is opened,
1612 * the sound subsystem scans for an available audio channel (or channels, if
1613 * opened in read+write) and then assigns them to the si_drv[12] private
1614 * data fields. As a result, any information returned linking a channel to
1615 * a specific character device isn't necessarily accurate.
1618 * Calling threads must not hold any snddev_info or pcm_channel locks.
1620 * @param dev device on which the ioctl was issued
1621 * @param ai ioctl request data container
1624 * @retval EINVAL ai->dev specifies an invalid device
1626 * @todo Verify correctness of Doxygen tags. ;)
1629 dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
1631 struct snddev_channel *sce;
1632 struct pcmchan_caps *caps;
1633 struct pcm_channel *ch;
1634 struct snddev_info *d;
1635 struct cdev *t_cdev;
1637 int i, nchan, ret, *rates, minch, maxch;
1640 * If probing the device that received the ioctl, make sure it's a
1641 * DSP device. (Users may use this ioctl with /dev/mixer and
1644 if ((ai->dev == -1) && (i_dev->si_devsw != &dsp_cdevsw))
1653 * Search for the requested audio device (channel). Start by
1654 * iterating over pcm devices.
1656 for (i = 0; i < devclass_get_maxunit(pcm_devclass); i++) {
1657 d = devclass_get_softc(pcm_devclass, i);
1661 /* See the note in function docblock */
1662 mtx_assert(d->lock, MA_NOTOWNED);
1666 SLIST_FOREACH(sce, &d->channels, link) {
1668 mtx_assert(ch->lock, MA_NOTOWNED);
1670 if (ai->dev == -1) {
1671 if ((ch == i_dev->si_drv1) || /* record ch */
1672 (ch == i_dev->si_drv2)) { /* playback ch */
1676 } else if (ai->dev == nchan) {
1677 t_cdev = sce->dsp_devt;
1688 /* Exhausted the search -- nothing is locked, so return. */
1692 /* Should've found the device, but something isn't right */
1693 if (t_cdev == NULL) {
1699 * At this point, the following synchronization stuff has happened:
1700 * - a specific PCM device is locked and its "in progress
1701 * operations" counter has been incremented, so be sure to unlock
1702 * and decrement when exiting;
1703 * - a specific audio channel has been locked, so be sure to unlock
1707 caps = chn_getcaps(ch);
1710 * With all handles collected, zero out the user's container and
1711 * begin filling in its fields.
1713 bzero((void *)ai, sizeof(oss_audioinfo));
1716 strlcpy(ai->name, ch->name, sizeof(ai->name));
1718 if ((ch->flags & CHN_F_BUSY) == 0)
1721 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
1725 * @c cmd - OSSv4 docs: "Only supported under Linux at this moment."
1726 * Cop-out, I know, but I'll save running around in the process
1727 * table for later. Is there a risk of leaking information?
1732 * These flags stolen from SNDCTL_DSP_GETCAPS handler. Note, however,
1733 * that a single channel operates in only one direction, so
1734 * DSP_CAP_DUPLEX is out.
1737 * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep these in
1740 ai->caps = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
1743 * Collect formats supported @b natively by the device. Also
1744 * determine min/max channels. (I.e., mono, stereo, or both?)
1746 * If any channel is stereo, maxch = 2;
1747 * if all channels are stereo, minch = 2, too;
1748 * if any channel is mono, minch = 1;
1749 * and if all channels are mono, maxch = 1.
1754 for (i = 0; caps->fmtlist[i]; i++) {
1755 fmts |= caps->fmtlist[i];
1756 if (caps->fmtlist[i] & AFMT_STEREO) {
1757 minch = (minch == 0) ? 2 : minch;
1761 maxch = (maxch == 0) ? 1 : maxch;
1765 if (ch->direction == PCMDIR_PLAY)
1766 ai->oformats = fmts;
1768 ai->iformats = fmts;
1772 * @c magic - OSSv4 docs: "Reserved for internal use by OSS."
1775 * @c card_number - OSSv4 docs: "Number of the sound card where this
1776 * device belongs or -1 if this information is not available.
1777 * Applications should normally not use this field for any
1780 ai->card_number = -1;
1782 * @todo @c song_name - depends first on SNDCTL_[GS]ETSONG
1783 * @todo @c label - depends on SNDCTL_[GS]ETLABEL
1784 * @todo @c port_number - routing information?
1786 ai->port_number = -1;
1787 ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1;
1790 * @c real_device - OSSv4 docs: "Obsolete."
1792 ai->real_device = -1;
1793 strlcpy(ai->devnode, t_cdev->si_name, sizeof(ai->devnode));
1794 ai->enabled = device_is_attached(d->dev) ? 1 : 0;
1797 * @c flags - OSSv4 docs: "Reserved for future use."
1800 * @c binding - OSSv4 docs: "Reserved for future use."
1802 * @todo @c handle - haven't decided how to generate this yet; bus,
1803 * vendor, device IDs?
1805 ai->min_rate = caps->minspeed;
1806 ai->max_rate = caps->maxspeed;
1808 ai->min_channels = minch;
1809 ai->max_channels = maxch;
1811 ai->nrates = chn_getrates(ch, &rates);
1812 if (ai->nrates > MAX_SAMPLE_RATES)
1813 ai->nrates = MAX_SAMPLE_RATES;
1815 for (i = 0; i < ai->nrates; i++)
1816 ai->rates[i] = rates[i];
1827 * @brief Assigns a PCM channel to a sync group.
1829 * Sync groups are used to enable audio operations on multiple devices
1830 * simultaneously. They may be used with any number of devices and may
1831 * span across applications. Devices are added to groups with
1832 * the SNDCTL_DSP_SYNCGROUP ioctl, and operations are triggered with the
1833 * SNDCTL_DSP_SYNCSTART ioctl.
1835 * If the @c id field of the @c group parameter is set to zero, then a new
1836 * sync group is created. Otherwise, wrch and rdch (if set) are added to
1837 * the group specified.
1839 * @todo As far as memory allocation, should we assume that things are
1840 * okay and allocate with M_WAITOK before acquiring channel locks,
1841 * freeing later if not?
1843 * @param wrch output channel associated w/ device (if any)
1844 * @param rdch input channel associated w/ device (if any)
1845 * @param group Sync group parameters
1848 * @retval non-zero error to be propagated upstream
1851 dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
1853 struct pcmchan_syncmember *smrd, *smwr;
1854 struct pcmchan_syncgroup *sg;
1863 * Free_unr() may sleep, so store released syncgroup IDs until after
1864 * all locks are released.
1866 sg_ids[0] = sg_ids[1] = sg_ids[2] = 0;
1871 * - Insert channel(s) into group's member list.
1872 * - Set CHN_F_NOTRIGGER on channel(s).
1873 * - Stop channel(s).
1877 * If device's channels are already mapped to a group, unmap them.
1881 sg_ids[0] = chn_syncdestroy(wrch);
1886 sg_ids[1] = chn_syncdestroy(rdch);
1890 * Verify that mode matches character device properites.
1891 * - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
1892 * - Bail if PCM_ENABLE_INPUT && rdch == NULL.
1894 if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) ||
1895 ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) {
1901 * An id of zero indicates the user wants to create a new
1904 if (group->id == 0) {
1905 sg = (struct pcmchan_syncgroup *)malloc(sizeof(*sg), M_DEVBUF, M_NOWAIT);
1907 SLIST_INIT(&sg->members);
1908 sg->id = alloc_unr(pcmsg_unrhdr);
1911 SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link);
1915 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
1916 if (sg->id == group->id)
1923 /* Couldn't create or find a syncgroup. Fail. */
1928 * Allocate a syncmember, assign it and a channel together, and
1929 * insert into syncgroup.
1931 if (group->mode & PCM_ENABLE_INPUT) {
1932 smrd = (struct pcmchan_syncmember *)malloc(sizeof(*smrd), M_DEVBUF, M_NOWAIT);
1938 SLIST_INSERT_HEAD(&sg->members, smrd, link);
1943 rdch->flags |= CHN_F_NOTRIGGER;
1947 if (group->mode & PCM_ENABLE_OUTPUT) {
1948 smwr = (struct pcmchan_syncmember *)malloc(sizeof(*smwr), M_DEVBUF, M_NOWAIT);
1954 SLIST_INSERT_HEAD(&sg->members, smwr, link);
1959 wrch->flags |= CHN_F_NOTRIGGER;
1967 free(smrd, M_DEVBUF);
1969 free(smwr, M_DEVBUF);
1970 if ((sg != NULL) && SLIST_EMPTY(&sg->members)) {
1972 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
1990 free_unr(pcmsg_unrhdr, sg_ids[0]);
1992 free_unr(pcmsg_unrhdr, sg_ids[1]);
1994 free_unr(pcmsg_unrhdr, sg_ids[2]);
2000 * @brief Launch a sync group into action
2002 * Sync groups are established via SNDCTL_DSP_SYNCGROUP. This function
2003 * iterates over all members, triggering them along the way.
2005 * @note Caller must not hold any channel locks.
2007 * @param sg_id sync group identifier
2010 * @retval non-zero error worthy of propagating upstream to user
2013 dsp_oss_syncstart(int sg_id)
2015 struct pcmchan_syncmember *sm, *sm_tmp;
2016 struct pcmchan_syncgroup *sg;
2017 struct pcm_channel *c;
2020 /* Get the synclists lock */
2027 /* Search for syncgroup by ID */
2028 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2029 if (sg->id == sg_id)
2033 /* Return EINVAL if not found */
2039 /* Any removals resulting in an empty group should've handled this */
2040 KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup"));
2043 * Attempt to lock all member channels - if any are already
2044 * locked, unlock those acquired, sleep for a bit, and try
2047 SLIST_FOREACH(sm, &sg->members, link) {
2048 if (CHN_TRYLOCK(sm->ch) == 0) {
2049 int timo = hz * 5/1000;
2053 /* Release all locked channels so far, retry */
2054 SLIST_FOREACH(sm_tmp, &sg->members, link) {
2055 /* sm is the member already locked */
2058 CHN_UNLOCK(sm_tmp->ch);
2061 /** @todo Is PRIBIO correct/ */
2062 ret = msleep(sm, &snd_pcm_syncgroups_mtx, PRIBIO | PCATCH, "pcmsgrp", timo);
2063 if (ret == EINTR || ret == ERESTART)
2067 ret = 0; /* Assumes ret == EWOULDBLOCK... */
2070 } while (needlocks && ret == 0);
2072 /* Proceed only if no errors encountered. */
2074 /* Launch channels */
2075 while((sm = SLIST_FIRST(&sg->members)) != NULL) {
2076 SLIST_REMOVE_HEAD(&sg->members, link);
2081 c->flags &= ~CHN_F_NOTRIGGER;
2087 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2094 * Free_unr() may sleep, so be sure to give up the syncgroup lock
2098 free_unr(pcmsg_unrhdr, sg_id);
2104 * @brief Handler for SNDCTL_DSP_POLICY
2106 * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment
2107 * size and count like with SNDCTL_DSP_SETFRAGMENT. Instead of the user
2108 * specifying those two parameters, s/he simply selects a number from 0..10
2109 * which corresponds to a buffer size. Smaller numbers request smaller
2110 * buffers with lower latencies (at greater overhead from more frequent
2111 * interrupts), while greater numbers behave in the opposite manner.
2113 * The 4Front spec states that a value of 5 should be the default. However,
2114 * this implementation deviates slightly by using a linear scale without
2115 * consulting drivers. I.e., even though drivers may have different default
2116 * buffer sizes, a policy argument of 5 will have the same result across
2119 * See http://manuals.opensound.com/developer/SNDCTL_DSP_POLICY.html for
2122 * @todo When SNDCTL_DSP_COOKEDMODE is supported, it'll be necessary to
2123 * work with hardware drivers directly.
2125 * @note PCM channel arguments must not be locked by caller.
2127 * @param wrch Pointer to opened playback channel (optional; may be NULL)
2128 * @param rdch " recording channel (optional; may be NULL)
2129 * @param policy Integer from [0:10]
2131 * @retval 0 constant (for now)
2134 dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
2136 int fragln, fragsz, maxfrags, ret;
2138 /* Default: success */
2141 /* Scale policy [0..10] to fragment size [2^4..2^16]. */
2143 RANGE(fragln, 0, 10);
2145 fragsz = 1 << fragln;
2147 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
2151 ret = chn_setblocksize(rdch, maxfrags, fragsz);
2155 if (wrch && ret == 0) {
2157 ret = chn_setblocksize(wrch, maxfrags, fragsz);
2164 #ifdef OSSV4_EXPERIMENT
2166 * @brief Enable or disable "cooked" mode
2168 * This is a handler for @c SNDCTL_DSP_COOKEDMODE. When in cooked mode, which
2169 * is the default, the sound system handles rate and format conversions
2170 * automatically (ex: user writing 11025Hz/8 bit/unsigned but card only
2171 * operates with 44100Hz/16bit/signed samples).
2173 * Disabling cooked mode is intended for applications wanting to mmap()
2174 * a sound card's buffer space directly, bypassing the FreeBSD 2-stage
2175 * feeder architecture, presumably to gain as much control over audio
2176 * hardware as possible.
2178 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_COOKEDMODE.html
2181 * @note Currently, this function is just a stub that always returns EINVAL.
2183 * @todo Figure out how to and actually implement this.
2185 * @param wrch playback channel (optional; may be NULL)
2186 * @param rdch recording channel (optional; may be NULL)
2187 * @param enabled 0 = raw mode, 1 = cooked mode
2189 * @retval EINVAL Operation not yet supported.
2192 dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
2198 * @brief Retrieve channel interleaving order
2200 * This is the handler for @c SNDCTL_DSP_GET_CHNORDER.
2202 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html
2205 * @note As the ioctl definition is still under construction, FreeBSD
2206 * does not currently support SNDCTL_DSP_GET_CHNORDER.
2208 * @param wrch playback channel (optional; may be NULL)
2209 * @param rdch recording channel (optional; may be NULL)
2210 * @param map channel map (result will be stored there)
2212 * @retval EINVAL Operation not yet supported.
2215 dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
2221 * @brief Specify channel interleaving order
2223 * This is the handler for @c SNDCTL_DSP_SET_CHNORDER.
2225 * @note As the ioctl definition is still under construction, FreeBSD
2226 * does not currently support @c SNDCTL_DSP_SET_CHNORDER.
2228 * @param wrch playback channel (optional; may be NULL)
2229 * @param rdch recording channel (optional; may be NULL)
2230 * @param map channel map
2232 * @retval EINVAL Operation not yet supported.
2235 dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
2241 * @brief Retrieve an audio device's label
2243 * This is a handler for the @c SNDCTL_GETLABEL ioctl.
2245 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
2248 * From Hannu@4Front: "For example ossxmix (just like some HW mixer
2249 * consoles) can show variable "labels" for certain controls. By default
2250 * the application name (say quake) is shown as the label but
2251 * applications may change the labels themselves."
2253 * @note As the ioctl definition is still under construction, FreeBSD
2254 * does not currently support @c SNDCTL_GETLABEL.
2256 * @param wrch playback channel (optional; may be NULL)
2257 * @param rdch recording channel (optional; may be NULL)
2258 * @param label label gets copied here
2260 * @retval EINVAL Operation not yet supported.
2263 dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
2269 * @brief Specify an audio device's label
2271 * This is a handler for the @c SNDCTL_SETLABEL ioctl. Please see the
2272 * comments for @c dsp_oss_getlabel immediately above.
2274 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
2277 * @note As the ioctl definition is still under construction, FreeBSD
2278 * does not currently support SNDCTL_SETLABEL.
2280 * @param wrch playback channel (optional; may be NULL)
2281 * @param rdch recording channel (optional; may be NULL)
2282 * @param label label gets copied from here
2284 * @retval EINVAL Operation not yet supported.
2287 dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
2293 * @brief Retrieve name of currently played song
2295 * This is a handler for the @c SNDCTL_GETSONG ioctl. Audio players could
2296 * tell the system the name of the currently playing song, which would be
2297 * visible in @c /dev/sndstat.
2299 * See @c http://manuals.opensound.com/developer/SNDCTL_GETSONG.html
2302 * @note As the ioctl definition is still under construction, FreeBSD
2303 * does not currently support SNDCTL_GETSONG.
2305 * @param wrch playback channel (optional; may be NULL)
2306 * @param rdch recording channel (optional; may be NULL)
2307 * @param song song name gets copied here
2309 * @retval EINVAL Operation not yet supported.
2312 dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
2318 * @brief Retrieve name of currently played song
2320 * This is a handler for the @c SNDCTL_SETSONG ioctl. Audio players could
2321 * tell the system the name of the currently playing song, which would be
2322 * visible in @c /dev/sndstat.
2324 * See @c http://manuals.opensound.com/developer/SNDCTL_SETSONG.html
2327 * @note As the ioctl definition is still under construction, FreeBSD
2328 * does not currently support SNDCTL_SETSONG.
2330 * @param wrch playback channel (optional; may be NULL)
2331 * @param rdch recording channel (optional; may be NULL)
2332 * @param song song name gets copied from here
2334 * @retval EINVAL Operation not yet supported.
2337 dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
2343 * @brief Rename a device
2345 * This is a handler for the @c SNDCTL_SETNAME ioctl.
2347 * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for
2350 * From Hannu@4Front: "This call is used to change the device name
2351 * reported in /dev/sndstat and ossinfo. So instead of using some generic
2352 * 'OSS loopback audio (MIDI) driver' the device may be given a meaningfull
2353 * name depending on the current context (for example 'OSS virtual wave table
2354 * synth' or 'VoIP link to London')."
2356 * @note As the ioctl definition is still under construction, FreeBSD
2357 * does not currently support SNDCTL_SETNAME.
2359 * @param wrch playback channel (optional; may be NULL)
2360 * @param rdch recording channel (optional; may be NULL)
2361 * @param name new device name gets copied from here
2363 * @retval EINVAL Operation not yet supported.
2366 dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
2370 #endif /* !OSSV4_EXPERIMENT */