xref: /freebsd/sys/dev/sound/pcm/dsp.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
1 /*
2  * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 
32 #include <dev/sound/pcm/sound.h>
33 
34 static int getchns(snddev_info *d, int chan, pcm_channel **rdch, pcm_channel **wrch);
35 
36 static pcm_channel *
37 allocchn(snddev_info *d, int direction)
38 {
39 	pcm_channel *chns = (direction == PCMDIR_PLAY)? d->play : d->rec;
40 	int i, cnt = (direction == PCMDIR_PLAY)? d->playcount : d->reccount;
41 	for (i = 0; i < cnt; i++) {
42 		if (!(chns[i].flags & CHN_F_BUSY)) {
43 			chns[i].flags |= CHN_F_BUSY;
44 			return &chns[i];
45 		}
46 	}
47 	return NULL;
48 }
49 
50 static int
51 getchns(snddev_info *d, int chan, pcm_channel **rdch, pcm_channel **wrch)
52 {
53 	KASSERT((d->flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
54 		("getchns: read and write both prioritised"));
55 
56 	if ((d->flags & SD_F_SIMPLEX) && (d->flags & SD_F_PRIO_SET)) {
57 		*rdch = (d->flags & SD_F_PRIO_RD)? d->arec[chan] : &d->fakechan;
58 		*wrch = (d->flags & SD_F_PRIO_WR)? d->aplay[chan] : &d->fakechan;
59 		d->fakechan.flags |= CHN_F_BUSY;
60 	} else {
61 		*rdch = d->arec[chan];
62 		*wrch = d->aplay[chan];
63 	}
64 	return 0;
65 }
66 
67 static void
68 setchns(snddev_info *d, int chan)
69 {
70 	KASSERT((d->flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
71 		("getchns: read and write both prioritised"));
72 	d->flags |= SD_F_DIR_SET;
73 	if (d->swap) d->swap(d->devinfo, (d->flags & SD_F_PRIO_WR)? PCMDIR_PLAY : PCMDIR_REC);
74 }
75 
76 int
77 dsp_open(snddev_info *d, int chan, int oflags, int devtype)
78 {
79 	pcm_channel *rdch, *wrch;
80 	u_int32_t fmt;
81 
82 	if (chan >= d->chancount) return ENODEV;
83 	if ((d->flags & SD_F_SIMPLEX) && (d->ref[chan] > 0)) return EBUSY;
84 	rdch = d->arec[chan];
85 	wrch = d->aplay[chan];
86 	if (oflags & FREAD) {
87 		if (rdch == NULL) {
88 			rdch = allocchn(d, PCMDIR_REC);
89 			if (!rdch) return EBUSY;
90 		} else return EBUSY;
91 	}
92 	if (oflags & FWRITE) {
93 		if (wrch == NULL) {
94 			wrch = allocchn(d, PCMDIR_PLAY);
95 			if (!wrch && (oflags & FREAD)) {
96 				rdch->flags &= ~CHN_F_BUSY;
97 				return EBUSY;
98 			}
99 		} else return EBUSY;
100 	}
101 	d->aplay[chan] = wrch;
102 	d->arec[chan] = rdch;
103 	d->ref[chan]++;
104 	switch (devtype) {
105 	case SND_DEV_DSP16:
106 		fmt = AFMT_S16_LE;
107 		break;
108 
109 	case SND_DEV_DSP:
110 		fmt = AFMT_U8;
111 		break;
112 
113 	case SND_DEV_AUDIO:
114 		fmt = AFMT_MU_LAW;
115 		break;
116 
117 	case SND_DEV_NORESET:
118 		fmt = 0;
119 		break;
120 
121 	default:
122 		return ENXIO;
123 	}
124 
125 	if (rdch && (oflags & FREAD)) {
126 	        chn_reset(rdch, fmt);
127 		if (oflags & O_NONBLOCK) rdch->flags |= CHN_F_NBIO;
128 	}
129 	if (wrch && (oflags & FWRITE)) {
130 	        chn_reset(wrch, fmt);
131 		if (oflags & O_NONBLOCK) wrch->flags |= CHN_F_NBIO;
132 	}
133 	return 0;
134 }
135 
136 int
137 dsp_close(snddev_info *d, int chan, int devtype)
138 {
139 	pcm_channel *rdch, *wrch;
140 
141 	d->ref[chan]--;
142 	if (d->ref[chan]) return 0;
143 	d->flags &= ~SD_F_TRANSIENT;
144 	rdch = d->arec[chan];
145 	wrch = d->aplay[chan];
146 
147 	if (rdch) {
148 		chn_abort(rdch);
149 		rdch->flags &= ~(CHN_F_BUSY | CHN_F_RUNNING | CHN_F_MAPPED);
150 		chn_reset(rdch, 0);
151 	}
152 	if (wrch) {
153 		chn_flush(wrch);
154 		wrch->flags &= ~(CHN_F_BUSY | CHN_F_RUNNING | CHN_F_MAPPED);
155 		chn_reset(wrch, 0);
156 	}
157 	d->aplay[chan] = NULL;
158 	d->arec[chan] = NULL;
159 	return 0;
160 }
161 
162 int
163 dsp_read(snddev_info *d, int chan, struct uio *buf, int flag)
164 {
165 	pcm_channel *rdch, *wrch;
166 
167 	if (!(d->flags & SD_F_PRIO_SET)) d->flags |= SD_F_PRIO_RD;
168 	if (!(d->flags & SD_F_DIR_SET)) setchns(d, chan);
169 	getchns(d, chan, &rdch, &wrch);
170 	KASSERT(rdch, ("dsp_read: nonexistant channel"));
171 	KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel"));
172 	if (rdch->flags & CHN_F_MAPPED) return EINVAL;
173 	if (!(rdch->flags & CHN_F_RUNNING))
174 		rdch->flags |= CHN_F_RUNNING;
175 	return chn_read(rdch, buf);
176 }
177 
178 int
179 dsp_write(snddev_info *d, int chan, struct uio *buf, int flag)
180 {
181 	pcm_channel *rdch, *wrch;
182 
183 	if (!(d->flags & SD_F_PRIO_SET)) d->flags |= SD_F_PRIO_WR;
184 	if (!(d->flags & SD_F_DIR_SET)) setchns(d, chan);
185 	getchns(d, chan, &rdch, &wrch);
186 	KASSERT(wrch, ("dsp_write: nonexistant channel"));
187 	KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel"));
188 	if (wrch->flags & CHN_F_MAPPED) return EINVAL;
189 	if (!(wrch->flags & CHN_F_RUNNING))
190 		wrch->flags |= CHN_F_RUNNING;
191 	return chn_write(wrch, buf);
192 }
193 
194 int
195 dsp_ioctl(snddev_info *d, int chan, u_long cmd, caddr_t arg)
196 {
197     	int ret = 0, *arg_i = (int *)arg;
198     	u_long s;
199     	pcm_channel *wrch = NULL, *rdch = NULL;
200 
201 	rdch = d->arec[chan];
202 	wrch = d->aplay[chan];
203 
204     	/*
205      	 * all routines are called with int. blocked. Make sure that
206      	 * ints are re-enabled when calling slow or blocking functions!
207      	 */
208     	s = spltty();
209     	switch(cmd) {
210 #ifdef OLDPCM_IOCTL
211     	/*
212      	 * we start with the new ioctl interface.
213      	 */
214     	case AIONWRITE:	/* how many bytes can write ? */
215 		if (wrch && wrch->buffer.dl)
216 			while (chn_wrfeed(wrch) > 0);
217 		*arg_i = wrch? wrch->buffer2nd.fl : 0;
218 		break;
219 
220     	case AIOSSIZE:     /* set the current blocksize */
221 		{
222 	    		struct snd_size *p = (struct snd_size *)arg;
223 	    		if (wrch) chn_setblocksize(wrch, p->play_size);
224 	    		if (rdch) chn_setblocksize(rdch, p->rec_size);
225 		}
226 		/* FALLTHROUGH */
227     	case AIOGSIZE:	/* get the current blocksize */
228 		{
229 	    		struct snd_size *p = (struct snd_size *)arg;
230 	    		if (wrch) p->play_size = wrch->blocksize2nd;
231 	    		if (rdch) p->rec_size = rdch->blocksize2nd;
232 		}
233 		break;
234 
235     	case AIOSFMT:
236 		{
237 	    		snd_chan_param *p = (snd_chan_param *)arg;
238 	    		if (wrch) {
239 				chn_setformat(wrch, p->play_format);
240 				chn_setspeed(wrch, p->play_rate);
241 	    		}
242 	    		if (rdch) {
243 				chn_setformat(rdch, p->rec_format);
244 				chn_setspeed(rdch, p->rec_rate);
245 	    		}
246 		}
247 		/* FALLTHROUGH */
248 
249     	case AIOGFMT:
250 		{
251 	    		snd_chan_param *p = (snd_chan_param *)arg;
252 	    		p->play_rate = wrch? wrch->speed : 0;
253 	    		p->rec_rate = rdch? rdch->speed : 0;
254 	    		p->play_format = wrch? wrch->format : 0;
255 	    		p->rec_format = rdch? rdch->format : 0;
256 		}
257 		break;
258 
259     	case AIOGCAP:     /* get capabilities */
260 		{
261 	    		snd_capabilities *p = (snd_capabilities *)arg;
262 			pcmchan_caps *pcaps = NULL, *rcaps = NULL;
263 			if (rdch) rcaps = chn_getcaps(rdch);
264 			if (wrch) pcaps = chn_getcaps(wrch);
265 	    		p->rate_min = max(rcaps? rcaps->minspeed : 0,
266 	                      		  pcaps? pcaps->minspeed : 0);
267 	    		p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
268 	                      		  pcaps? pcaps->maxspeed : 1000000);
269 	    		p->bufsize = min(rdch? rdch->buffer2nd.bufsize : 1000000,
270 	                     		 wrch? wrch->buffer2nd.bufsize : 1000000);
271 			/* XXX bad on sb16 */
272 	    		p->formats = (rcaps? rcaps->formats : 0xffffffff) &
273 			 	     (pcaps? pcaps->formats : 0xffffffff);
274 	    		p->mixers = 1; /* default: one mixer */
275 	    		p->inputs = d->mixer.devs;
276 	    		p->left = p->right = 100;
277 		}
278 		break;
279 
280     	case AIOSTOP:
281 		if (*arg_i == AIOSYNC_PLAY && wrch) *arg_i = chn_abort(wrch);
282 		else if (*arg_i == AIOSYNC_CAPTURE && rdch) *arg_i = chn_abort(rdch);
283 		else {
284 	   	 	printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
285 	    		*arg_i = 0;
286 		}
287 		break;
288 
289     	case AIOSYNC:
290 		printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
291 	    		((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
292 		break;
293 #endif
294 	/*
295 	 * here follow the standard ioctls (filio.h etc.)
296 	 */
297     	case FIONREAD: /* get # bytes to read */
298 		if (rdch && rdch->buffer.dl)
299 			while (chn_rdfeed(rdch) > 0);
300 		*arg_i = rdch? rdch->buffer2nd.rl : 0;
301 		break;
302 
303     	case FIOASYNC: /*set/clear async i/o */
304 		DEB( printf("FIOASYNC\n") ; )
305 		break;
306 
307     	case SNDCTL_DSP_NONBLOCK:
308     	case FIONBIO: /* set/clear non-blocking i/o */
309 		if (rdch) rdch->flags &= ~CHN_F_NBIO;
310 		if (wrch) wrch->flags &= ~CHN_F_NBIO;
311 		if (*arg_i) {
312 		    	if (rdch) rdch->flags |= CHN_F_NBIO;
313 		    	if (wrch) wrch->flags |= CHN_F_NBIO;
314 		}
315 		break;
316 
317     	/*
318 	 * Finally, here is the linux-compatible ioctl interface
319 	 */
320 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
321     	case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
322     	case SNDCTL_DSP_GETBLKSIZE:
323 		if (wrch)
324 			*arg_i = wrch->blocksize2nd;
325 		else if (rdch)
326 			*arg_i = rdch->blocksize2nd;
327 		else
328 			*arg_i = 0;
329 		break ;
330 
331     	case SNDCTL_DSP_SETBLKSIZE:
332 		RANGE(*arg_i, 16, 65536);
333 		if (wrch) chn_setblocksize(wrch, 2, *arg_i);
334 		if (rdch) chn_setblocksize(rdch, 2, *arg_i);
335 		break;
336 
337     	case SNDCTL_DSP_RESET:
338 		DEB(printf("dsp reset\n"));
339 		splx(s);
340 		if (wrch) chn_abort(wrch);
341 		if (rdch) chn_abort(rdch);
342 		break;
343 
344     	case SNDCTL_DSP_SYNC:
345 		DEB(printf("dsp sync\n"));
346 		splx(s);
347 		if (wrch) chn_sync(wrch, wrch->buffer2nd.bufsize - 4);
348 		break;
349 
350     	case SNDCTL_DSP_SPEED:
351 		splx(s);
352 		if (wrch) chn_setspeed(wrch, *arg_i);
353 		if (rdch) chn_setspeed(rdch, *arg_i);
354 		/* fallthru */
355 
356     	case SOUND_PCM_READ_RATE:
357 		*arg_i = wrch? wrch->speed : rdch->speed;
358 		break;
359 
360     	case SNDCTL_DSP_STEREO:
361 		splx(s);
362 		if (wrch) chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) |
363 					((*arg_i)? AFMT_STEREO : 0));
364 		if (rdch) chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) |
365 				        ((*arg_i)? AFMT_STEREO : 0));
366 		*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 1 : 0;
367 		break;
368 
369     	case SOUND_PCM_WRITE_CHANNELS:
370 		splx(s);
371 		if (wrch) chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) |
372 					((*arg_i == 2)? AFMT_STEREO : 0));
373 		if (rdch) chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) |
374 					((*arg_i == 2)? AFMT_STEREO : 0));
375 		/* fallthru */
376 
377     	case SOUND_PCM_READ_CHANNELS:
378 		*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 2 : 1;
379 		break;
380 
381     	case SNDCTL_DSP_GETFMTS:	/* returns a mask of supported fmts */
382 		*arg_i = wrch? chn_getcaps(wrch)->formats : chn_getcaps(rdch)->formats;
383 		break ;
384 
385     	case SNDCTL_DSP_SETFMT:	/* sets _one_ format */
386 		splx(s);
387 		if (wrch) chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
388 		if (rdch) chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
389 		*arg_i = (wrch? wrch->format: rdch->format) & ~AFMT_STEREO;
390 		break;
391 
392     	case SNDCTL_DSP_SUBDIVIDE:
393 		/* XXX watch out, this is RW! */
394 		DEB(printf("SNDCTL_DSP_SUBDIVIDE unimplemented\n");)
395 		break;
396 
397     	case SNDCTL_DSP_SETFRAGMENT:
398 		/* XXX watch out, this is RW! */
399 		DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
400 		{
401 			pcm_channel *c = wrch? wrch : rdch;
402 			u_int32_t fragln = (*arg_i) & 0x0000ffff;
403 			u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
404 			u_int32_t fragsz;
405 
406 			RANGE(fragln, 4, 16);
407 			fragsz = 1 << fragln;
408 
409 			if (maxfrags == 0)
410 				maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
411 			if (maxfrags < 2) {
412 				ret = EINVAL;
413 				break;
414 			}
415 			if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
416 				maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
417 
418 			DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
419 		    	if (rdch)
420 				ret = chn_setblocksize(rdch, maxfrags, fragsz);
421 		    	if (wrch && ret == 0)
422 				ret = chn_setblocksize(wrch, maxfrags, fragsz);
423 
424 	    		*arg_i = (c->fragments << 16) | c->blocksize;
425 		}
426 		break;
427 
428     	case SNDCTL_DSP_GETISPACE: /* XXX Space for reading? Makes no sense... */
429 		/* return the size of data available in the input queue */
430 		{
431 	    		audio_buf_info *a = (audio_buf_info *)arg;
432 	    		if (rdch) {
433 	        		snd_dbuf *b = &rdch->buffer;
434 	        		snd_dbuf *bs = &rdch->buffer2nd;
435 				if (b->dl && !(rdch->flags & CHN_F_MAPPED))
436 					/*
437 					 * Suck up the secondary and DMA buffer.
438 					 * chn_rdfeed*() takes care of the alignment.
439 					 */
440 					while (chn_rdfeed(rdch) > 0);
441 				a->bytes = bs->rl;
442 	        		a->fragments = a->bytes / rdch->blocksize2nd;
443 	        		a->fragstotal = rdch->fragments;
444 	        		a->fragsize = rdch->blocksize2nd;
445 	    		}
446 		}
447 		break;
448 
449     	case SNDCTL_DSP_GETOSPACE:
450 		/* return space available in the output queue */
451 		{
452 	    		audio_buf_info *a = (audio_buf_info *)arg;
453 	    		if (wrch) {
454 	        		snd_dbuf *b = &wrch->buffer;
455 	        		snd_dbuf *bs = &wrch->buffer2nd;
456 				if (b->dl && !(wrch->flags & CHN_F_MAPPED)) {
457 					/*
458 					 * Fill up the secondary and DMA buffer.
459 					 * chn_wrfeed*() takes care of the alignment.
460 					 * Check for underflow before writing into the buffers.
461 					 */
462 					chn_checkunderflow(wrch);
463 					while (chn_wrfeed(wrch) > 0);
464 				}
465 				a->bytes = bs->fl;
466 	        		a->fragments = a->bytes / wrch->blocksize2nd;
467 	        		a->fragstotal = wrch->fragments;
468 	        		a->fragsize = wrch->blocksize2nd;
469 	    		}
470 		}
471 		break;
472 
473     	case SNDCTL_DSP_GETIPTR:
474 		{
475 	    		count_info *a = (count_info *)arg;
476 	    		if (rdch) {
477 	        		snd_dbuf *b = &rdch->buffer;
478 	        		snd_dbuf *bs = &rdch->buffer2nd;
479 	        		if (b->dl && !(rdch->flags & CHN_F_MAPPED))
480 					/*
481 					 * Suck up the secondary and DMA buffer.
482 					 * chn_rdfeed*() takes care of the alignment.
483 					 */
484 					while (chn_rdfeed(rdch) > 0);
485 	        		a->bytes = bs->total;
486 	        		a->blocks = bs->rl / rdch->blocksize2nd;
487 	        		a->ptr = bs->rl % rdch->blocksize2nd;
488 	    		} else ret = EINVAL;
489 		}
490 		break;
491 
492     	case SNDCTL_DSP_GETOPTR:
493 		{
494 	    		count_info *a = (count_info *)arg;
495 	    		if (wrch) {
496     	        		snd_dbuf *b = &wrch->buffer;
497 	        		snd_dbuf *bs = &wrch->buffer2nd;
498 				if (b->dl && !(wrch->flags & CHN_F_MAPPED)) {
499 					/*
500 					 * Fill up the secondary and DMA buffer.
501 					 * chn_wrfeed*() takes care of the alignment.
502 					 * Check for underflow before writing into the buffers.
503 					 */
504 					chn_checkunderflow(wrch);
505 					while (chn_wrfeed(wrch) > 0);
506 				}
507 	        		a->bytes = bs->total;
508 	        		a->blocks = wrch->blocks;
509 	        		a->ptr = bs->rp;
510 				wrch->blocks = 0;
511 	    		} else ret = EINVAL;
512 		}
513 		break;
514 
515     	case SNDCTL_DSP_GETCAPS:
516 		*arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
517 		if (rdch && wrch && !(d->flags & SD_F_SIMPLEX))
518 			*arg_i |= DSP_CAP_DUPLEX;
519 		break;
520 
521     	case SOUND_PCM_READ_BITS:
522         	*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_16BIT)? 16 : 8;
523 		break;
524 
525     	case SNDCTL_DSP_SETTRIGGER:
526 		if (rdch) {
527 			rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
528 		    	if (*arg_i & PCM_ENABLE_INPUT)
529 				rdch->flags |= CHN_F_TRIGGERED;
530 			else
531 				rdch->flags |= CHN_F_NOTRIGGER;
532 			chn_intr(rdch);
533 		}
534 		if (wrch) {
535 			wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
536 		    	if (*arg_i & PCM_ENABLE_OUTPUT)
537 				wrch->flags |= CHN_F_TRIGGERED;
538 			else
539 				wrch->flags |= CHN_F_NOTRIGGER;
540 			chn_intr(wrch);
541 		}
542 		break;
543 
544     	case SNDCTL_DSP_GETTRIGGER:
545 		*arg_i = 0;
546 		if (wrch && wrch->flags & CHN_F_TRIGGERED)
547 			*arg_i |= PCM_ENABLE_OUTPUT;
548 		if (rdch && rdch->flags & CHN_F_TRIGGERED)
549 			*arg_i |= PCM_ENABLE_INPUT;
550 		break;
551 
552 	case SNDCTL_DSP_GETODELAY:
553 		if (wrch) {
554 			snd_dbuf *b = &wrch->buffer;
555 			if (b->dl) {
556 				chn_checkunderflow(wrch);
557 				if (!(wrch->flags & CHN_F_MAPPED))
558 					while (chn_wrfeed(wrch) > 0);
559 			}
560 			*arg = b->total;
561 		} else
562 			ret = EINVAL;
563 		break;
564 
565     	case SNDCTL_DSP_MAPINBUF:
566     	case SNDCTL_DSP_MAPOUTBUF:
567     	case SNDCTL_DSP_SETSYNCRO:
568 		/* undocumented */
569 
570     	case SNDCTL_DSP_POST:
571     	case SOUND_PCM_WRITE_FILTER:
572     	case SOUND_PCM_READ_FILTER:
573 		/* dunno what these do, don't sound important */
574     	default:
575 		DEB(printf("default ioctl chan%d fn 0x%08lx fail\n", chan, cmd));
576 		ret = EINVAL;
577 		break;
578     	}
579     	splx(s);
580     	return ret;
581 }
582 
583 int
584 dsp_poll(snddev_info *d, int chan, int events, struct proc *p)
585 {
586 	int ret = 0, e;
587 	pcm_channel *wrch = NULL, *rdch = NULL;
588 
589 	getchns(d, chan, &rdch, &wrch);
590 	e = events & (POLLOUT | POLLWRNORM);
591 	if (wrch && e) ret |= chn_poll(wrch, e, p);
592 	e = events & (POLLIN | POLLRDNORM);
593 	if (rdch && e) ret |= chn_poll(rdch, e, p);
594 	return ret;
595 }
596 
597 int
598 dsp_mmap(snddev_info *d, int chan, vm_offset_t offset, int nprot)
599 {
600 	pcm_channel *wrch = NULL, *rdch = NULL, *c = NULL;
601 
602 	getchns(d, chan, &rdch, &wrch);
603 	/* XXX this is broken by line 204 of vm/device_pager.c, so force write buffer */
604 	if (1 || (wrch && (nprot & PROT_WRITE))) c = wrch;
605 	else if (rdch && (nprot & PROT_READ)) c = rdch;
606 	if (c) {
607 		c->flags |= CHN_F_MAPPED;
608 		return atop(vtophys(c->buffer2nd.buf + offset));
609 	}
610 	return -1;
611 }
612 
613