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