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
26 * $FreeBSD: src/sys/dev/sound/pcm/dsp.c,v 1.80.2.6 2006/04/04 17:43:48 ariff Exp $
27 * $DragonFly: src/sys/dev/sound/pcm/dsp.c,v 1.14 2007/01/04 21:47:03 corecode Exp $
30 #include <sys/param.h>
31 #include <sys/queue.h>
33 #include <dev/sound/pcm/sound.h>
35 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/dsp.c,v 1.14 2007/01/04 21:47:03 corecode Exp $");
39 static d_open_t dsp_open;
40 static d_close_t dsp_close;
41 static d_read_t dsp_read;
42 static d_write_t dsp_write;
43 static d_ioctl_t dsp_ioctl;
44 static d_poll_t dsp_poll;
45 static d_mmap_t dsp_mmap;
47 struct dev_ops dsp_cdevsw = {
48 { "dsp", SND_CDEV_MAJOR, 0},
49 /*.d_flags = D_NEEDGIANT,*/
60 static eventhandler_tag dsp_ehtag;
63 static struct snddev_info *
64 dsp_get_info(struct cdev *dev)
66 struct snddev_info *d;
70 if (unit >= devclass_get_maxunit(pcm_devclass))
72 d = devclass_get_softc(pcm_devclass, unit);
78 dsp_get_flags(struct cdev *dev)
84 if (unit >= devclass_get_maxunit(pcm_devclass))
86 bdev = devclass_get_device(pcm_devclass, unit);
88 return pcm_getflags(bdev);
92 dsp_set_flags(struct cdev *dev, u_int32_t flags)
98 if (unit >= devclass_get_maxunit(pcm_devclass))
100 bdev = devclass_get_device(pcm_devclass, unit);
102 pcm_setflags(bdev, flags);
106 * return the channels associated with an open device instance.
107 * set the priority if the device is simplex and one direction (only) is
109 * lock channels specified.
112 getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch, u_int32_t prio)
114 struct snddev_info *d;
117 flags = dsp_get_flags(dev);
118 d = dsp_get_info(dev);
121 KASSERT((flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
122 ("getchns: read and write both prioritised"));
124 if ((flags & SD_F_PRIO_SET) == 0 && (prio != (SD_F_PRIO_RD | SD_F_PRIO_WR))) {
125 flags |= prio & (SD_F_PRIO_RD | SD_F_PRIO_WR);
126 dsp_set_flags(dev, flags);
129 *rdch = dev->si_drv1;
130 *wrch = dev->si_drv2;
131 if ((flags & SD_F_SIMPLEX) && (flags & SD_F_PRIO_SET)) {
133 if (*rdch && flags & SD_F_PRIO_WR) {
135 *rdch = pcm_getfakechan(d);
136 } else if (*wrch && flags & SD_F_PRIO_RD) {
138 *wrch = pcm_getfakechan(d);
142 pcm_getfakechan(d)->flags |= CHN_F_BUSY;
146 if (*rdch && *rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
148 if (*wrch && *wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
154 /* unlock specified channels */
156 relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch, u_int32_t prio)
158 struct snddev_info *d;
160 d = dsp_get_info(dev);
161 if (wrch && wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
163 if (rdch && rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
169 dsp_open(struct dev_open_args *ap)
171 struct cdev *i_dev = ap->a_head.a_dev;
172 struct thread *td = curthread;
173 int flags = ap->a_oflags;
174 struct pcm_channel *rdch, *wrch;
175 struct snddev_info *d;
181 if (i_dev == NULL || td == NULL)
184 if ((flags & (FREAD | FWRITE)) == 0)
187 d = dsp_get_info(i_dev);
188 devtype = PCMDEV(i_dev);
191 /* decide default format */
205 case SND_DEV_NORESET:
213 chnum = PCMCHAN(i_dev);
217 panic("impossible devtype %d", devtype);
220 /* lock snddev so nobody else can monkey with it */
223 rdch = i_dev->si_drv1;
224 wrch = i_dev->si_drv2;
226 if (rdch || wrch || ((dsp_get_flags(i_dev) & SD_F_SIMPLEX) &&
227 (flags & (FREAD | FWRITE)) == (FREAD | FWRITE))) {
228 /* simplex or not, better safe than sorry. */
234 * if we get here, the open request is valid- either:
235 * * we were previously not open
236 * * we were open for play xor record and the opener wants
237 * the non-open direction
242 error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, chnum);
243 if (error != 0 && error != EBUSY && chnum != -1 && (flags & FWRITE))
244 error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, -1);
246 if (error == 0 && (chn_reset(rdch, fmt) ||
247 (fmt && chn_setspeed(rdch, DSP_DEFAULT_SPEED))))
252 pcm_chnrelease(rdch);
256 if (flags & O_NONBLOCK)
257 rdch->flags |= CHN_F_NBIO;
263 if (flags & FWRITE) {
266 error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, chnum);
267 if (error != 0 && error != EBUSY && chnum != -1 && (flags & FREAD))
268 error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, -1);
270 if (error == 0 && (chn_reset(wrch, fmt) ||
271 (fmt && chn_setspeed(wrch, DSP_DEFAULT_SPEED))))
276 pcm_chnrelease(wrch);
279 * Lock, deref and release previously created record channel
282 pcm_chnref(rdch, -1);
283 pcm_chnrelease(rdch);
289 if (flags & O_NONBLOCK)
290 wrch->flags |= CHN_F_NBIO;
296 i_dev->si_drv1 = rdch;
297 i_dev->si_drv2 = wrch;
304 dsp_close(struct dev_close_args *ap)
306 struct cdev *i_dev = ap->a_head.a_dev;
307 struct pcm_channel *rdch, *wrch;
308 struct snddev_info *d;
311 d = dsp_get_info(i_dev);
313 rdch = i_dev->si_drv1;
314 wrch = i_dev->si_drv2;
321 refs += pcm_chnref(rdch, -1);
322 chn_abort(rdch); /* won't sleep */
323 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
325 pcm_chnrelease(rdch);
329 refs += pcm_chnref(wrch, -1);
331 * XXX: Maybe the right behaviour is to abort on non_block.
332 * It seems that mplayer flushes the audio queue by quickly
333 * closing and re-opening. In FBSD, there's a long pause
334 * while the audio queue flushes that I presume isn't there in
337 chn_flush(wrch); /* may sleep */
338 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
340 pcm_chnrelease(wrch);
345 i_dev->si_drv1 = NULL;
347 i_dev->si_drv2 = NULL;
349 * If there are no more references, release the channels.
351 if (refs == 0 && i_dev->si_drv1 == NULL &&
352 i_dev->si_drv2 == NULL) {
353 if (pcm_getfakechan(d))
354 pcm_getfakechan(d)->flags = 0;
355 /* What is this?!? */
356 dsp_set_flags(i_dev, dsp_get_flags(i_dev) & ~SD_F_TRANSIENT);
364 dsp_read(struct dev_read_args *ap)
366 struct cdev *i_dev = ap->a_head.a_dev;
367 struct uio *buf = ap->a_uio;
368 int flag = ap->a_ioflag;
369 struct pcm_channel *rdch, *wrch;
372 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD);
374 KASSERT(rdch, ("dsp_read: nonexistant channel"));
375 KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel"));
377 if (rdch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
378 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
381 if (!(rdch->flags & CHN_F_RUNNING))
382 rdch->flags |= CHN_F_RUNNING;
383 ret = chn_read(rdch, buf, flag);
384 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
390 dsp_write(struct dev_write_args *ap)
392 struct cdev *i_dev = ap->a_head.a_dev;
393 struct uio *buf = ap->a_uio;
394 int flag = ap->a_ioflag;
395 struct pcm_channel *rdch, *wrch;
398 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_WR);
400 KASSERT(wrch, ("dsp_write: nonexistant channel"));
401 KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel"));
403 if (wrch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
404 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
407 if (!(wrch->flags & CHN_F_RUNNING))
408 wrch->flags |= CHN_F_RUNNING;
409 ret = chn_write(wrch, buf, flag);
410 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
416 dsp_ioctl(struct dev_ioctl_args *ap)
418 struct cdev *i_dev = ap->a_head.a_dev;
419 u_long cmd = ap->a_cmd;
420 caddr_t arg = ap->a_data;
421 struct pcm_channel *chn, *rdch, *wrch;
422 struct snddev_info *d;
424 int ret = 0, *arg_i = (int *)arg, tmp;
427 * this is an evil hack to allow broken apps to perform mixer ioctls
431 d = dsp_get_info(i_dev);
432 if (IOCGROUP(cmd) == 'M') {
434 * This is at least, a bug to bug compatible with OSS.
436 if (d->mixer_dev != NULL) {
437 ap->a_head.a_dev = d->mixer_dev;
438 return mixer_ioctl(ap);
444 getchns(i_dev, &rdch, &wrch, 0);
447 if (wrch && (wrch->flags & CHN_F_DEAD))
449 if (rdch && (rdch->flags & CHN_F_DEAD))
452 relchns(i_dev, rdch, wrch, 0);
463 * we start with the new ioctl interface.
465 case AIONWRITE: /* how many bytes can write ? */
469 if (wrch && wrch->bufhard.dl)
470 while (chn_wrfeed(wrch) == 0);
472 *arg_i = sndbuf_getfree(wrch->bufsoft);
480 case AIOSSIZE: /* set the current blocksize */
482 struct snd_size *p = (struct snd_size *)arg;
488 chn_setblocksize(wrch, 2, p->play_size);
489 p->play_size = sndbuf_getblksz(wrch->bufsoft);
494 chn_setblocksize(rdch, 2, p->rec_size);
495 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
500 case AIOGSIZE: /* get the current blocksize */
502 struct snd_size *p = (struct snd_size *)arg;
506 p->play_size = sndbuf_getblksz(wrch->bufsoft);
511 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
520 snd_chan_param *p = (snd_chan_param *)arg;
522 if (cmd == AIOSFMT &&
523 ((p->play_format != 0 && p->play_rate == 0) ||
524 (p->rec_format != 0 && p->rec_rate == 0))) {
530 if (cmd == AIOSFMT && p->play_format != 0) {
531 chn_setformat(wrch, p->play_format);
532 chn_setspeed(wrch, p->play_rate);
534 p->play_rate = wrch->speed;
535 p->play_format = wrch->format;
543 if (cmd == AIOSFMT && p->rec_format != 0) {
544 chn_setformat(rdch, p->rec_format);
545 chn_setspeed(rdch, p->rec_rate);
547 p->rec_rate = rdch->speed;
548 p->rec_format = rdch->format;
557 case AIOGCAP: /* get capabilities */
559 snd_capabilities *p = (snd_capabilities *)arg;
560 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
565 rcaps = chn_getcaps(rdch);
569 pcaps = chn_getcaps(wrch);
571 p->rate_min = max(rcaps? rcaps->minspeed : 0,
572 pcaps? pcaps->minspeed : 0);
573 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
574 pcaps? pcaps->maxspeed : 1000000);
575 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
576 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
577 /* XXX bad on sb16 */
578 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
579 (wrch? chn_getformats(wrch) : 0xffffffff);
581 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
583 p->mixers = 1; /* default: one mixer */
584 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
585 p->left = p->right = 100;
594 if (*arg_i == AIOSYNC_PLAY && wrch) {
596 *arg_i = chn_abort(wrch);
598 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
600 *arg_i = chn_abort(rdch);
603 kprintf("AIOSTOP: bad channel 0x%x\n", *arg_i);
609 kprintf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
610 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
614 * here follow the standard ioctls (filio.h etc.)
616 case FIONREAD: /* get # bytes to read */
619 /* if (rdch && rdch->bufhard.dl)
620 while (chn_rdfeed(rdch) == 0);
622 *arg_i = sndbuf_getready(rdch->bufsoft);
630 case FIOASYNC: /*set/clear async i/o */
631 DEB( kprintf("FIOASYNC\n") ; )
634 case SNDCTL_DSP_NONBLOCK:
635 case FIONBIO: /* set/clear non-blocking i/o */
639 rdch->flags |= CHN_F_NBIO;
641 rdch->flags &= ~CHN_F_NBIO;
647 wrch->flags |= CHN_F_NBIO;
649 wrch->flags &= ~CHN_F_NBIO;
655 * Finally, here is the linux-compatible ioctl interface
657 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
658 case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
659 case SNDCTL_DSP_GETBLKSIZE:
660 chn = wrch ? wrch : rdch;
663 *arg_i = sndbuf_getblksz(chn->bufsoft);
671 case SNDCTL_DSP_SETBLKSIZE:
672 RANGE(*arg_i, 16, 65536);
675 chn_setblocksize(wrch, 2, *arg_i);
680 chn_setblocksize(rdch, 2, *arg_i);
685 case SNDCTL_DSP_RESET:
686 DEB(kprintf("dsp reset\n"));
701 case SNDCTL_DSP_SYNC:
702 DEB(kprintf("dsp sync\n"));
703 /* chn_sync may sleep */
706 chn_sync(wrch, sndbuf_getsize(wrch->bufsoft) - 4);
711 case SNDCTL_DSP_SPEED:
712 /* chn_setspeed may sleep */
716 ret = chn_setspeed(wrch, *arg_i);
720 if (rdch && ret == 0) {
722 ret = chn_setspeed(rdch, *arg_i);
730 case SOUND_PCM_READ_RATE:
731 chn = wrch ? wrch : rdch;
742 case SNDCTL_DSP_STEREO:
744 *arg_i = (*arg_i)? AFMT_STEREO : 0;
747 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
748 tmp = (wrch->format & AFMT_STEREO)? 1 : 0;
751 if (rdch && ret == 0) {
753 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
755 tmp = (rdch->format & AFMT_STEREO)? 1 : 0;
761 case SOUND_PCM_WRITE_CHANNELS:
762 /* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
765 *arg_i = (*arg_i != 1)? AFMT_STEREO : 0;
768 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
769 tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
772 if (rdch && ret == 0) {
774 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
776 tmp = (rdch->format & AFMT_STEREO)? 2 : 1;
781 chn = wrch ? wrch : rdch;
783 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
788 case SOUND_PCM_READ_CHANNELS:
789 chn = wrch ? wrch : rdch;
792 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
800 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
801 chn = wrch ? wrch : rdch;
804 *arg_i = chn_getformats(chn);
812 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
813 if ((*arg_i != AFMT_QUERY)) {
817 ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
818 tmp = wrch->format & ~AFMT_STEREO;
821 if (rdch && ret == 0) {
823 ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
825 tmp = rdch->format & ~AFMT_STEREO;
830 chn = wrch ? wrch : rdch;
832 *arg_i = chn->format & ~AFMT_STEREO;
837 case SNDCTL_DSP_SETFRAGMENT:
838 DEB(kprintf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
840 u_int32_t fragln = (*arg_i) & 0x0000ffff;
841 u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
843 u_int32_t r_maxfrags, r_fragsz;
845 RANGE(fragln, 4, 16);
846 fragsz = 1 << fragln;
849 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
852 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
853 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
855 DEB(kprintf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
858 ret = chn_setblocksize(rdch, maxfrags, fragsz);
859 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
860 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
863 r_maxfrags = maxfrags;
866 if (wrch && ret == 0) {
868 ret = chn_setblocksize(wrch, maxfrags, fragsz);
869 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
870 fragsz = sndbuf_getblksz(wrch->bufsoft);
872 } else { /* use whatever came from the read channel */
873 maxfrags = r_maxfrags;
882 *arg_i = (maxfrags << 16) | fragln;
886 case SNDCTL_DSP_GETISPACE:
887 /* return the size of data available in the input queue */
889 audio_buf_info *a = (audio_buf_info *)arg;
891 struct snd_dbuf *bs = rdch->bufsoft;
894 a->bytes = sndbuf_getready(bs);
895 a->fragments = a->bytes / sndbuf_getblksz(bs);
896 a->fragstotal = sndbuf_getblkcnt(bs);
897 a->fragsize = sndbuf_getblksz(bs);
903 case SNDCTL_DSP_GETOSPACE:
904 /* return space available in the output queue */
906 audio_buf_info *a = (audio_buf_info *)arg;
908 struct snd_dbuf *bs = wrch->bufsoft;
911 /* XXX abusive DMA update: chn_wrupdate(wrch); */
912 a->bytes = sndbuf_getfree(bs);
913 a->fragments = a->bytes / sndbuf_getblksz(bs);
914 a->fragstotal = sndbuf_getblkcnt(bs);
915 a->fragsize = sndbuf_getblksz(bs);
921 case SNDCTL_DSP_GETIPTR:
923 count_info *a = (count_info *)arg;
925 struct snd_dbuf *bs = rdch->bufsoft;
928 /* XXX abusive DMA update: chn_rdupdate(rdch); */
929 a->bytes = sndbuf_gettotal(bs);
930 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
931 a->ptr = sndbuf_getreadyptr(bs);
932 rdch->blocks = sndbuf_getblocks(bs);
939 case SNDCTL_DSP_GETOPTR:
941 count_info *a = (count_info *)arg;
943 struct snd_dbuf *bs = wrch->bufsoft;
946 /* XXX abusive DMA update: chn_wrupdate(wrch); */
947 a->bytes = sndbuf_gettotal(bs);
948 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
949 a->ptr = sndbuf_getreadyptr(bs);
950 wrch->blocks = sndbuf_getblocks(bs);
957 case SNDCTL_DSP_GETCAPS:
958 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
959 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
960 *arg_i |= DSP_CAP_DUPLEX;
963 case SOUND_PCM_READ_BITS:
964 chn = wrch ? wrch : rdch;
967 if (chn->format & AFMT_8BIT)
969 else if (chn->format & AFMT_16BIT)
971 else if (chn->format & AFMT_24BIT)
973 else if (chn->format & AFMT_32BIT)
984 case SNDCTL_DSP_SETTRIGGER:
987 rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
988 if (*arg_i & PCM_ENABLE_INPUT)
991 rdch->flags |= CHN_F_NOTRIGGER;
996 wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
997 if (*arg_i & PCM_ENABLE_OUTPUT)
1000 wrch->flags |= CHN_F_NOTRIGGER;
1005 case SNDCTL_DSP_GETTRIGGER:
1009 if (wrch->flags & CHN_F_TRIGGERED)
1010 *arg_i |= PCM_ENABLE_OUTPUT;
1015 if (rdch->flags & CHN_F_TRIGGERED)
1016 *arg_i |= PCM_ENABLE_INPUT;
1021 case SNDCTL_DSP_GETODELAY:
1023 struct snd_dbuf *b = wrch->bufhard;
1024 struct snd_dbuf *bs = wrch->bufsoft;
1027 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1028 *arg_i = sndbuf_getready(b) + sndbuf_getready(bs);
1034 case SNDCTL_DSP_POST:
1037 wrch->flags &= ~CHN_F_NOTRIGGER;
1043 case SNDCTL_DSP_SETDUPLEX:
1045 * switch to full-duplex mode if card is in half-duplex
1046 * mode and is able to work in full-duplex mode
1048 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1049 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1052 case SNDCTL_DSP_MAPINBUF:
1053 case SNDCTL_DSP_MAPOUTBUF:
1054 case SNDCTL_DSP_SETSYNCRO:
1057 case SNDCTL_DSP_SUBDIVIDE:
1058 case SOUND_PCM_WRITE_FILTER:
1059 case SOUND_PCM_READ_FILTER:
1060 /* dunno what these do, don't sound important */
1063 DEB(kprintf("default ioctl fn 0x%08lx fail\n", cmd));
1067 relchns(i_dev, rdch, wrch, 0);
1072 dsp_poll(struct dev_poll_args *ap)
1074 struct cdev *i_dev = ap->a_head.a_dev;
1075 int events = ap->a_events;
1076 struct thread *td = curthread;
1077 struct pcm_channel *wrch = NULL, *rdch = NULL;
1081 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1084 e = (events & (POLLOUT | POLLWRNORM));
1086 ret |= chn_poll(wrch, e, td);
1089 e = (events & (POLLIN | POLLRDNORM));
1091 ret |= chn_poll(rdch, e, td);
1093 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1100 dsp_mmap(struct dev_mmap_args *ap)
1102 struct cdev *i_dev = ap->a_head.a_dev;
1103 vm_offset_t offset = ap->a_offset;
1104 int nprot = ap->a_nprot;
1105 struct pcm_channel *wrch = NULL, *rdch = NULL, *c;
1107 if (nprot & PROT_EXEC)
1110 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1113 * XXX the linux api uses the nprot to select read/write buffer
1114 * our vm system doesn't allow this, so force write buffer
1117 if (wrch && (nprot & PROT_WRITE)) {
1119 } else if (rdch && (nprot & PROT_READ)) {
1129 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1133 if (offset >= sndbuf_getsize(c->bufsoft)) {
1134 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1138 if (!(c->flags & CHN_F_MAPPED))
1139 c->flags |= CHN_F_MAPPED;
1141 ap->a_result = vtophys(sndbuf_getbufofs(c->bufsoft, offset));
1142 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1150 * Clone logic is this:
1151 * x E X = {dsp, dspW, audio}
1152 * x -> x${sysctl("hw.snd.unit")}
1154 * for i N = 1 to channels of device N
1155 * if xN.i isn't busy, return its dev_t
1158 dsp_clone(void *arg, struct ucred *cred, char *name, int namelen,
1162 struct snddev_info *pcm_dev;
1163 struct snddev_channel *pcm_chan;
1164 int i, unit, devtype;
1165 static int devtypes[3] = {SND_DEV_DSP, SND_DEV_DSP16, SND_DEV_AUDIO};
1166 static char *devnames[3] = {"dsp", "dspW", "audio"};
1170 if (pcm_devclass == NULL)
1175 for (i = 0; (i < 3) && (unit == -1); i++) {
1176 devtype = devtypes[i];
1177 if (strcmp(name, devnames[i]) == 0) {
1180 if (dev_stdclone(name, NULL, devnames[i], &unit) != 1)
1184 if (unit == -1 || unit >= devclass_get_maxunit(pcm_devclass))
1187 pcm_dev = devclass_get_softc(pcm_devclass, unit);
1189 if (pcm_dev == NULL)
1192 SLIST_FOREACH(pcm_chan, &pcm_dev->channels, link) {
1196 pdev = pcm_chan->dsp_devt;
1199 pdev = pcm_chan->dspW_devt;
1202 pdev = pcm_chan->audio_devt;
1205 panic("Unknown devtype %d", devtype);
1208 if ((pdev != NULL) && (pdev->si_drv1 == NULL) && (pdev->si_drv2 == NULL)) {
1217 dsp_sysinit(void *p)
1219 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
1223 dsp_sysuninit(void *p)
1225 if (dsp_ehtag != NULL)
1226 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
1229 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
1230 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);