xref: /freebsd/sys/dev/sound/pcm/dsp.c (revision c68159a6d8eede11766cf13896d0f7670dbd51aa)
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)
244 				p->play_size = wrch->buffer2nd.blksz;
245 	    		if (rdch)
246 				p->rec_size = rdch->buffer2nd.blksz;
247 		}
248 		break;
249 
250     	case AIOSFMT:
251 		{
252 	    		snd_chan_param *p = (snd_chan_param *)arg;
253 	    		if (wrch) {
254 				chn_setformat(wrch, p->play_format);
255 				chn_setspeed(wrch, p->play_rate);
256 	    		}
257 	    		if (rdch) {
258 				chn_setformat(rdch, p->rec_format);
259 				chn_setspeed(rdch, p->rec_rate);
260 	    		}
261 		}
262 		/* FALLTHROUGH */
263 
264     	case AIOGFMT:
265 		{
266 	    		snd_chan_param *p = (snd_chan_param *)arg;
267 	    		p->play_rate = wrch? wrch->speed : 0;
268 	    		p->rec_rate = rdch? rdch->speed : 0;
269 	    		p->play_format = wrch? wrch->format : 0;
270 	    		p->rec_format = rdch? rdch->format : 0;
271 		}
272 		break;
273 
274     	case AIOGCAP:     /* get capabilities */
275 		{
276 	    		snd_capabilities *p = (snd_capabilities *)arg;
277 			pcmchan_caps *pcaps = NULL, *rcaps = NULL;
278 			if (rdch) rcaps = chn_getcaps(rdch);
279 			if (wrch) pcaps = chn_getcaps(wrch);
280 	    		p->rate_min = max(rcaps? rcaps->minspeed : 0,
281 	                      		  pcaps? pcaps->minspeed : 0);
282 	    		p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
283 	                      		  pcaps? pcaps->maxspeed : 1000000);
284 	    		p->bufsize = min(rdch? rdch->buffer2nd.bufsize : 1000000,
285 	                     		 wrch? wrch->buffer2nd.bufsize : 1000000);
286 			/* XXX bad on sb16 */
287 	    		p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
288 			 	     (wrch? chn_getformats(wrch) : 0xffffffff);
289 			if (rdch && wrch)
290 				p->formats |= (d->flags & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
291 	    		p->mixers = 1; /* default: one mixer */
292 	    		p->inputs = mix_getdevs(d->mixer);
293 	    		p->left = p->right = 100;
294 		}
295 		break;
296 
297     	case AIOSTOP:
298 		if (*arg_i == AIOSYNC_PLAY && wrch)
299 			*arg_i = chn_abort(wrch);
300 		else if (*arg_i == AIOSYNC_CAPTURE && rdch)
301 			*arg_i = chn_abort(rdch);
302 		else {
303 	   	 	printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
304 	    		*arg_i = 0;
305 		}
306 		break;
307 
308     	case AIOSYNC:
309 		printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
310 	    		((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
311 		break;
312 #endif
313 	/*
314 	 * here follow the standard ioctls (filio.h etc.)
315 	 */
316     	case FIONREAD: /* get # bytes to read */
317 		if (rdch && rdch->buffer.dl)
318 			while (chn_rdfeed(rdch) > 0);
319 		*arg_i = rdch? rdch->buffer2nd.rl : 0;
320 		break;
321 
322     	case FIOASYNC: /*set/clear async i/o */
323 		DEB( printf("FIOASYNC\n") ; )
324 		break;
325 
326     	case SNDCTL_DSP_NONBLOCK:
327     	case FIONBIO: /* set/clear non-blocking i/o */
328 		if (rdch) rdch->flags &= ~CHN_F_NBIO;
329 		if (wrch) wrch->flags &= ~CHN_F_NBIO;
330 		if (*arg_i) {
331 		    	if (rdch) rdch->flags |= CHN_F_NBIO;
332 		    	if (wrch) wrch->flags |= CHN_F_NBIO;
333 		}
334 		break;
335 
336     	/*
337 	 * Finally, here is the linux-compatible ioctl interface
338 	 */
339 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
340     	case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
341     	case SNDCTL_DSP_GETBLKSIZE:
342 		if (wrch)
343 			*arg_i = wrch->buffer2nd.blksz;
344 		else if (rdch)
345 			*arg_i = rdch->buffer2nd.blksz;
346 		else
347 			*arg_i = 0;
348 		break ;
349 
350     	case SNDCTL_DSP_SETBLKSIZE:
351 		RANGE(*arg_i, 16, 65536);
352 		if (wrch) chn_setblocksize(wrch, 2, *arg_i);
353 		if (rdch) chn_setblocksize(rdch, 2, *arg_i);
354 		break;
355 
356     	case SNDCTL_DSP_RESET:
357 		DEB(printf("dsp reset\n"));
358 		splx(s);
359 		if (wrch) chn_abort(wrch);
360 		if (rdch) chn_abort(rdch);
361 		break;
362 
363     	case SNDCTL_DSP_SYNC:
364 		DEB(printf("dsp sync\n"));
365 		splx(s);
366 		if (wrch) chn_sync(wrch, wrch->buffer2nd.bufsize - 4);
367 		break;
368 
369     	case SNDCTL_DSP_SPEED:
370 		splx(s);
371 		if (wrch)
372 			ret = chn_setspeed(wrch, *arg_i);
373 		if (rdch && ret == 0)
374 			ret = chn_setspeed(rdch, *arg_i);
375 		/* fallthru */
376 
377     	case SOUND_PCM_READ_RATE:
378 		*arg_i = wrch? wrch->speed : rdch->speed;
379 		break;
380 
381     	case SNDCTL_DSP_STEREO:
382 		splx(s);
383 		if (wrch)
384 			ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) |
385 					((*arg_i)? AFMT_STEREO : 0));
386 		if (rdch && ret == 0)
387 			ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) |
388 				        ((*arg_i)? AFMT_STEREO : 0));
389 		*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 1 : 0;
390 		break;
391 
392     	case SOUND_PCM_WRITE_CHANNELS:
393 /*	case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
394 		splx(s);
395 		if (*arg_i == 1 || *arg_i == 2) {
396 	  		if (wrch)
397 				ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) |
398 					((*arg_i == 2)? AFMT_STEREO : 0));
399 			if (rdch && ret == 0)
400 				ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) |
401 				        ((*arg_i == 2)? AFMT_STEREO : 0));
402 			*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 2 : 1;
403 		} else
404 			*arg_i = 0;
405 		break;
406 
407     	case SOUND_PCM_READ_CHANNELS:
408 		*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 2 : 1;
409 		break;
410 
411     	case SNDCTL_DSP_GETFMTS:	/* returns a mask of supported fmts */
412 		*arg_i = wrch? chn_getformats(wrch) : chn_getformats(rdch);
413 		break ;
414 
415     	case SNDCTL_DSP_SETFMT:	/* sets _one_ format */
416 		splx(s);
417 		if ((*arg_i != AFMT_QUERY)) {
418 			if (wrch)
419 				ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
420 			if (rdch && ret == 0)
421 				ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
422 		}
423 		*arg_i = (wrch? wrch->format : rdch->format) & ~AFMT_STEREO;
424 		break;
425 
426     	case SNDCTL_DSP_SUBDIVIDE:
427 		/* XXX watch out, this is RW! */
428 		printf("SNDCTL_DSP_SUBDIVIDE unimplemented\n");
429 		break;
430 
431     	case SNDCTL_DSP_SETFRAGMENT:
432 		DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
433 		{
434 			pcm_channel *c = wrch? wrch : rdch;
435 			u_int32_t fragln = (*arg_i) & 0x0000ffff;
436 			u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
437 			u_int32_t fragsz;
438 
439 			RANGE(fragln, 4, 16);
440 			fragsz = 1 << fragln;
441 
442 			if (maxfrags == 0)
443 				maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
444 			if (maxfrags < 2) {
445 				ret = EINVAL;
446 				break;
447 			}
448 			if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
449 				maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
450 
451 			DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
452 		    	if (rdch)
453 				ret = chn_setblocksize(rdch, maxfrags, fragsz);
454 		    	if (wrch && ret == 0)
455 				ret = chn_setblocksize(wrch, maxfrags, fragsz);
456 
457 			fragsz = c->buffer2nd.blksz;
458 			fragln = 0;
459 			while (fragsz > 1) {
460 				fragln++;
461 				fragsz >>= 1;
462 			}
463 	    		*arg_i = (c->buffer2nd.blkcnt << 16) | fragln;
464 		}
465 		break;
466 
467     	case SNDCTL_DSP_GETISPACE: /* XXX Space for reading? Makes no sense... */
468 		/* return the size of data available in the input queue */
469 		{
470 	    		audio_buf_info *a = (audio_buf_info *)arg;
471 	    		if (rdch) {
472 	        		snd_dbuf *b = &rdch->buffer;
473 	        		snd_dbuf *bs = &rdch->buffer2nd;
474 				if (b->dl && !(rdch->flags & CHN_F_MAPPED))
475 					/*
476 					 * Suck up the secondary and DMA buffer.
477 					 * chn_rdfeed*() takes care of the alignment.
478 					 */
479 					while (chn_rdfeed(rdch) > 0);
480 				a->bytes = bs->rl;
481 	        		a->fragments = a->bytes / rdch->buffer2nd.blksz;
482 	        		a->fragstotal = rdch->buffer2nd.blkcnt;
483 	        		a->fragsize = rdch->buffer2nd.blksz;
484 	    		}
485 		}
486 		break;
487 
488     	case SNDCTL_DSP_GETOSPACE:
489 		/* return space available in the output queue */
490 		{
491 	    		audio_buf_info *a = (audio_buf_info *)arg;
492 	    		if (wrch) {
493 	        		snd_dbuf *b = &wrch->buffer;
494 	        		snd_dbuf *bs = &wrch->buffer2nd;
495 				if (b->dl && !(wrch->flags & CHN_F_MAPPED)) {
496 					/*
497 					 * Fill up the secondary and DMA buffer.
498 					 * chn_wrfeed*() takes care of the alignment.
499 					 * Check for underflow before writing into the buffers.
500 					 */
501 					chn_checkunderflow(wrch);
502 					while (chn_wrfeed(wrch) > 0);
503 				}
504 				a->bytes = bs->fl;
505 	        		a->fragments = a->bytes / wrch->buffer2nd.blksz;
506 	        		a->fragstotal = wrch->buffer2nd.blkcnt;
507 	        		a->fragsize = wrch->buffer2nd.blksz;
508 	    		}
509 		}
510 		break;
511 
512     	case SNDCTL_DSP_GETIPTR:
513 		{
514 	    		count_info *a = (count_info *)arg;
515 	    		if (rdch) {
516 	        		snd_dbuf *b = &rdch->buffer;
517 	        		snd_dbuf *bs = &rdch->buffer2nd;
518 	        		if (b->dl && !(rdch->flags & CHN_F_MAPPED))
519 					/*
520 					 * Suck up the secondary and DMA buffer.
521 					 * chn_rdfeed*() takes care of the alignment.
522 					 */
523 					while (chn_rdfeed(rdch) > 0);
524 	        		a->bytes = bs->total;
525 	        		a->blocks = rdch->blocks;
526 	        		a->ptr = bs->rp;
527 				rdch->blocks = 0;
528 	    		} else ret = EINVAL;
529 		}
530 		break;
531 
532     	case SNDCTL_DSP_GETOPTR:
533 		{
534 	    		count_info *a = (count_info *)arg;
535 	    		if (wrch) {
536     	        		snd_dbuf *b = &wrch->buffer;
537 	        		snd_dbuf *bs = &wrch->buffer2nd;
538 				if (b->dl && !(wrch->flags & CHN_F_MAPPED)) {
539 					/*
540 					 * Fill up the secondary and DMA buffer.
541 					 * chn_wrfeed*() takes care of the alignment.
542 					 * Check for underflow before writing into the buffers.
543 					 */
544 					chn_checkunderflow(wrch);
545 					while (chn_wrfeed(wrch) > 0);
546 				}
547 	        		a->bytes = bs->total;
548 	        		a->blocks = wrch->blocks;
549 	        		a->ptr = bs->rp;
550 				wrch->blocks = 0;
551 	    		} else ret = EINVAL;
552 		}
553 		break;
554 
555     	case SNDCTL_DSP_GETCAPS:
556 		*arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
557 		if (rdch && wrch && !(d->flags & SD_F_SIMPLEX))
558 			*arg_i |= DSP_CAP_DUPLEX;
559 		break;
560 
561     	case SOUND_PCM_READ_BITS:
562         	*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_16BIT)? 16 : 8;
563 		break;
564 
565     	case SNDCTL_DSP_SETTRIGGER:
566 		if (rdch) {
567 			rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
568 		    	if (*arg_i & PCM_ENABLE_INPUT)
569 				rdch->flags |= CHN_F_TRIGGERED;
570 			else
571 				rdch->flags |= CHN_F_NOTRIGGER;
572 			chn_intr(rdch);
573 		}
574 		if (wrch) {
575 			wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
576 		    	if (*arg_i & PCM_ENABLE_OUTPUT)
577 				wrch->flags |= CHN_F_TRIGGERED;
578 			else
579 				wrch->flags |= CHN_F_NOTRIGGER;
580 			chn_intr(wrch);
581 		}
582 		break;
583 
584     	case SNDCTL_DSP_GETTRIGGER:
585 		*arg_i = 0;
586 		if (wrch && wrch->flags & CHN_F_TRIGGERED)
587 			*arg_i |= PCM_ENABLE_OUTPUT;
588 		if (rdch && rdch->flags & CHN_F_TRIGGERED)
589 			*arg_i |= PCM_ENABLE_INPUT;
590 		break;
591 
592 	case SNDCTL_DSP_GETODELAY:
593 		if (wrch) {
594 			snd_dbuf *b = &wrch->buffer;
595 	        	snd_dbuf *bs = &wrch->buffer2nd;
596 			if (b->dl) {
597 				chn_checkunderflow(wrch);
598 				if (!(wrch->flags & CHN_F_MAPPED))
599 					while (chn_wrfeed(wrch) > 0);
600 			}
601 			*arg_i = b->rl + bs->rl;
602 		} else
603 			ret = EINVAL;
604 		break;
605 
606     	case SNDCTL_DSP_POST:
607 		if (wrch) {
608 			wrch->flags &= ~CHN_F_NOTRIGGER;
609 			chn_start(wrch, 1);
610 		}
611 		break;
612 
613     	case SNDCTL_DSP_MAPINBUF:
614     	case SNDCTL_DSP_MAPOUTBUF:
615     	case SNDCTL_DSP_SETSYNCRO:
616 		/* undocumented */
617 
618     	case SOUND_PCM_WRITE_FILTER:
619     	case SOUND_PCM_READ_FILTER:
620 		/* dunno what these do, don't sound important */
621     	default:
622 		DEB(printf("default ioctl chan%d fn 0x%08lx fail\n", chan, cmd));
623 		ret = EINVAL;
624 		break;
625     	}
626     	splx(s);
627     	return ret;
628 }
629 
630 int
631 dsp_poll(snddev_info *d, int chan, int events, struct proc *p)
632 {
633 	int ret = 0, e;
634 	pcm_channel *wrch = NULL, *rdch = NULL;
635 
636 	getchns(d, chan, &rdch, &wrch);
637 	e = events & (POLLOUT | POLLWRNORM);
638 	if (wrch && e) ret |= chn_poll(wrch, e, p);
639 	e = events & (POLLIN | POLLRDNORM);
640 	if (rdch && e) ret |= chn_poll(rdch, e, p);
641 	return ret;
642 }
643 
644 int
645 dsp_mmap(snddev_info *d, int chan, vm_offset_t offset, int nprot)
646 {
647 	pcm_channel *wrch = NULL, *rdch = NULL, *c = NULL;
648 
649 	getchns(d, chan, &rdch, &wrch);
650 	/* XXX this is broken by line 204 of vm/device_pager.c, so force write buffer */
651 	if (1 || (wrch && (nprot & PROT_WRITE)))
652 		c = wrch;
653 	else if (rdch && (nprot & PROT_READ))
654 		c = rdch;
655 	if (c && (c->format == c->buffer.fmt)) {
656 		c->flags |= CHN_F_MAPPED;
657 		return atop(vtophys(c->buffer2nd.buf + offset));
658 	} else
659 		return -1;
660 }
661 
662