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 struct snddev_info *
62 dsp_get_info(struct cdev *dev)
64 struct snddev_info *d;
68 if (unit >= devclass_get_maxunit(pcm_devclass))
70 d = devclass_get_softc(pcm_devclass, unit);
76 dsp_get_flags(struct cdev *dev)
82 if (unit >= devclass_get_maxunit(pcm_devclass))
84 bdev = devclass_get_device(pcm_devclass, unit);
86 return pcm_getflags(bdev);
90 dsp_set_flags(struct cdev *dev, u_int32_t flags)
96 if (unit >= devclass_get_maxunit(pcm_devclass))
98 bdev = devclass_get_device(pcm_devclass, unit);
100 pcm_setflags(bdev, flags);
104 * return the channels associated with an open device instance.
105 * set the priority if the device is simplex and one direction (only) is
107 * lock channels specified.
110 getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch, u_int32_t prio)
112 struct snddev_info *d;
115 flags = dsp_get_flags(dev);
116 d = dsp_get_info(dev);
119 KASSERT((flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
120 ("getchns: read and write both prioritised"));
122 if ((flags & SD_F_PRIO_SET) == 0 && (prio != (SD_F_PRIO_RD | SD_F_PRIO_WR))) {
123 flags |= prio & (SD_F_PRIO_RD | SD_F_PRIO_WR);
124 dsp_set_flags(dev, flags);
127 *rdch = dev->si_drv1;
128 *wrch = dev->si_drv2;
129 if ((flags & SD_F_SIMPLEX) && (flags & SD_F_PRIO_SET)) {
131 if (*rdch && flags & SD_F_PRIO_WR) {
133 *rdch = pcm_getfakechan(d);
134 } else if (*wrch && flags & SD_F_PRIO_RD) {
136 *wrch = pcm_getfakechan(d);
140 pcm_getfakechan(d)->flags |= CHN_F_BUSY;
144 if (*rdch && *rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
146 if (*wrch && *wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
152 /* unlock specified channels */
154 relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch, u_int32_t prio)
156 struct snddev_info *d;
158 d = dsp_get_info(dev);
159 if (wrch && wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
161 if (rdch && rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
167 dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
169 struct pcm_channel *rdch, *wrch;
170 struct snddev_info *d;
176 d = dsp_get_info(i_dev);
177 devtype = PCMDEV(i_dev);
179 /* decide default format */
193 case SND_DEV_NORESET:
205 panic("impossible devtype %d", devtype);
210 /* lock snddev so nobody else can monkey with it */
213 rdch = i_dev->si_drv1;
214 wrch = i_dev->si_drv2;
216 if ((dsp_get_flags(i_dev) & SD_F_SIMPLEX) && (rdch || wrch)) {
217 /* we're a simplex device and already open, no go */
222 if (((flags & FREAD) && rdch) || ((flags & FWRITE) && wrch)) {
224 * device already open in one or both directions that
225 * the opener wants; we can't handle this.
232 * if we get here, the open request is valid- either:
233 * * we were previously not open
234 * * we were open for play xor record and the opener wants
235 * the non-open direction
240 if (devtype == SND_DEV_DSPREC)
241 rdch = pcm_chnalloc(d, PCMDIR_REC, td->td_proc->p_pid, PCMCHAN(i_dev));
243 rdch = pcm_chnalloc(d, PCMDIR_REC, td->td_proc->p_pid, -1);
245 /* no channel available, exit */
248 /* got a channel, already locked for us */
249 if (chn_reset(rdch, fmt) ||
250 (fmt && chn_setspeed(rdch, DSP_DEFAULT_SPEED))) {
251 pcm_chnrelease(rdch);
253 i_dev->si_drv1 = NULL;
258 if (flags & O_NONBLOCK)
259 rdch->flags |= CHN_F_NBIO;
264 * Record channel created, ref'ed and unlocked
269 if (flags & FWRITE) {
272 wrch = pcm_chnalloc(d, PCMDIR_PLAY, td->td_proc->p_pid, -1);
276 error = EBUSY; /* XXX Right return code? */
277 else if (chn_reset(wrch, fmt) ||
278 (fmt && chn_setspeed(wrch, DSP_DEFAULT_SPEED)))
286 pcm_chnrelease(wrch);
288 i_dev->si_drv2 = NULL;
293 * Lock, deref and release previously created record channel
296 pcm_chnref(rdch, -1);
297 pcm_chnrelease(rdch);
299 i_dev->si_drv1 = NULL;
306 if (flags & O_NONBLOCK)
307 wrch->flags |= CHN_F_NBIO;
313 i_dev->si_drv1 = rdch;
314 i_dev->si_drv2 = wrch;
321 dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
323 struct pcm_channel *rdch, *wrch;
324 struct snddev_info *d;
327 d = dsp_get_info(i_dev);
329 rdch = i_dev->si_drv1;
330 wrch = i_dev->si_drv2;
337 refs += pcm_chnref(rdch, -1);
342 refs += pcm_chnref(wrch, -1);
347 * If there are no more references, release the channels.
349 if ((rdch || wrch) && refs == 0) {
353 if (pcm_getfakechan(d))
354 pcm_getfakechan(d)->flags = 0;
356 i_dev->si_drv1 = NULL;
357 i_dev->si_drv2 = NULL;
359 dsp_set_flags(i_dev, dsp_get_flags(i_dev) & ~SD_F_TRANSIENT);
365 chn_abort(rdch); /* won't sleep */
366 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
368 pcm_chnrelease(rdch);
373 * XXX: Maybe the right behaviour is to abort on non_block.
374 * It seems that mplayer flushes the audio queue by quickly
375 * closing and re-opening. In FBSD, there's a long pause
376 * while the audio queue flushes that I presume isn't there in
379 chn_flush(wrch); /* may sleep */
380 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
382 pcm_chnrelease(wrch);
389 dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
391 struct pcm_channel *rdch, *wrch;
394 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD);
396 KASSERT(rdch, ("dsp_read: nonexistant channel"));
397 KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel"));
399 if (rdch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
400 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
403 if (!(rdch->flags & CHN_F_RUNNING))
404 rdch->flags |= CHN_F_RUNNING;
405 ret = chn_read(rdch, buf);
406 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
412 dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
414 struct pcm_channel *rdch, *wrch;
417 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_WR);
419 KASSERT(wrch, ("dsp_write: nonexistant channel"));
420 KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel"));
422 if (wrch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
423 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
426 if (!(wrch->flags & CHN_F_RUNNING))
427 wrch->flags |= CHN_F_RUNNING;
428 ret = chn_write(wrch, buf);
429 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
435 dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
437 struct pcm_channel *chn, *rdch, *wrch;
438 struct snddev_info *d;
440 int ret = 0, *arg_i = (int *)arg, tmp;
443 * this is an evil hack to allow broken apps to perform mixer ioctls
447 d = dsp_get_info(i_dev);
448 if (IOCGROUP(cmd) == 'M')
449 return mixer_ioctl(d->mixer_dev, cmd, arg, mode, td);
451 getchns(i_dev, &rdch, &wrch, 0);
454 if (wrch && (wrch->flags & CHN_F_DEAD))
456 if (rdch && (rdch->flags & CHN_F_DEAD))
459 relchns(i_dev, rdch, wrch, 0);
470 * we start with the new ioctl interface.
472 case AIONWRITE: /* how many bytes can write ? */
476 if (wrch && wrch->bufhard.dl)
477 while (chn_wrfeed(wrch) == 0);
479 *arg_i = sndbuf_getfree(wrch->bufsoft);
487 case AIOSSIZE: /* set the current blocksize */
489 struct snd_size *p = (struct snd_size *)arg;
495 chn_setblocksize(wrch, 2, p->play_size);
496 p->play_size = sndbuf_getblksz(wrch->bufsoft);
501 chn_setblocksize(rdch, 2, p->rec_size);
502 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
507 case AIOGSIZE: /* get the current blocksize */
509 struct snd_size *p = (struct snd_size *)arg;
513 p->play_size = sndbuf_getblksz(wrch->bufsoft);
518 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
527 snd_chan_param *p = (snd_chan_param *)arg;
529 if (cmd == AIOSFMT &&
530 ((p->play_format != 0 && p->play_rate == 0) ||
531 (p->rec_format != 0 && p->rec_rate == 0))) {
537 if (cmd == AIOSFMT && p->play_format != 0) {
538 chn_setformat(wrch, p->play_format);
539 chn_setspeed(wrch, p->play_rate);
541 p->play_rate = wrch->speed;
542 p->play_format = wrch->format;
550 if (cmd == AIOSFMT && p->rec_format != 0) {
551 chn_setformat(rdch, p->rec_format);
552 chn_setspeed(rdch, p->rec_rate);
554 p->rec_rate = rdch->speed;
555 p->rec_format = rdch->format;
564 case AIOGCAP: /* get capabilities */
566 snd_capabilities *p = (snd_capabilities *)arg;
567 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
572 rcaps = chn_getcaps(rdch);
576 pcaps = chn_getcaps(wrch);
578 p->rate_min = max(rcaps? rcaps->minspeed : 0,
579 pcaps? pcaps->minspeed : 0);
580 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
581 pcaps? pcaps->maxspeed : 1000000);
582 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
583 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
584 /* XXX bad on sb16 */
585 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
586 (wrch? chn_getformats(wrch) : 0xffffffff);
588 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
590 p->mixers = 1; /* default: one mixer */
591 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
592 p->left = p->right = 100;
601 if (*arg_i == AIOSYNC_PLAY && wrch) {
603 *arg_i = chn_abort(wrch);
605 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
607 *arg_i = chn_abort(rdch);
610 printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
616 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
617 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
621 * here follow the standard ioctls (filio.h etc.)
623 case FIONREAD: /* get # bytes to read */
626 /* if (rdch && rdch->bufhard.dl)
627 while (chn_rdfeed(rdch) == 0);
629 *arg_i = sndbuf_getready(rdch->bufsoft);
637 case FIOASYNC: /*set/clear async i/o */
638 DEB( printf("FIOASYNC\n") ; )
641 case SNDCTL_DSP_NONBLOCK:
642 case FIONBIO: /* set/clear non-blocking i/o */
646 rdch->flags |= CHN_F_NBIO;
648 rdch->flags &= ~CHN_F_NBIO;
654 wrch->flags |= CHN_F_NBIO;
656 wrch->flags &= ~CHN_F_NBIO;
662 * Finally, here is the linux-compatible ioctl interface
664 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
665 case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
666 case SNDCTL_DSP_GETBLKSIZE:
667 chn = wrch ? wrch : rdch;
670 *arg_i = sndbuf_getblksz(chn->bufsoft);
678 case SNDCTL_DSP_SETBLKSIZE:
679 RANGE(*arg_i, 16, 65536);
682 chn_setblocksize(wrch, 2, *arg_i);
687 chn_setblocksize(rdch, 2, *arg_i);
692 case SNDCTL_DSP_RESET:
693 DEB(printf("dsp reset\n"));
708 case SNDCTL_DSP_SYNC:
709 DEB(printf("dsp sync\n"));
710 /* chn_sync may sleep */
713 chn_sync(wrch, sndbuf_getsize(wrch->bufsoft) - 4);
718 case SNDCTL_DSP_SPEED:
719 /* chn_setspeed may sleep */
723 ret = chn_setspeed(wrch, *arg_i);
727 if (rdch && ret == 0) {
729 ret = chn_setspeed(rdch, *arg_i);
737 case SOUND_PCM_READ_RATE:
738 chn = wrch ? wrch : rdch;
749 case SNDCTL_DSP_STEREO:
751 *arg_i = (*arg_i)? AFMT_STEREO : 0;
754 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
755 tmp = (wrch->format & AFMT_STEREO)? 1 : 0;
758 if (rdch && ret == 0) {
760 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
762 tmp = (rdch->format & AFMT_STEREO)? 1 : 0;
768 case SOUND_PCM_WRITE_CHANNELS:
769 /* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
772 *arg_i = (*arg_i != 1)? AFMT_STEREO : 0;
775 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
776 tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
779 if (rdch && ret == 0) {
781 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
783 tmp = (rdch->format & AFMT_STEREO)? 2 : 1;
788 chn = wrch ? wrch : rdch;
790 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
795 case SOUND_PCM_READ_CHANNELS:
796 chn = wrch ? wrch : rdch;
799 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
807 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
808 chn = wrch ? wrch : rdch;
811 *arg_i = chn_getformats(chn);
819 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
820 if ((*arg_i != AFMT_QUERY)) {
824 ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
825 tmp = wrch->format & ~AFMT_STEREO;
828 if (rdch && ret == 0) {
830 ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
832 tmp = rdch->format & ~AFMT_STEREO;
837 chn = wrch ? wrch : rdch;
839 *arg_i = chn->format & ~AFMT_STEREO;
844 case SNDCTL_DSP_SETFRAGMENT:
845 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
847 u_int32_t fragln = (*arg_i) & 0x0000ffff;
848 u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
850 u_int32_t r_maxfrags, r_fragsz;
852 RANGE(fragln, 4, 16);
853 fragsz = 1 << fragln;
856 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
859 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
860 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
862 DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
865 ret = chn_setblocksize(rdch, maxfrags, fragsz);
866 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
867 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
870 r_maxfrags = maxfrags;
873 if (wrch && ret == 0) {
875 ret = chn_setblocksize(wrch, maxfrags, fragsz);
876 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
877 fragsz = sndbuf_getblksz(wrch->bufsoft);
879 } else { /* use whatever came from the read channel */
880 maxfrags = r_maxfrags;
889 *arg_i = (maxfrags << 16) | fragln;
893 case SNDCTL_DSP_GETISPACE:
894 /* return the size of data available in the input queue */
896 audio_buf_info *a = (audio_buf_info *)arg;
898 struct snd_dbuf *bs = rdch->bufsoft;
901 a->bytes = sndbuf_getready(bs);
902 a->fragments = a->bytes / sndbuf_getblksz(bs);
903 a->fragstotal = sndbuf_getblkcnt(bs);
904 a->fragsize = sndbuf_getblksz(bs);
910 case SNDCTL_DSP_GETOSPACE:
911 /* return space available in the output queue */
913 audio_buf_info *a = (audio_buf_info *)arg;
915 struct snd_dbuf *bs = wrch->bufsoft;
918 /* XXX abusive DMA update: chn_wrupdate(wrch); */
919 a->bytes = sndbuf_getfree(bs);
920 a->fragments = a->bytes / sndbuf_getblksz(bs);
921 a->fragstotal = sndbuf_getblkcnt(bs);
922 a->fragsize = sndbuf_getblksz(bs);
928 case SNDCTL_DSP_GETIPTR:
930 count_info *a = (count_info *)arg;
932 struct snd_dbuf *bs = rdch->bufsoft;
935 /* XXX abusive DMA update: chn_rdupdate(rdch); */
936 a->bytes = sndbuf_gettotal(bs);
937 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
938 a->ptr = sndbuf_getreadyptr(bs);
939 rdch->blocks = sndbuf_getblocks(bs);
946 case SNDCTL_DSP_GETOPTR:
948 count_info *a = (count_info *)arg;
950 struct snd_dbuf *bs = wrch->bufsoft;
953 /* XXX abusive DMA update: chn_wrupdate(wrch); */
954 a->bytes = sndbuf_gettotal(bs);
955 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
956 a->ptr = sndbuf_getreadyptr(bs);
957 wrch->blocks = sndbuf_getblocks(bs);
964 case SNDCTL_DSP_GETCAPS:
965 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
966 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
967 *arg_i |= DSP_CAP_DUPLEX;
970 case SOUND_PCM_READ_BITS:
971 chn = wrch ? wrch : rdch;
974 if (chn->format & AFMT_8BIT)
976 else if (chn->format & AFMT_16BIT)
978 else if (chn->format & AFMT_24BIT)
980 else if (chn->format & AFMT_32BIT)
991 case SNDCTL_DSP_SETTRIGGER:
994 rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
995 if (*arg_i & PCM_ENABLE_INPUT)
998 rdch->flags |= CHN_F_NOTRIGGER;
1003 wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
1004 if (*arg_i & PCM_ENABLE_OUTPUT)
1007 wrch->flags |= CHN_F_NOTRIGGER;
1012 case SNDCTL_DSP_GETTRIGGER:
1016 if (wrch->flags & CHN_F_TRIGGERED)
1017 *arg_i |= PCM_ENABLE_OUTPUT;
1022 if (rdch->flags & CHN_F_TRIGGERED)
1023 *arg_i |= PCM_ENABLE_INPUT;
1028 case SNDCTL_DSP_GETODELAY:
1030 struct snd_dbuf *b = wrch->bufhard;
1031 struct snd_dbuf *bs = wrch->bufsoft;
1034 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1035 *arg_i = sndbuf_getready(b) + sndbuf_getready(bs);
1041 case SNDCTL_DSP_POST:
1044 wrch->flags &= ~CHN_F_NOTRIGGER;
1050 case SNDCTL_DSP_SETDUPLEX:
1052 * switch to full-duplex mode if card is in half-duplex
1053 * mode and is able to work in full-duplex mode
1055 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1056 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1059 case SNDCTL_DSP_MAPINBUF:
1060 case SNDCTL_DSP_MAPOUTBUF:
1061 case SNDCTL_DSP_SETSYNCRO:
1064 case SNDCTL_DSP_SUBDIVIDE:
1065 case SOUND_PCM_WRITE_FILTER:
1066 case SOUND_PCM_READ_FILTER:
1067 /* dunno what these do, don't sound important */
1070 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
1074 relchns(i_dev, rdch, wrch, 0);
1079 dsp_poll(struct cdev *i_dev, int events, struct thread *td)
1081 struct pcm_channel *wrch = NULL, *rdch = NULL;
1085 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1088 e = (events & (POLLOUT | POLLWRNORM));
1090 ret |= chn_poll(wrch, e, td);
1093 e = (events & (POLLIN | POLLRDNORM));
1095 ret |= chn_poll(rdch, e, td);
1097 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1103 dsp_mmap(struct cdev *i_dev, vm_offset_t offset, vm_paddr_t *paddr, int 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 *paddr = 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 int devtypes[3] = {SND_DEV_DSP, SND_DEV_DSP16, SND_DEV_AUDIO};
1166 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);