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