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 if (i_dev == NULL || td == NULL)
179 if ((flags & (FREAD | FWRITE)) == 0)
182 d = dsp_get_info(i_dev);
183 devtype = PCMDEV(i_dev);
186 /* decide default format */
200 case SND_DEV_NORESET:
208 chnum = PCMCHAN(i_dev);
212 panic("impossible devtype %d", devtype);
215 /* lock snddev so nobody else can monkey with it */
218 rdch = i_dev->si_drv1;
219 wrch = i_dev->si_drv2;
221 if (rdch || wrch || ((dsp_get_flags(i_dev) & SD_F_SIMPLEX) &&
222 (flags & (FREAD | FWRITE)) == (FREAD | FWRITE))) {
223 /* simplex or not, better safe than sorry. */
229 * if we get here, the open request is valid- either:
230 * * we were previously not open
231 * * we were open for play xor record and the opener wants
232 * the non-open direction
237 error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, chnum);
238 if (error != 0 && error != EBUSY && chnum != -1 && (flags & FWRITE))
239 error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, -1);
241 if (error == 0 && (chn_reset(rdch, fmt) ||
242 (fmt && chn_setspeed(rdch, DSP_DEFAULT_SPEED))))
247 pcm_chnrelease(rdch);
251 if (flags & O_NONBLOCK)
252 rdch->flags |= CHN_F_NBIO;
258 if (flags & FWRITE) {
261 error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, chnum);
262 if (error != 0 && error != EBUSY && chnum != -1 && (flags & FREAD))
263 error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, -1);
265 if (error == 0 && (chn_reset(wrch, fmt) ||
266 (fmt && chn_setspeed(wrch, DSP_DEFAULT_SPEED))))
271 pcm_chnrelease(wrch);
274 * Lock, deref and release previously created record channel
277 pcm_chnref(rdch, -1);
278 pcm_chnrelease(rdch);
284 if (flags & O_NONBLOCK)
285 wrch->flags |= CHN_F_NBIO;
291 i_dev->si_drv1 = rdch;
292 i_dev->si_drv2 = wrch;
299 dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
301 struct pcm_channel *rdch, *wrch;
302 struct snddev_info *d;
305 d = dsp_get_info(i_dev);
307 rdch = i_dev->si_drv1;
308 wrch = i_dev->si_drv2;
309 if (rdch && td->td_proc->p_pid != rdch->pid)
311 if (wrch && td->td_proc->p_pid != wrch->pid)
319 refs += pcm_chnref(rdch, -1);
320 chn_abort(rdch); /* won't sleep */
321 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
323 pcm_chnrelease(rdch);
327 refs += pcm_chnref(wrch, -1);
329 * XXX: Maybe the right behaviour is to abort on non_block.
330 * It seems that mplayer flushes the audio queue by quickly
331 * closing and re-opening. In FBSD, there's a long pause
332 * while the audio queue flushes that I presume isn't there in
335 chn_flush(wrch); /* may sleep */
336 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
338 pcm_chnrelease(wrch);
343 i_dev->si_drv1 = NULL;
345 i_dev->si_drv2 = NULL;
347 * If there are no more references, release the channels.
349 if (refs == 0 && i_dev->si_drv1 == NULL &&
350 i_dev->si_drv2 == NULL) {
351 if (pcm_getfakechan(d))
352 pcm_getfakechan(d)->flags = 0;
353 /* What is this?!? */
354 dsp_set_flags(i_dev, dsp_get_flags(i_dev) & ~SD_F_TRANSIENT);
362 dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
364 struct pcm_channel *rdch, *wrch;
367 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD);
369 KASSERT(rdch, ("dsp_read: nonexistant channel"));
370 KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel"));
372 if (rdch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
373 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
376 if (!(rdch->flags & CHN_F_RUNNING))
377 rdch->flags |= CHN_F_RUNNING;
378 ret = chn_read(rdch, buf);
379 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
385 dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
387 struct pcm_channel *rdch, *wrch;
390 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_WR);
392 KASSERT(wrch, ("dsp_write: nonexistant channel"));
393 KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel"));
395 if (wrch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
396 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
399 if (!(wrch->flags & CHN_F_RUNNING))
400 wrch->flags |= CHN_F_RUNNING;
401 ret = chn_write(wrch, buf);
402 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
408 dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
410 struct pcm_channel *chn, *rdch, *wrch;
411 struct snddev_info *d;
413 int ret = 0, *arg_i = (int *)arg, tmp;
416 * this is an evil hack to allow broken apps to perform mixer ioctls
420 d = dsp_get_info(i_dev);
421 if (IOCGROUP(cmd) == 'M') {
423 * This is at least, a bug to bug compatible with OSS.
425 if (d->mixer_dev != NULL)
426 return mixer_ioctl(d->mixer_dev, cmd, arg, -1, td);
431 getchns(i_dev, &rdch, &wrch, 0);
434 if (wrch && ((wrch->flags & CHN_F_DEAD) ||
435 td->td_proc->p_pid != wrch->pid))
437 if (rdch && ((rdch->flags & CHN_F_DEAD) ||
438 td->td_proc->p_pid != rdch->pid))
441 relchns(i_dev, rdch, wrch, 0);
452 * we start with the new ioctl interface.
454 case AIONWRITE: /* how many bytes can write ? */
458 if (wrch && wrch->bufhard.dl)
459 while (chn_wrfeed(wrch) == 0);
461 *arg_i = sndbuf_getfree(wrch->bufsoft);
469 case AIOSSIZE: /* set the current blocksize */
471 struct snd_size *p = (struct snd_size *)arg;
477 chn_setblocksize(wrch, 2, p->play_size);
478 p->play_size = sndbuf_getblksz(wrch->bufsoft);
483 chn_setblocksize(rdch, 2, p->rec_size);
484 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
489 case AIOGSIZE: /* get the current blocksize */
491 struct snd_size *p = (struct snd_size *)arg;
495 p->play_size = sndbuf_getblksz(wrch->bufsoft);
500 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
509 snd_chan_param *p = (snd_chan_param *)arg;
511 if (cmd == AIOSFMT &&
512 ((p->play_format != 0 && p->play_rate == 0) ||
513 (p->rec_format != 0 && p->rec_rate == 0))) {
519 if (cmd == AIOSFMT && p->play_format != 0) {
520 chn_setformat(wrch, p->play_format);
521 chn_setspeed(wrch, p->play_rate);
523 p->play_rate = wrch->speed;
524 p->play_format = wrch->format;
532 if (cmd == AIOSFMT && p->rec_format != 0) {
533 chn_setformat(rdch, p->rec_format);
534 chn_setspeed(rdch, p->rec_rate);
536 p->rec_rate = rdch->speed;
537 p->rec_format = rdch->format;
546 case AIOGCAP: /* get capabilities */
548 snd_capabilities *p = (snd_capabilities *)arg;
549 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
554 rcaps = chn_getcaps(rdch);
558 pcaps = chn_getcaps(wrch);
560 p->rate_min = max(rcaps? rcaps->minspeed : 0,
561 pcaps? pcaps->minspeed : 0);
562 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
563 pcaps? pcaps->maxspeed : 1000000);
564 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
565 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
566 /* XXX bad on sb16 */
567 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
568 (wrch? chn_getformats(wrch) : 0xffffffff);
570 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
572 p->mixers = 1; /* default: one mixer */
573 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
574 p->left = p->right = 100;
583 if (*arg_i == AIOSYNC_PLAY && wrch) {
585 *arg_i = chn_abort(wrch);
587 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
589 *arg_i = chn_abort(rdch);
592 printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
598 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
599 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
603 * here follow the standard ioctls (filio.h etc.)
605 case FIONREAD: /* get # bytes to read */
608 /* if (rdch && rdch->bufhard.dl)
609 while (chn_rdfeed(rdch) == 0);
611 *arg_i = sndbuf_getready(rdch->bufsoft);
619 case FIOASYNC: /*set/clear async i/o */
620 DEB( printf("FIOASYNC\n") ; )
623 case SNDCTL_DSP_NONBLOCK:
624 case FIONBIO: /* set/clear non-blocking i/o */
628 rdch->flags |= CHN_F_NBIO;
630 rdch->flags &= ~CHN_F_NBIO;
636 wrch->flags |= CHN_F_NBIO;
638 wrch->flags &= ~CHN_F_NBIO;
644 * Finally, here is the linux-compatible ioctl interface
646 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
647 case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
648 case SNDCTL_DSP_GETBLKSIZE:
649 chn = wrch ? wrch : rdch;
652 *arg_i = sndbuf_getblksz(chn->bufsoft);
660 case SNDCTL_DSP_SETBLKSIZE:
661 RANGE(*arg_i, 16, 65536);
664 chn_setblocksize(wrch, 2, *arg_i);
669 chn_setblocksize(rdch, 2, *arg_i);
674 case SNDCTL_DSP_RESET:
675 DEB(printf("dsp reset\n"));
690 case SNDCTL_DSP_SYNC:
691 DEB(printf("dsp sync\n"));
692 /* chn_sync may sleep */
695 chn_sync(wrch, sndbuf_getsize(wrch->bufsoft) - 4);
700 case SNDCTL_DSP_SPEED:
701 /* chn_setspeed may sleep */
705 ret = chn_setspeed(wrch, *arg_i);
709 if (rdch && ret == 0) {
711 ret = chn_setspeed(rdch, *arg_i);
719 case SOUND_PCM_READ_RATE:
720 chn = wrch ? wrch : rdch;
731 case SNDCTL_DSP_STEREO:
733 *arg_i = (*arg_i)? AFMT_STEREO : 0;
736 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
737 tmp = (wrch->format & AFMT_STEREO)? 1 : 0;
740 if (rdch && ret == 0) {
742 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
744 tmp = (rdch->format & AFMT_STEREO)? 1 : 0;
750 case SOUND_PCM_WRITE_CHANNELS:
751 /* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
754 *arg_i = (*arg_i != 1)? AFMT_STEREO : 0;
757 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
758 tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
761 if (rdch && ret == 0) {
763 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
765 tmp = (rdch->format & AFMT_STEREO)? 2 : 1;
770 chn = wrch ? wrch : rdch;
772 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
777 case SOUND_PCM_READ_CHANNELS:
778 chn = wrch ? wrch : rdch;
781 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
789 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
790 chn = wrch ? wrch : rdch;
793 *arg_i = chn_getformats(chn);
801 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
802 if ((*arg_i != AFMT_QUERY)) {
806 ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
807 tmp = wrch->format & ~AFMT_STEREO;
810 if (rdch && ret == 0) {
812 ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
814 tmp = rdch->format & ~AFMT_STEREO;
819 chn = wrch ? wrch : rdch;
821 *arg_i = chn->format & ~AFMT_STEREO;
826 case SNDCTL_DSP_SETFRAGMENT:
827 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
829 u_int32_t fragln = (*arg_i) & 0x0000ffff;
830 u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
832 u_int32_t r_maxfrags, r_fragsz;
834 RANGE(fragln, 4, 16);
835 fragsz = 1 << fragln;
838 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
841 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
842 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
844 DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
847 ret = chn_setblocksize(rdch, maxfrags, fragsz);
848 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
849 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
852 r_maxfrags = maxfrags;
855 if (wrch && ret == 0) {
857 ret = chn_setblocksize(wrch, maxfrags, fragsz);
858 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
859 fragsz = sndbuf_getblksz(wrch->bufsoft);
861 } else { /* use whatever came from the read channel */
862 maxfrags = r_maxfrags;
871 *arg_i = (maxfrags << 16) | fragln;
875 case SNDCTL_DSP_GETISPACE:
876 /* return the size of data available in the input queue */
878 audio_buf_info *a = (audio_buf_info *)arg;
880 struct snd_dbuf *bs = rdch->bufsoft;
883 a->bytes = sndbuf_getready(bs);
884 a->fragments = a->bytes / sndbuf_getblksz(bs);
885 a->fragstotal = sndbuf_getblkcnt(bs);
886 a->fragsize = sndbuf_getblksz(bs);
892 case SNDCTL_DSP_GETOSPACE:
893 /* return space available in the output queue */
895 audio_buf_info *a = (audio_buf_info *)arg;
897 struct snd_dbuf *bs = wrch->bufsoft;
900 /* XXX abusive DMA update: chn_wrupdate(wrch); */
901 a->bytes = sndbuf_getfree(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_GETIPTR:
912 count_info *a = (count_info *)arg;
914 struct snd_dbuf *bs = rdch->bufsoft;
917 /* XXX abusive DMA update: chn_rdupdate(rdch); */
918 a->bytes = sndbuf_gettotal(bs);
919 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
920 a->ptr = sndbuf_getreadyptr(bs);
921 rdch->blocks = sndbuf_getblocks(bs);
928 case SNDCTL_DSP_GETOPTR:
930 count_info *a = (count_info *)arg;
932 struct snd_dbuf *bs = wrch->bufsoft;
935 /* XXX abusive DMA update: chn_wrupdate(wrch); */
936 a->bytes = sndbuf_gettotal(bs);
937 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
938 a->ptr = sndbuf_getreadyptr(bs);
939 wrch->blocks = sndbuf_getblocks(bs);
946 case SNDCTL_DSP_GETCAPS:
947 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
948 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
949 *arg_i |= DSP_CAP_DUPLEX;
952 case SOUND_PCM_READ_BITS:
953 chn = wrch ? wrch : rdch;
956 if (chn->format & AFMT_8BIT)
958 else if (chn->format & AFMT_16BIT)
960 else if (chn->format & AFMT_24BIT)
962 else if (chn->format & AFMT_32BIT)
973 case SNDCTL_DSP_SETTRIGGER:
976 rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
977 if (*arg_i & PCM_ENABLE_INPUT)
980 rdch->flags |= CHN_F_NOTRIGGER;
985 wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
986 if (*arg_i & PCM_ENABLE_OUTPUT)
989 wrch->flags |= CHN_F_NOTRIGGER;
994 case SNDCTL_DSP_GETTRIGGER:
998 if (wrch->flags & CHN_F_TRIGGERED)
999 *arg_i |= PCM_ENABLE_OUTPUT;
1004 if (rdch->flags & CHN_F_TRIGGERED)
1005 *arg_i |= PCM_ENABLE_INPUT;
1010 case SNDCTL_DSP_GETODELAY:
1012 struct snd_dbuf *b = wrch->bufhard;
1013 struct snd_dbuf *bs = wrch->bufsoft;
1016 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1017 *arg_i = sndbuf_getready(b) + sndbuf_getready(bs);
1023 case SNDCTL_DSP_POST:
1026 wrch->flags &= ~CHN_F_NOTRIGGER;
1032 case SNDCTL_DSP_SETDUPLEX:
1034 * switch to full-duplex mode if card is in half-duplex
1035 * mode and is able to work in full-duplex mode
1037 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1038 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1041 case SNDCTL_DSP_MAPINBUF:
1042 case SNDCTL_DSP_MAPOUTBUF:
1043 case SNDCTL_DSP_SETSYNCRO:
1046 case SNDCTL_DSP_SUBDIVIDE:
1047 case SOUND_PCM_WRITE_FILTER:
1048 case SOUND_PCM_READ_FILTER:
1049 /* dunno what these do, don't sound important */
1052 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
1056 relchns(i_dev, rdch, wrch, 0);
1061 dsp_poll(struct cdev *i_dev, int events, struct thread *td)
1063 struct pcm_channel *wrch = NULL, *rdch = NULL;
1067 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1070 e = (events & (POLLOUT | POLLWRNORM));
1072 ret |= chn_poll(wrch, e, td);
1075 e = (events & (POLLIN | POLLRDNORM));
1077 ret |= chn_poll(rdch, e, td);
1079 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1085 dsp_mmap(struct cdev *i_dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
1087 struct pcm_channel *wrch = NULL, *rdch = NULL, *c;
1089 if (nprot & PROT_EXEC)
1092 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1095 * XXX the linux api uses the nprot to select read/write buffer
1096 * our vm system doesn't allow this, so force write buffer
1099 if (wrch && (nprot & PROT_WRITE)) {
1101 } else if (rdch && (nprot & PROT_READ)) {
1111 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1115 if (offset >= sndbuf_getsize(c->bufsoft)) {
1116 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1120 if (!(c->flags & CHN_F_MAPPED))
1121 c->flags |= CHN_F_MAPPED;
1123 *paddr = vtophys(sndbuf_getbufofs(c->bufsoft, offset));
1124 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1132 * Clone logic is this:
1133 * x E X = {dsp, dspW, audio}
1134 * x -> x${sysctl("hw.snd.unit")}
1136 * for i N = 1 to channels of device N
1137 * if xN.i isn't busy, return its dev_t
1140 dsp_clone(void *arg, struct ucred *cred, char *name, int namelen,
1144 struct snddev_info *pcm_dev;
1145 struct snddev_channel *pcm_chan;
1146 int i, unit, devtype;
1147 static int devtypes[3] = {SND_DEV_DSP, SND_DEV_DSP16, SND_DEV_AUDIO};
1148 static char *devnames[3] = {"dsp", "dspW", "audio"};
1152 if (pcm_devclass == NULL)
1157 for (i = 0; (i < 3) && (unit == -1); i++) {
1158 devtype = devtypes[i];
1159 if (strcmp(name, devnames[i]) == 0) {
1162 if (dev_stdclone(name, NULL, devnames[i], &unit) != 1)
1166 if (unit == -1 || unit >= devclass_get_maxunit(pcm_devclass))
1169 pcm_dev = devclass_get_softc(pcm_devclass, unit);
1171 if (pcm_dev == NULL)
1174 SLIST_FOREACH(pcm_chan, &pcm_dev->channels, link) {
1178 pdev = pcm_chan->dsp_devt;
1181 pdev = pcm_chan->dspW_devt;
1184 pdev = pcm_chan->audio_devt;
1187 panic("Unknown devtype %d", devtype);
1190 if ((pdev != NULL) && (pdev->si_drv1 == NULL) && (pdev->si_drv2 == NULL)) {
1199 dsp_sysinit(void *p)
1201 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
1205 dsp_sysuninit(void *p)
1207 if (dsp_ehtag != NULL)
1208 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
1211 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
1212 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);