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