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