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