xref: /dragonfly/sys/dev/sound/pcm/dsp.c (revision 3f5e28f4)
1 /*-
2  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
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: src/sys/dev/sound/pcm/dsp.c,v 1.80.2.6 2006/04/04 17:43:48 ariff Exp $
27  * $DragonFly: src/sys/dev/sound/pcm/dsp.c,v 1.14 2007/01/04 21:47:03 corecode Exp $
28  */
29 
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 
33 #include <dev/sound/pcm/sound.h>
34 
35 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/dsp.c,v 1.14 2007/01/04 21:47:03 corecode Exp $");
36 
37 #define OLDPCM_IOCTL
38 
39 static d_open_t dsp_open;
40 static d_close_t dsp_close;
41 static d_read_t dsp_read;
42 static d_write_t dsp_write;
43 static d_ioctl_t dsp_ioctl;
44 static d_poll_t dsp_poll;
45 static d_mmap_t dsp_mmap;
46 
47 struct dev_ops dsp_cdevsw = {
48 	{ "dsp", SND_CDEV_MAJOR, 0},
49 	/*.d_flags =	D_NEEDGIANT,*/
50 	.d_open =	dsp_open,
51 	.d_close =	dsp_close,
52 	.d_read =	dsp_read,
53 	.d_write =	dsp_write,
54 	.d_ioctl =	dsp_ioctl,
55 	.d_poll =	dsp_poll,
56 	.d_mmap =	dsp_mmap,
57 };
58 
59 #ifdef USING_DEVFS
60 static eventhandler_tag dsp_ehtag;
61 #endif
62 
63 static struct snddev_info *
64 dsp_get_info(struct cdev *dev)
65 {
66 	struct snddev_info *d;
67 	int unit;
68 
69 	unit = PCMUNIT(dev);
70 	if (unit >= devclass_get_maxunit(pcm_devclass))
71 		return NULL;
72 	d = devclass_get_softc(pcm_devclass, unit);
73 
74 	return d;
75 }
76 
77 static u_int32_t
78 dsp_get_flags(struct cdev *dev)
79 {
80 	device_t bdev;
81 	int unit;
82 
83 	unit = PCMUNIT(dev);
84 	if (unit >= devclass_get_maxunit(pcm_devclass))
85 		return 0xffffffff;
86 	bdev = devclass_get_device(pcm_devclass, unit);
87 
88 	return pcm_getflags(bdev);
89 }
90 
91 static void
92 dsp_set_flags(struct cdev *dev, u_int32_t flags)
93 {
94 	device_t bdev;
95 	int unit;
96 
97 	unit = PCMUNIT(dev);
98 	if (unit >= devclass_get_maxunit(pcm_devclass))
99 		return;
100 	bdev = devclass_get_device(pcm_devclass, unit);
101 
102 	pcm_setflags(bdev, flags);
103 }
104 
105 /*
106  * return the channels associated with an open device instance.
107  * set the priority if the device is simplex and one direction (only) is
108  * specified.
109  * lock channels specified.
110  */
111 static int
112 getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch, u_int32_t prio)
113 {
114 	struct snddev_info *d;
115 	u_int32_t flags;
116 
117 	flags = dsp_get_flags(dev);
118 	d = dsp_get_info(dev);
119 	pcm_inprog(d, 1);
120 	pcm_lock(d);
121 	KASSERT((flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
122 		("getchns: read and write both prioritised"));
123 
124 	if ((flags & SD_F_PRIO_SET) == 0 && (prio != (SD_F_PRIO_RD | SD_F_PRIO_WR))) {
125 		flags |= prio & (SD_F_PRIO_RD | SD_F_PRIO_WR);
126 		dsp_set_flags(dev, flags);
127 	}
128 
129 	*rdch = dev->si_drv1;
130 	*wrch = dev->si_drv2;
131 	if ((flags & SD_F_SIMPLEX) && (flags & SD_F_PRIO_SET)) {
132 		if (prio) {
133 			if (*rdch && flags & SD_F_PRIO_WR) {
134 				dev->si_drv1 = NULL;
135 				*rdch = pcm_getfakechan(d);
136 			} else if (*wrch && flags & SD_F_PRIO_RD) {
137 				dev->si_drv2 = NULL;
138 				*wrch = pcm_getfakechan(d);
139 			}
140 		}
141 
142 		pcm_getfakechan(d)->flags |= CHN_F_BUSY;
143 	}
144 	pcm_unlock(d);
145 
146 	if (*rdch && *rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
147 		CHN_LOCK(*rdch);
148 	if (*wrch && *wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
149 		CHN_LOCK(*wrch);
150 
151 	return 0;
152 }
153 
154 /* unlock specified channels */
155 static void
156 relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch, u_int32_t prio)
157 {
158 	struct snddev_info *d;
159 
160 	d = dsp_get_info(dev);
161 	if (wrch && wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
162 		CHN_UNLOCK(wrch);
163 	if (rdch && rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
164 		CHN_UNLOCK(rdch);
165 	pcm_inprog(d, -1);
166 }
167 
168 static int
169 dsp_open(struct dev_open_args *ap)
170 {
171 	struct cdev *i_dev = ap->a_head.a_dev;
172 	struct thread *td = curthread;
173 	int flags = ap->a_oflags;
174 	struct pcm_channel *rdch, *wrch;
175 	struct snddev_info *d;
176 	u_int32_t fmt;
177 	int devtype;
178 	int error;
179 	int chnum;
180 
181 	if (i_dev == NULL || td == NULL)
182 		return ENODEV;
183 
184 	if ((flags & (FREAD | FWRITE)) == 0)
185 		return EINVAL;
186 
187 	d = dsp_get_info(i_dev);
188 	devtype = PCMDEV(i_dev);
189 	chnum = -1;
190 
191 	/* decide default format */
192 	switch (devtype) {
193 	case SND_DEV_DSP16:
194 		fmt = AFMT_S16_LE;
195 		break;
196 
197 	case SND_DEV_DSP:
198 		fmt = AFMT_U8;
199 		break;
200 
201 	case SND_DEV_AUDIO:
202 		fmt = AFMT_MU_LAW;
203 		break;
204 
205 	case SND_DEV_NORESET:
206 		fmt = 0;
207 		break;
208 
209 	case SND_DEV_DSPREC:
210 		fmt = AFMT_U8;
211 		if (flags & FWRITE)
212 			return EINVAL;
213 		chnum = PCMCHAN(i_dev);
214 		break;
215 
216 	default:
217 		panic("impossible devtype %d", devtype);
218 	}
219 
220 	/* lock snddev so nobody else can monkey with it */
221 	pcm_lock(d);
222 
223 	rdch = i_dev->si_drv1;
224 	wrch = i_dev->si_drv2;
225 
226 	if (rdch || wrch || ((dsp_get_flags(i_dev) & SD_F_SIMPLEX) &&
227 		    (flags & (FREAD | FWRITE)) == (FREAD | FWRITE))) {
228 		/* simplex or not, better safe than sorry. */
229 		pcm_unlock(d);
230 		return EBUSY;
231 	}
232 
233 	/*
234 	 * if we get here, the open request is valid- either:
235 	 *   * we were previously not open
236 	 *   * we were open for play xor record and the opener wants
237 	 *     the non-open direction
238 	 */
239 	if (flags & FREAD) {
240 		/* open for read */
241 		pcm_unlock(d);
242 		error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, chnum);
243 		if (error != 0 && error != EBUSY && chnum != -1 && (flags & FWRITE))
244 			error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, -1);
245 
246 		if (error == 0 && (chn_reset(rdch, fmt) ||
247 				(fmt && chn_setspeed(rdch, DSP_DEFAULT_SPEED))))
248 			error = ENODEV;
249 
250 		if (error != 0) {
251 			if (rdch)
252 				pcm_chnrelease(rdch);
253 			return error;
254 		}
255 
256 		if (flags & O_NONBLOCK)
257 			rdch->flags |= CHN_F_NBIO;
258 		pcm_chnref(rdch, 1);
259 	 	CHN_UNLOCK(rdch);
260 		pcm_lock(d);
261 	}
262 
263 	if (flags & FWRITE) {
264 	    /* open for write */
265 	    pcm_unlock(d);
266 	    error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, chnum);
267 	    if (error != 0 && error != EBUSY && chnum != -1 && (flags & FREAD))
268 	    	error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, -1);
269 
270 	    if (error == 0 && (chn_reset(wrch, fmt) ||
271 	    		(fmt && chn_setspeed(wrch, DSP_DEFAULT_SPEED))))
272 		error = ENODEV;
273 
274 	    if (error != 0) {
275 		if (wrch)
276 		    pcm_chnrelease(wrch);
277 		if (rdch) {
278 		    /*
279 		     * Lock, deref and release previously created record channel
280 		     */
281 		    CHN_LOCK(rdch);
282 		    pcm_chnref(rdch, -1);
283 		    pcm_chnrelease(rdch);
284 		}
285 
286 		return error;
287 	    }
288 
289 	    if (flags & O_NONBLOCK)
290 		wrch->flags |= CHN_F_NBIO;
291 	    pcm_chnref(wrch, 1);
292 	    CHN_UNLOCK(wrch);
293 	    pcm_lock(d);
294 	}
295 
296 	i_dev->si_drv1 = rdch;
297 	i_dev->si_drv2 = wrch;
298 
299 	pcm_unlock(d);
300 	return 0;
301 }
302 
303 static int
304 dsp_close(struct dev_close_args *ap)
305 {
306 	struct cdev *i_dev = ap->a_head.a_dev;
307 	struct pcm_channel *rdch, *wrch;
308 	struct snddev_info *d;
309 	int refs;
310 
311 	d = dsp_get_info(i_dev);
312 	pcm_lock(d);
313 	rdch = i_dev->si_drv1;
314 	wrch = i_dev->si_drv2;
315 	pcm_unlock(d);
316 
317 	if (rdch || wrch) {
318 		refs = 0;
319 		if (rdch) {
320 			CHN_LOCK(rdch);
321 			refs += pcm_chnref(rdch, -1);
322 			chn_abort(rdch); /* won't sleep */
323 			rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
324 			chn_reset(rdch, 0);
325 			pcm_chnrelease(rdch);
326 		}
327 		if (wrch) {
328 			CHN_LOCK(wrch);
329 			refs += pcm_chnref(wrch, -1);
330 			/*
331 			 * XXX: Maybe the right behaviour is to abort on non_block.
332 			 * It seems that mplayer flushes the audio queue by quickly
333 			 * closing and re-opening.  In FBSD, there's a long pause
334 			 * while the audio queue flushes that I presume isn't there in
335 			 * linux.
336 			 */
337 			chn_flush(wrch); /* may sleep */
338 			wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
339 			chn_reset(wrch, 0);
340 			pcm_chnrelease(wrch);
341 		}
342 
343 		pcm_lock(d);
344 		if (rdch)
345 			i_dev->si_drv1 = NULL;
346 		if (wrch)
347 			i_dev->si_drv2 = NULL;
348 		/*
349 		 * If there are no more references, release the channels.
350 		 */
351 		if (refs == 0 && i_dev->si_drv1 == NULL &&
352 			    i_dev->si_drv2 == NULL) {
353 			if (pcm_getfakechan(d))
354 				pcm_getfakechan(d)->flags = 0;
355 			/* What is this?!? */
356 			dsp_set_flags(i_dev, dsp_get_flags(i_dev) & ~SD_F_TRANSIENT);
357 		}
358 		pcm_unlock(d);
359 	}
360 	return 0;
361 }
362 
363 static int
364 dsp_read(struct dev_read_args *ap)
365 {
366 	struct cdev *i_dev = ap->a_head.a_dev;
367 	struct uio *buf = ap->a_uio;
368 	int flag = ap->a_ioflag;
369 	struct pcm_channel *rdch, *wrch;
370 	int ret;
371 
372 	getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD);
373 
374 	KASSERT(rdch, ("dsp_read: nonexistant channel"));
375 	KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel"));
376 
377 	if (rdch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
378 		relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
379 		return EINVAL;
380 	}
381 	if (!(rdch->flags & CHN_F_RUNNING))
382 		rdch->flags |= CHN_F_RUNNING;
383 	ret = chn_read(rdch, buf, flag);
384 	relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
385 
386 	return ret;
387 }
388 
389 static int
390 dsp_write(struct dev_write_args *ap)
391 {
392 	struct cdev *i_dev = ap->a_head.a_dev;
393 	struct uio *buf = ap->a_uio;
394 	int flag = ap->a_ioflag;
395 	struct pcm_channel *rdch, *wrch;
396 	int ret;
397 
398 	getchns(i_dev, &rdch, &wrch, SD_F_PRIO_WR);
399 
400 	KASSERT(wrch, ("dsp_write: nonexistant channel"));
401 	KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel"));
402 
403 	if (wrch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
404 		relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
405 		return EINVAL;
406 	}
407 	if (!(wrch->flags & CHN_F_RUNNING))
408 		wrch->flags |= CHN_F_RUNNING;
409 	ret = chn_write(wrch, buf, flag);
410 	relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
411 
412 	return ret;
413 }
414 
415 static int
416 dsp_ioctl(struct dev_ioctl_args *ap)
417 {
418 	struct cdev *i_dev = ap->a_head.a_dev;
419 	u_long cmd = ap->a_cmd;
420 	caddr_t arg = ap->a_data;
421     	struct pcm_channel *chn, *rdch, *wrch;
422 	struct snddev_info *d;
423 	int kill;
424     	int ret = 0, *arg_i = (int *)arg, tmp;
425 
426 	/*
427 	 * this is an evil hack to allow broken apps to perform mixer ioctls
428 	 * on dsp devices.
429 	 */
430 
431 	d = dsp_get_info(i_dev);
432 	if (IOCGROUP(cmd) == 'M') {
433 		/*
434 		 * This is at least, a bug to bug compatible with OSS.
435 		 */
436 		if (d->mixer_dev != NULL) {
437 			ap->a_head.a_dev = d->mixer_dev;
438 			return mixer_ioctl(ap);
439 		} else {
440 			return EBADF;
441 		}
442 	}
443 
444 	getchns(i_dev, &rdch, &wrch, 0);
445 
446 	kill = 0;
447 	if (wrch && (wrch->flags & CHN_F_DEAD))
448 		kill |= 1;
449 	if (rdch && (rdch->flags & CHN_F_DEAD))
450 		kill |= 2;
451 	if (kill == 3) {
452 		relchns(i_dev, rdch, wrch, 0);
453 		return EINVAL;
454 	}
455 	if (kill & 1)
456 		wrch = NULL;
457 	if (kill & 2)
458 		rdch = NULL;
459 
460     	switch(cmd) {
461 #ifdef OLDPCM_IOCTL
462     	/*
463      	 * we start with the new ioctl interface.
464      	 */
465     	case AIONWRITE:	/* how many bytes can write ? */
466 		if (wrch) {
467 			CHN_LOCK(wrch);
468 /*
469 		if (wrch && wrch->bufhard.dl)
470 			while (chn_wrfeed(wrch) == 0);
471 */
472 			*arg_i = sndbuf_getfree(wrch->bufsoft);
473 			CHN_UNLOCK(wrch);
474 		} else {
475 			*arg_i = 0;
476 			ret = EINVAL;
477 		}
478 		break;
479 
480     	case AIOSSIZE:     /* set the current blocksize */
481 		{
482 	    		struct snd_size *p = (struct snd_size *)arg;
483 
484 			p->play_size = 0;
485 			p->rec_size = 0;
486 	    		if (wrch) {
487 				CHN_LOCK(wrch);
488 				chn_setblocksize(wrch, 2, p->play_size);
489 				p->play_size = sndbuf_getblksz(wrch->bufsoft);
490 				CHN_UNLOCK(wrch);
491 			}
492 	    		if (rdch) {
493 				CHN_LOCK(rdch);
494 				chn_setblocksize(rdch, 2, p->rec_size);
495 				p->rec_size = sndbuf_getblksz(rdch->bufsoft);
496 				CHN_UNLOCK(rdch);
497 			}
498 		}
499 		break;
500     	case AIOGSIZE:	/* get the current blocksize */
501 		{
502 	    		struct snd_size *p = (struct snd_size *)arg;
503 
504 	    		if (wrch) {
505 				CHN_LOCK(wrch);
506 				p->play_size = sndbuf_getblksz(wrch->bufsoft);
507 				CHN_UNLOCK(wrch);
508 			}
509 	    		if (rdch) {
510 				CHN_LOCK(rdch);
511 				p->rec_size = sndbuf_getblksz(rdch->bufsoft);
512 				CHN_UNLOCK(rdch);
513 			}
514 		}
515 		break;
516 
517     	case AIOSFMT:
518     	case AIOGFMT:
519 		{
520 	    		snd_chan_param *p = (snd_chan_param *)arg;
521 
522 			if (cmd == AIOSFMT &&
523 			    ((p->play_format != 0 && p->play_rate == 0) ||
524 			    (p->rec_format != 0 && p->rec_rate == 0))) {
525 				ret = EINVAL;
526 				break;
527 			}
528 	    		if (wrch) {
529 				CHN_LOCK(wrch);
530 				if (cmd == AIOSFMT && p->play_format != 0) {
531 					chn_setformat(wrch, p->play_format);
532 					chn_setspeed(wrch, p->play_rate);
533 				}
534 	    			p->play_rate = wrch->speed;
535 	    			p->play_format = wrch->format;
536 				CHN_UNLOCK(wrch);
537 			} else {
538 	    			p->play_rate = 0;
539 	    			p->play_format = 0;
540 	    		}
541 	    		if (rdch) {
542 				CHN_LOCK(rdch);
543 				if (cmd == AIOSFMT && p->rec_format != 0) {
544 					chn_setformat(rdch, p->rec_format);
545 					chn_setspeed(rdch, p->rec_rate);
546 				}
547 				p->rec_rate = rdch->speed;
548 				p->rec_format = rdch->format;
549 				CHN_UNLOCK(rdch);
550 			} else {
551 	    			p->rec_rate = 0;
552 	    			p->rec_format = 0;
553 	    		}
554 		}
555 		break;
556 
557     	case AIOGCAP:     /* get capabilities */
558 		{
559 	    		snd_capabilities *p = (snd_capabilities *)arg;
560 			struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
561 			struct cdev *pdev;
562 
563 			if (rdch) {
564 				CHN_LOCK(rdch);
565 				rcaps = chn_getcaps(rdch);
566 			}
567 			if (wrch) {
568 				CHN_LOCK(wrch);
569 				pcaps = chn_getcaps(wrch);
570 			}
571 	    		p->rate_min = max(rcaps? rcaps->minspeed : 0,
572 	                      		  pcaps? pcaps->minspeed : 0);
573 	    		p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
574 	                      		  pcaps? pcaps->maxspeed : 1000000);
575 	    		p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
576 	                     		 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
577 			/* XXX bad on sb16 */
578 	    		p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
579 			 	     (wrch? chn_getformats(wrch) : 0xffffffff);
580 			if (rdch && wrch)
581 				p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
582 			pdev = d->mixer_dev;
583 	    		p->mixers = 1; /* default: one mixer */
584 	    		p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
585 	    		p->left = p->right = 100;
586 			if (rdch)
587 				CHN_UNLOCK(rdch);
588 			if (wrch)
589 				CHN_UNLOCK(wrch);
590 		}
591 		break;
592 
593     	case AIOSTOP:
594 		if (*arg_i == AIOSYNC_PLAY && wrch) {
595 			CHN_LOCK(wrch);
596 			*arg_i = chn_abort(wrch);
597 			CHN_UNLOCK(wrch);
598 		} else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
599 			CHN_LOCK(rdch);
600 			*arg_i = chn_abort(rdch);
601 			CHN_UNLOCK(rdch);
602 		} else {
603 	   	 	kprintf("AIOSTOP: bad channel 0x%x\n", *arg_i);
604 	    		*arg_i = 0;
605 		}
606 		break;
607 
608     	case AIOSYNC:
609 		kprintf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
610 	    		((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
611 		break;
612 #endif
613 	/*
614 	 * here follow the standard ioctls (filio.h etc.)
615 	 */
616     	case FIONREAD: /* get # bytes to read */
617 		if (rdch) {
618 			CHN_LOCK(rdch);
619 /*			if (rdch && rdch->bufhard.dl)
620 				while (chn_rdfeed(rdch) == 0);
621 */
622 			*arg_i = sndbuf_getready(rdch->bufsoft);
623 			CHN_UNLOCK(rdch);
624 		} else {
625 			*arg_i = 0;
626 			ret = EINVAL;
627 		}
628 		break;
629 
630     	case FIOASYNC: /*set/clear async i/o */
631 		DEB( kprintf("FIOASYNC\n") ; )
632 		break;
633 
634     	case SNDCTL_DSP_NONBLOCK:
635     	case FIONBIO: /* set/clear non-blocking i/o */
636 		if (rdch) {
637 			CHN_LOCK(rdch);
638 			if (*arg_i)
639 				rdch->flags |= CHN_F_NBIO;
640 			else
641 				rdch->flags &= ~CHN_F_NBIO;
642 			CHN_UNLOCK(rdch);
643 		}
644 		if (wrch) {
645 			CHN_LOCK(wrch);
646 			if (*arg_i)
647 				wrch->flags |= CHN_F_NBIO;
648 			else
649 				wrch->flags &= ~CHN_F_NBIO;
650 			CHN_UNLOCK(wrch);
651 		}
652 		break;
653 
654     	/*
655 	 * Finally, here is the linux-compatible ioctl interface
656 	 */
657 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
658     	case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
659     	case SNDCTL_DSP_GETBLKSIZE:
660 		chn = wrch ? wrch : rdch;
661 		if (chn) {
662 			CHN_LOCK(chn);
663 			*arg_i = sndbuf_getblksz(chn->bufsoft);
664 			CHN_UNLOCK(chn);
665 		} else {
666 			*arg_i = 0;
667 			ret = EINVAL;
668 		}
669 		break ;
670 
671     	case SNDCTL_DSP_SETBLKSIZE:
672 		RANGE(*arg_i, 16, 65536);
673 		if (wrch) {
674 			CHN_LOCK(wrch);
675 			chn_setblocksize(wrch, 2, *arg_i);
676 			CHN_UNLOCK(wrch);
677 		}
678 		if (rdch) {
679 			CHN_LOCK(rdch);
680 			chn_setblocksize(rdch, 2, *arg_i);
681 			CHN_UNLOCK(rdch);
682 		}
683 		break;
684 
685     	case SNDCTL_DSP_RESET:
686 		DEB(kprintf("dsp reset\n"));
687 		if (wrch) {
688 			CHN_LOCK(wrch);
689 			chn_abort(wrch);
690 			chn_resetbuf(wrch);
691 			CHN_UNLOCK(wrch);
692 		}
693 		if (rdch) {
694 			CHN_LOCK(rdch);
695 			chn_abort(rdch);
696 			chn_resetbuf(rdch);
697 			CHN_UNLOCK(rdch);
698 		}
699 		break;
700 
701     	case SNDCTL_DSP_SYNC:
702 		DEB(kprintf("dsp sync\n"));
703 		/* chn_sync may sleep */
704 		if (wrch) {
705 			CHN_LOCK(wrch);
706 			chn_sync(wrch, sndbuf_getsize(wrch->bufsoft) - 4);
707 			CHN_UNLOCK(wrch);
708 		}
709 		break;
710 
711     	case SNDCTL_DSP_SPEED:
712 		/* chn_setspeed may sleep */
713 		tmp = 0;
714 		if (wrch) {
715 			CHN_LOCK(wrch);
716 			ret = chn_setspeed(wrch, *arg_i);
717 			tmp = wrch->speed;
718 			CHN_UNLOCK(wrch);
719 		}
720 		if (rdch && ret == 0) {
721 			CHN_LOCK(rdch);
722 			ret = chn_setspeed(rdch, *arg_i);
723 			if (tmp == 0)
724 				tmp = rdch->speed;
725 			CHN_UNLOCK(rdch);
726 		}
727 		*arg_i = tmp;
728 		break;
729 
730     	case SOUND_PCM_READ_RATE:
731 		chn = wrch ? wrch : rdch;
732 		if (chn) {
733 			CHN_LOCK(chn);
734 			*arg_i = chn->speed;
735 			CHN_UNLOCK(chn);
736 		} else {
737 			*arg_i = 0;
738 			ret = EINVAL;
739 		}
740 		break;
741 
742     	case SNDCTL_DSP_STEREO:
743 		tmp = -1;
744 		*arg_i = (*arg_i)? AFMT_STEREO : 0;
745 		if (wrch) {
746 			CHN_LOCK(wrch);
747 			ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
748 			tmp = (wrch->format & AFMT_STEREO)? 1 : 0;
749 			CHN_UNLOCK(wrch);
750 		}
751 		if (rdch && ret == 0) {
752 			CHN_LOCK(rdch);
753 			ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
754 			if (tmp == -1)
755 				tmp = (rdch->format & AFMT_STEREO)? 1 : 0;
756 			CHN_UNLOCK(rdch);
757 		}
758 		*arg_i = tmp;
759 		break;
760 
761     	case SOUND_PCM_WRITE_CHANNELS:
762 /*	case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
763 		if (*arg_i != 0) {
764 			tmp = 0;
765 			*arg_i = (*arg_i != 1)? AFMT_STEREO : 0;
766 	  		if (wrch) {
767 				CHN_LOCK(wrch);
768 				ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
769 				tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
770 				CHN_UNLOCK(wrch);
771 			}
772 			if (rdch && ret == 0) {
773 				CHN_LOCK(rdch);
774 				ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
775 				if (tmp == 0)
776 					tmp = (rdch->format & AFMT_STEREO)? 2 : 1;
777 				CHN_UNLOCK(rdch);
778 			}
779 			*arg_i = tmp;
780 		} else {
781 			chn = wrch ? wrch : rdch;
782 			CHN_LOCK(chn);
783 			*arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
784 			CHN_UNLOCK(chn);
785 		}
786 		break;
787 
788     	case SOUND_PCM_READ_CHANNELS:
789 		chn = wrch ? wrch : rdch;
790 		if (chn) {
791 			CHN_LOCK(chn);
792 			*arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
793 			CHN_UNLOCK(chn);
794 		} else {
795 			*arg_i = 0;
796 			ret = EINVAL;
797 		}
798 		break;
799 
800     	case SNDCTL_DSP_GETFMTS:	/* returns a mask of supported fmts */
801 		chn = wrch ? wrch : rdch;
802 		if (chn) {
803 			CHN_LOCK(chn);
804 			*arg_i = chn_getformats(chn);
805 			CHN_UNLOCK(chn);
806 		} else {
807 			*arg_i = 0;
808 			ret = EINVAL;
809 		}
810 		break ;
811 
812     	case SNDCTL_DSP_SETFMT:	/* sets _one_ format */
813 		if ((*arg_i != AFMT_QUERY)) {
814 			tmp = 0;
815 			if (wrch) {
816 				CHN_LOCK(wrch);
817 				ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
818 				tmp = wrch->format & ~AFMT_STEREO;
819 				CHN_UNLOCK(wrch);
820 			}
821 			if (rdch && ret == 0) {
822 				CHN_LOCK(rdch);
823 				ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
824 				if (tmp == 0)
825 					tmp = rdch->format & ~AFMT_STEREO;
826 				CHN_UNLOCK(rdch);
827 			}
828 			*arg_i = tmp;
829 		} else {
830 			chn = wrch ? wrch : rdch;
831 			CHN_LOCK(chn);
832 			*arg_i = chn->format & ~AFMT_STEREO;
833 			CHN_UNLOCK(chn);
834 		}
835 		break;
836 
837     	case SNDCTL_DSP_SETFRAGMENT:
838 		DEB(kprintf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
839 		{
840 			u_int32_t fragln = (*arg_i) & 0x0000ffff;
841 			u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
842 			u_int32_t fragsz;
843 			u_int32_t r_maxfrags, r_fragsz;
844 
845 			RANGE(fragln, 4, 16);
846 			fragsz = 1 << fragln;
847 
848 			if (maxfrags == 0)
849 				maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
850 			if (maxfrags < 2)
851 				maxfrags = 2;
852 			if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
853 				maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
854 
855 			DEB(kprintf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
856 		    	if (rdch) {
857 				CHN_LOCK(rdch);
858 				ret = chn_setblocksize(rdch, maxfrags, fragsz);
859 				r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
860 				r_fragsz = sndbuf_getblksz(rdch->bufsoft);
861 				CHN_UNLOCK(rdch);
862 			} else {
863 				r_maxfrags = maxfrags;
864 				r_fragsz = fragsz;
865 			}
866 		    	if (wrch && ret == 0) {
867 				CHN_LOCK(wrch);
868 				ret = chn_setblocksize(wrch, maxfrags, fragsz);
869  				maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
870 				fragsz = sndbuf_getblksz(wrch->bufsoft);
871 				CHN_UNLOCK(wrch);
872 			} else { /* use whatever came from the read channel */
873 				maxfrags = r_maxfrags;
874 				fragsz = r_fragsz;
875 			}
876 
877 			fragln = 0;
878 			while (fragsz > 1) {
879 				fragln++;
880 				fragsz >>= 1;
881 			}
882 	    		*arg_i = (maxfrags << 16) | fragln;
883 		}
884 		break;
885 
886     	case SNDCTL_DSP_GETISPACE:
887 		/* return the size of data available in the input queue */
888 		{
889 	    		audio_buf_info *a = (audio_buf_info *)arg;
890 	    		if (rdch) {
891 	        		struct snd_dbuf *bs = rdch->bufsoft;
892 
893 				CHN_LOCK(rdch);
894 				a->bytes = sndbuf_getready(bs);
895 	        		a->fragments = a->bytes / sndbuf_getblksz(bs);
896 	        		a->fragstotal = sndbuf_getblkcnt(bs);
897 	        		a->fragsize = sndbuf_getblksz(bs);
898 				CHN_UNLOCK(rdch);
899 	    		}
900 		}
901 		break;
902 
903     	case SNDCTL_DSP_GETOSPACE:
904 		/* return space available in the output queue */
905 		{
906 	    		audio_buf_info *a = (audio_buf_info *)arg;
907 	    		if (wrch) {
908 	        		struct snd_dbuf *bs = wrch->bufsoft;
909 
910 				CHN_LOCK(wrch);
911 				/* XXX abusive DMA update: chn_wrupdate(wrch); */
912 				a->bytes = sndbuf_getfree(bs);
913 	        		a->fragments = a->bytes / sndbuf_getblksz(bs);
914 	        		a->fragstotal = sndbuf_getblkcnt(bs);
915 	        		a->fragsize = sndbuf_getblksz(bs);
916 				CHN_UNLOCK(wrch);
917 	    		}
918 		}
919 		break;
920 
921     	case SNDCTL_DSP_GETIPTR:
922 		{
923 	    		count_info *a = (count_info *)arg;
924 	    		if (rdch) {
925 	        		struct snd_dbuf *bs = rdch->bufsoft;
926 
927 				CHN_LOCK(rdch);
928 				/* XXX abusive DMA update: chn_rdupdate(rdch); */
929 	        		a->bytes = sndbuf_gettotal(bs);
930 	        		a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
931 	        		a->ptr = sndbuf_getreadyptr(bs);
932 				rdch->blocks = sndbuf_getblocks(bs);
933 				CHN_UNLOCK(rdch);
934 	    		} else
935 				ret = EINVAL;
936 		}
937 		break;
938 
939     	case SNDCTL_DSP_GETOPTR:
940 		{
941 	    		count_info *a = (count_info *)arg;
942 	    		if (wrch) {
943 	        		struct snd_dbuf *bs = wrch->bufsoft;
944 
945 				CHN_LOCK(wrch);
946 				/* XXX abusive DMA update: chn_wrupdate(wrch); */
947 	        		a->bytes = sndbuf_gettotal(bs);
948 	        		a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
949 	        		a->ptr = sndbuf_getreadyptr(bs);
950 				wrch->blocks = sndbuf_getblocks(bs);
951 				CHN_UNLOCK(wrch);
952 	    		} else
953 				ret = EINVAL;
954 		}
955 		break;
956 
957     	case SNDCTL_DSP_GETCAPS:
958 		*arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
959 		if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
960 			*arg_i |= DSP_CAP_DUPLEX;
961 		break;
962 
963     	case SOUND_PCM_READ_BITS:
964 		chn = wrch ? wrch : rdch;
965 		if (chn) {
966 			CHN_LOCK(chn);
967 			if (chn->format & AFMT_8BIT)
968 				*arg_i = 8;
969 			else if (chn->format & AFMT_16BIT)
970 				*arg_i = 16;
971 			else if (chn->format & AFMT_24BIT)
972 				*arg_i = 24;
973 			else if (chn->format & AFMT_32BIT)
974 				*arg_i = 32;
975 			else
976 				ret = EINVAL;
977 			CHN_UNLOCK(chn);
978 		} else {
979 			*arg_i = 0;
980 			ret = EINVAL;
981 		}
982 		break;
983 
984     	case SNDCTL_DSP_SETTRIGGER:
985 		if (rdch) {
986 			CHN_LOCK(rdch);
987 			rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
988 		    	if (*arg_i & PCM_ENABLE_INPUT)
989 				chn_start(rdch, 1);
990 			else
991 				rdch->flags |= CHN_F_NOTRIGGER;
992 			CHN_UNLOCK(rdch);
993 		}
994 		if (wrch) {
995 			CHN_LOCK(wrch);
996 			wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
997 		    	if (*arg_i & PCM_ENABLE_OUTPUT)
998 				chn_start(wrch, 1);
999 			else
1000 				wrch->flags |= CHN_F_NOTRIGGER;
1001 			CHN_UNLOCK(wrch);
1002 		}
1003 		break;
1004 
1005     	case SNDCTL_DSP_GETTRIGGER:
1006 		*arg_i = 0;
1007 		if (wrch) {
1008 			CHN_LOCK(wrch);
1009 			if (wrch->flags & CHN_F_TRIGGERED)
1010 				*arg_i |= PCM_ENABLE_OUTPUT;
1011 			CHN_UNLOCK(wrch);
1012 		}
1013 		if (rdch) {
1014 			CHN_LOCK(rdch);
1015 			if (rdch->flags & CHN_F_TRIGGERED)
1016 				*arg_i |= PCM_ENABLE_INPUT;
1017 			CHN_UNLOCK(rdch);
1018 		}
1019 		break;
1020 
1021 	case SNDCTL_DSP_GETODELAY:
1022 		if (wrch) {
1023 			struct snd_dbuf *b = wrch->bufhard;
1024 	        	struct snd_dbuf *bs = wrch->bufsoft;
1025 
1026 			CHN_LOCK(wrch);
1027 			/* XXX abusive DMA update: chn_wrupdate(wrch); */
1028 			*arg_i = sndbuf_getready(b) + sndbuf_getready(bs);
1029 			CHN_UNLOCK(wrch);
1030 		} else
1031 			ret = EINVAL;
1032 		break;
1033 
1034     	case SNDCTL_DSP_POST:
1035 		if (wrch) {
1036 			CHN_LOCK(wrch);
1037 			wrch->flags &= ~CHN_F_NOTRIGGER;
1038 			chn_start(wrch, 1);
1039 			CHN_UNLOCK(wrch);
1040 		}
1041 		break;
1042 
1043 	case SNDCTL_DSP_SETDUPLEX:
1044 		/*
1045 		 * switch to full-duplex mode if card is in half-duplex
1046 		 * mode and is able to work in full-duplex mode
1047 		 */
1048 		if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1049 			dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1050 		break;
1051 
1052     	case SNDCTL_DSP_MAPINBUF:
1053     	case SNDCTL_DSP_MAPOUTBUF:
1054     	case SNDCTL_DSP_SETSYNCRO:
1055 		/* undocumented */
1056 
1057     	case SNDCTL_DSP_SUBDIVIDE:
1058     	case SOUND_PCM_WRITE_FILTER:
1059     	case SOUND_PCM_READ_FILTER:
1060 		/* dunno what these do, don't sound important */
1061 
1062     	default:
1063 		DEB(kprintf("default ioctl fn 0x%08lx fail\n", cmd));
1064 		ret = EINVAL;
1065 		break;
1066     	}
1067 	relchns(i_dev, rdch, wrch, 0);
1068     	return ret;
1069 }
1070 
1071 static int
1072 dsp_poll(struct dev_poll_args *ap)
1073 {
1074 	struct cdev *i_dev = ap->a_head.a_dev;
1075 	int events = ap->a_events;
1076 	struct thread *td = curthread;
1077 	struct pcm_channel *wrch = NULL, *rdch = NULL;
1078 	int ret, e;
1079 
1080 	ret = 0;
1081 	getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1082 
1083 	if (wrch) {
1084 		e = (events & (POLLOUT | POLLWRNORM));
1085 		if (e)
1086 			ret |= chn_poll(wrch, e, td);
1087 	}
1088 	if (rdch) {
1089 		e = (events & (POLLIN | POLLRDNORM));
1090 		if (e)
1091 			ret |= chn_poll(rdch, e, td);
1092 	}
1093 	relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1094 
1095 	ap->a_events = ret;
1096 	return (0);
1097 }
1098 
1099 static int
1100 dsp_mmap(struct dev_mmap_args *ap)
1101 {
1102 	struct cdev *i_dev = ap->a_head.a_dev;
1103 	vm_offset_t offset = ap->a_offset;
1104 	int nprot = ap->a_nprot;
1105 	struct pcm_channel *wrch = NULL, *rdch = NULL, *c;
1106 
1107 	if (nprot & PROT_EXEC)
1108 		return -1;
1109 
1110 	getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1111 #if 0
1112 	/*
1113 	 * XXX the linux api uses the nprot to select read/write buffer
1114 	 * our vm system doesn't allow this, so force write buffer
1115 	 */
1116 
1117 	if (wrch && (nprot & PROT_WRITE)) {
1118 		c = wrch;
1119 	} else if (rdch && (nprot & PROT_READ)) {
1120 		c = rdch;
1121 	} else {
1122 		return -1;
1123 	}
1124 #else
1125 	c = wrch;
1126 #endif
1127 
1128 	if (c == NULL) {
1129 		relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1130 		return -1;
1131 	}
1132 
1133 	if (offset >= sndbuf_getsize(c->bufsoft)) {
1134 		relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1135 		return -1;
1136 	}
1137 
1138 	if (!(c->flags & CHN_F_MAPPED))
1139 		c->flags |= CHN_F_MAPPED;
1140 
1141 	ap->a_result = vtophys(sndbuf_getbufofs(c->bufsoft, offset));
1142 	relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1143 
1144 	return (0);
1145 }
1146 
1147 #ifdef USING_DEVFS
1148 
1149 /*
1150  * Clone logic is this:
1151  * x E X = {dsp, dspW, audio}
1152  * x -> x${sysctl("hw.snd.unit")}
1153  * xN->
1154  *    for i N = 1 to channels of device N
1155  *    	if xN.i isn't busy, return its dev_t
1156  */
1157 static void
1158 dsp_clone(void *arg, struct ucred *cred, char *name, int namelen,
1159     struct cdev **dev)
1160 {
1161 	struct cdev *pdev;
1162 	struct snddev_info *pcm_dev;
1163 	struct snddev_channel *pcm_chan;
1164 	int i, unit, devtype;
1165 	static int devtypes[3] = {SND_DEV_DSP, SND_DEV_DSP16, SND_DEV_AUDIO};
1166 	static char *devnames[3] = {"dsp", "dspW", "audio"};
1167 
1168 	if (*dev != NULL)
1169 		return;
1170 	if (pcm_devclass == NULL)
1171 		return;
1172 
1173 	devtype = 0;
1174 	unit = -1;
1175 	for (i = 0; (i < 3) && (unit == -1); i++) {
1176 		devtype = devtypes[i];
1177 		if (strcmp(name, devnames[i]) == 0) {
1178 			unit = snd_unit;
1179 		} else {
1180 			if (dev_stdclone(name, NULL, devnames[i], &unit) != 1)
1181 				unit = -1;
1182 		}
1183 	}
1184 	if (unit == -1 || unit >= devclass_get_maxunit(pcm_devclass))
1185 		return;
1186 
1187 	pcm_dev = devclass_get_softc(pcm_devclass, unit);
1188 
1189 	if (pcm_dev == NULL)
1190 		return;
1191 
1192 	SLIST_FOREACH(pcm_chan, &pcm_dev->channels, link) {
1193 
1194 		switch(devtype) {
1195 			case SND_DEV_DSP:
1196 				pdev = pcm_chan->dsp_devt;
1197 				break;
1198 			case SND_DEV_DSP16:
1199 				pdev = pcm_chan->dspW_devt;
1200 				break;
1201 			case SND_DEV_AUDIO:
1202 				pdev = pcm_chan->audio_devt;
1203 				break;
1204 			default:
1205 				panic("Unknown devtype %d", devtype);
1206 		}
1207 
1208 		if ((pdev != NULL) && (pdev->si_drv1 == NULL) && (pdev->si_drv2 == NULL)) {
1209 			*dev = pdev;
1210 			dev_ref(*dev);
1211 			return;
1212 		}
1213 	}
1214 }
1215 
1216 static void
1217 dsp_sysinit(void *p)
1218 {
1219 	dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
1220 }
1221 
1222 static void
1223 dsp_sysuninit(void *p)
1224 {
1225 	if (dsp_ehtag != NULL)
1226 		EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
1227 }
1228 
1229 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
1230 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
1231 #endif
1232 
1233 
1234