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