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