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