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