xref: /dragonfly/sys/dev/sound/pcm/dsp.c (revision 6bd457ed)
1 /*
2  * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/sound/pcm/dsp.c,v 1.15.2.13 2002/08/30 13:53:03 orion Exp $
27  * $DragonFly: src/sys/dev/sound/pcm/dsp.c,v 1.8 2005/06/10 23:07:01 dillon 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.8 2005/06/10 23:07:01 dillon 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 static struct cdevsw dsp_cdevsw = {
48 	/* name */	"dsp",
49 	/* maj */	SND_CDEV_MAJOR,
50 	/* flags */	0,
51 	/* port */	NULL,
52 	/* clone */	NULL,
53 
54 	/* open */	dsp_open,
55 	/* close */	dsp_close,
56 	/* read */	dsp_read,
57 	/* write */	dsp_write,
58 	/* ioctl */	dsp_ioctl,
59 	/* poll */	dsp_poll,
60 	/* mmap */	dsp_mmap,
61 	/* strategy */	nostrategy,
62 	/* dump */	nodump,
63 	/* psize */	nopsize
64 };
65 
66 #ifdef USING_DEVFS
67 static eventhandler_tag dsp_ehtag;
68 #endif
69 
70 static struct snddev_info *
71 dsp_get_info(dev_t dev)
72 {
73 	struct snddev_info *d;
74 	int unit;
75 
76 	unit = PCMUNIT(dev);
77 	if (unit >= devclass_get_maxunit(pcm_devclass))
78 		return NULL;
79 	d = devclass_get_softc(pcm_devclass, unit);
80 
81 	return d;
82 }
83 
84 static u_int32_t
85 dsp_get_flags(dev_t dev)
86 {
87 	device_t bdev;
88 	int unit;
89 
90 	unit = PCMUNIT(dev);
91 	if (unit >= devclass_get_maxunit(pcm_devclass))
92 		return 0xffffffff;
93 	bdev = devclass_get_device(pcm_devclass, unit);
94 
95 	return pcm_getflags(bdev);
96 }
97 
98 static void
99 dsp_set_flags(dev_t dev, u_int32_t flags)
100 {
101 	device_t bdev;
102 	int unit;
103 
104 	unit = PCMUNIT(dev);
105 	if (unit >= devclass_get_maxunit(pcm_devclass))
106 		return;
107 	bdev = devclass_get_device(pcm_devclass, unit);
108 
109 	pcm_setflags(bdev, flags);
110 }
111 
112 /*
113  * return the channels channels associated with an open device instance.
114  * set the priority if the device is simplex and one direction (only) is
115  * specified.
116  * lock channels specified.
117  */
118 static int
119 getchns(dev_t dev, struct pcm_channel **rdch, struct pcm_channel **wrch, u_int32_t prio)
120 {
121 	struct snddev_info *d;
122 	u_int32_t flags;
123 
124 	flags = dsp_get_flags(dev);
125 	d = dsp_get_info(dev);
126 	pcm_lock(d);
127 	pcm_inprog(d, 1);
128 	KASSERT((flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
129 		("getchns: read and write both prioritised"));
130 
131 	if ((flags & SD_F_PRIO_SET) == 0 && (prio != (SD_F_PRIO_RD | SD_F_PRIO_WR))) {
132 		flags |= prio & (SD_F_PRIO_RD | SD_F_PRIO_WR);
133 		dsp_set_flags(dev, flags);
134 	}
135 
136 	*rdch = dev->si_drv1;
137 	*wrch = dev->si_drv2;
138 	if ((flags & SD_F_SIMPLEX) && (flags & SD_F_PRIO_SET)) {
139 		if (prio) {
140 			if (*rdch && flags & SD_F_PRIO_WR) {
141 				dev->si_drv1 = NULL;
142 				*rdch = pcm_getfakechan(d);
143 			} else if (*wrch && flags & SD_F_PRIO_RD) {
144 				dev->si_drv2 = NULL;
145 				*wrch = pcm_getfakechan(d);
146 			}
147 		}
148 
149 		pcm_getfakechan(d)->flags |= CHN_F_BUSY;
150 	}
151 	pcm_unlock(d);
152 
153 	if (*rdch && *rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
154 		CHN_LOCK(*rdch);
155 	if (*wrch && *wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
156 		CHN_LOCK(*wrch);
157 
158 	return 0;
159 }
160 
161 /* unlock specified channels */
162 static void
163 relchns(dev_t dev, struct pcm_channel *rdch, struct pcm_channel *wrch, u_int32_t prio)
164 {
165 	struct snddev_info *d;
166 
167 	d = dsp_get_info(dev);
168 	if (wrch && wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
169 		CHN_UNLOCK(wrch);
170 	if (rdch && rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
171 		CHN_UNLOCK(rdch);
172 	pcm_lock(d);
173 	pcm_inprog(d, -1);
174 	pcm_unlock(d);
175 }
176 
177 static int
178 dsp_open(dev_t i_dev, int flags, int mode, struct thread *td)
179 {
180 	struct pcm_channel *rdch, *wrch;
181 	struct snddev_info *d;
182 	u_int32_t fmt;
183 	int devtype;
184 	struct proc *p = td->td_proc;
185 
186 	KKASSERT(p != NULL);
187 
188 	crit_enter();
189 	d = dsp_get_info(i_dev);
190 	devtype = PCMDEV(i_dev);
191 
192 	/* decide default format */
193 	switch (devtype) {
194 	case SND_DEV_DSP16:
195 		fmt = AFMT_S16_LE;
196 		break;
197 
198 	case SND_DEV_DSP:
199 		fmt = AFMT_U8;
200 		break;
201 
202 	case SND_DEV_AUDIO:
203 		fmt = AFMT_MU_LAW;
204 		break;
205 
206 	case SND_DEV_NORESET:
207 		fmt = 0;
208 		break;
209 
210 	case SND_DEV_DSPREC:
211 		fmt = AFMT_U8;
212 		if (mode & FWRITE) {
213 			crit_exit();
214 			return EINVAL;
215 		}
216 		break;
217 
218 	default:
219 		panic("impossible devtype %d", devtype);
220 	}
221 
222 	/* lock snddev so nobody else can monkey with it */
223 	pcm_lock(d);
224 
225 	rdch = i_dev->si_drv1;
226 	wrch = i_dev->si_drv2;
227 
228 	if ((dsp_get_flags(i_dev) & SD_F_SIMPLEX) && (rdch || wrch)) {
229 		/* simplex device, already open, exit */
230 		pcm_unlock(d);
231 		crit_exit();
232 		return EBUSY;
233 	}
234 
235 	if (((flags & FREAD) && rdch) || ((flags & FWRITE) && wrch)) {
236 		/* device already open in one or both directions */
237 		pcm_unlock(d);
238 		crit_exit();
239 		return EBUSY;
240 	}
241 
242 	/*  if we get here, the open request is valid */
243 	if (flags & FREAD) {
244 		/* open for read */
245 		if (devtype == SND_DEV_DSPREC)
246 			rdch = pcm_chnalloc(d, PCMDIR_REC, p->p_pid, PCMCHAN(i_dev));
247 		else
248 			rdch = pcm_chnalloc(d, PCMDIR_REC, p->p_pid, -1);
249 		if (!rdch) {
250 			/* no channel available, exit */
251 			pcm_unlock(d);
252 			crit_exit();
253 			return EBUSY;
254 		}
255 		/* got a channel, already locked for us */
256 	}
257 
258 	if (flags & FWRITE) {
259 		/* open for write */
260 		wrch = pcm_chnalloc(d, PCMDIR_PLAY, p->p_pid, -1);
261 		if (!wrch) {
262 			/* no channel available */
263 			if (rdch && (flags & FREAD)) {
264 				/* just opened a read channel, release it */
265 				pcm_chnrelease(rdch);
266 			}
267 				/* exit */
268 			pcm_unlock(d);
269 			crit_exit();
270 			return EBUSY;
271 		}
272 		/* got a channel, already locked for us */
273 	}
274 
275 	i_dev->si_drv1 = rdch;
276 	i_dev->si_drv2 = wrch;
277 	pcm_unlock(d);
278 	/* finished with snddev, new channels still locked */
279 
280 	/* bump refcounts, reset and unlock any channels that we just opened */
281 	if (flags & FREAD) {
282 		if (chn_reset(rdch, fmt)) {
283 			pcm_lock(d);
284 			pcm_chnrelease(rdch);
285 			if (wrch && (flags & FWRITE))
286 				pcm_chnrelease(wrch);
287 			pcm_unlock(d);
288 			crit_exit();
289 			return ENODEV;
290 		}
291 		if (flags & O_NONBLOCK)
292 			rdch->flags |= CHN_F_NBIO;
293 		pcm_chnref(rdch, 1);
294 	 	CHN_UNLOCK(rdch);
295 	}
296 	if (flags & FWRITE) {
297 		if (chn_reset(wrch, fmt)) {
298 			pcm_lock(d);
299 			pcm_chnrelease(wrch);
300 			if (flags & FREAD) {
301 				CHN_LOCK(rdch);
302 				pcm_chnref(rdch, -1);
303 				pcm_chnrelease(rdch);
304 				CHN_UNLOCK(rdch);
305 			}
306 			pcm_unlock(d);
307 			crit_exit();
308 			return ENODEV;
309 		}
310 		if (flags & O_NONBLOCK)
311 			wrch->flags |= CHN_F_NBIO;
312 		pcm_chnref(wrch, 1);
313 	 	CHN_UNLOCK(wrch);
314 	}
315 	crit_exit();
316 	return 0;
317 }
318 
319 static int
320 dsp_close(dev_t i_dev, int flags, int mode, struct thread *td)
321 {
322 	struct pcm_channel *rdch, *wrch;
323 	struct snddev_info *d;
324 	int exit;
325 
326 	crit_enter();
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 		crit_exit();
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 	crit_exit();
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 	int ret;
388 
389 	crit_enter();
390 	getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD);
391 
392 	KASSERT(rdch, ("dsp_read: nonexistant channel"));
393 	KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel"));
394 
395 	if (rdch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
396 		relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
397 		crit_exit();
398 		return EINVAL;
399 	}
400 	if (!(rdch->flags & CHN_F_RUNNING))
401 		rdch->flags |= CHN_F_RUNNING;
402 	ret = chn_read(rdch, buf);
403 	relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
404 
405 	crit_exit();
406 	return ret;
407 }
408 
409 static int
410 dsp_write(dev_t i_dev, struct uio *buf, int flag)
411 {
412 	struct pcm_channel *rdch, *wrch;
413 	int ret;
414 
415 	crit_enter();
416 	getchns(i_dev, &rdch, &wrch, SD_F_PRIO_WR);
417 
418 	KASSERT(wrch, ("dsp_write: nonexistant channel"));
419 	KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel"));
420 
421 	if (wrch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
422 		relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
423 		crit_exit();
424 		return EINVAL;
425 	}
426 	if (!(wrch->flags & CHN_F_RUNNING))
427 		wrch->flags |= CHN_F_RUNNING;
428 	ret = chn_write(wrch, buf);
429 	relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
430 
431 	crit_exit();
432 	return ret;
433 }
434 
435 static int
436 dsp_ioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
437 {
438     	struct pcm_channel *wrch, *rdch;
439 	struct snddev_info *d;
440 	int kill;
441     	int ret = 0, *arg_i = (int *)arg, tmp;
442 
443 	/*
444 	 * this is an evil hack to allow broken apps to perform mixer ioctls
445 	 * on dsp devices.
446 	 */
447 
448 	if (IOCGROUP(cmd) == 'M') {
449 		dev_t pdev;
450 
451 		pdev = make_adhoc_dev(&dsp_cdevsw,
452 				PCMMKMINOR(PCMUNIT(i_dev), SND_DEV_CTL, 0));
453 		return mixer_ioctl(pdev, cmd, arg, mode, td);
454 	}
455 
456 	crit_enter();
457 	d = dsp_get_info(i_dev);
458 	getchns(i_dev, &rdch, &wrch, 0);
459 
460 	kill = 0;
461 	if (wrch && (wrch->flags & CHN_F_DEAD))
462 		kill |= 1;
463 	if (rdch && (rdch->flags & CHN_F_DEAD))
464 		kill |= 2;
465 	if (kill == 3) {
466 		relchns(i_dev, rdch, wrch, 0);
467 		crit_exit();
468 		return EINVAL;
469 	}
470 	if (kill & 1)
471 		wrch = NULL;
472 	if (kill & 2)
473 		rdch = NULL;
474 
475     	switch(cmd) {
476 #ifdef OLDPCM_IOCTL
477     	/*
478      	 * we start with the new ioctl interface.
479      	 */
480     	case AIONWRITE:	/* how many bytes can write ? */
481 /*
482 		if (wrch && wrch->bufhard.dl)
483 			while (chn_wrfeed(wrch) == 0);
484 */
485 		*arg_i = wrch? sndbuf_getfree(wrch->bufsoft) : 0;
486 		break;
487 
488     	case AIOSSIZE:     /* set the current blocksize */
489 		{
490 	    		struct snd_size *p = (struct snd_size *)arg;
491 
492 			p->play_size = 0;
493 			p->rec_size = 0;
494 	    		if (wrch) {
495 				CHN_LOCK(wrch);
496 				chn_setblocksize(wrch, 2, p->play_size);
497 				p->play_size = sndbuf_getblksz(wrch->bufsoft);
498 				CHN_UNLOCK(wrch);
499 			}
500 	    		if (rdch) {
501 				CHN_LOCK(rdch);
502 				chn_setblocksize(rdch, 2, p->rec_size);
503 				p->rec_size = sndbuf_getblksz(rdch->bufsoft);
504 				CHN_UNLOCK(rdch);
505 			}
506 		}
507 		break;
508     	case AIOGSIZE:	/* get the current blocksize */
509 		{
510 	    		struct snd_size *p = (struct snd_size *)arg;
511 
512 	    		if (wrch)
513 				p->play_size = sndbuf_getblksz(wrch->bufsoft);
514 	    		if (rdch)
515 				p->rec_size = sndbuf_getblksz(rdch->bufsoft);
516 		}
517 		break;
518 
519     	case AIOSFMT:
520 		{
521 	    		snd_chan_param *p = (snd_chan_param *)arg;
522 
523 	    		if (wrch) {
524 				CHN_LOCK(wrch);
525 				chn_setformat(wrch, p->play_format);
526 				chn_setspeed(wrch, p->play_rate);
527 				CHN_UNLOCK(wrch);
528 	    		}
529 	    		if (rdch) {
530 				CHN_LOCK(rdch);
531 				chn_setformat(rdch, p->rec_format);
532 				chn_setspeed(rdch, p->rec_rate);
533 				CHN_UNLOCK(rdch);
534 	    		}
535 		}
536 		/* FALLTHROUGH */
537 
538     	case AIOGFMT:
539 		{
540 	    		snd_chan_param *p = (snd_chan_param *)arg;
541 
542 	    		p->play_rate = wrch? wrch->speed : 0;
543 	    		p->rec_rate = rdch? rdch->speed : 0;
544 	    		p->play_format = wrch? wrch->format : 0;
545 	    		p->rec_format = rdch? rdch->format : 0;
546 		}
547 		break;
548 
549     	case AIOGCAP:     /* get capabilities */
550 		{
551 	    		snd_capabilities *p = (snd_capabilities *)arg;
552 			struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
553 			dev_t pdev;
554 
555 			if (rdch) {
556 				CHN_LOCK(rdch);
557 				rcaps = chn_getcaps(rdch);
558 			}
559 			if (wrch) {
560 				CHN_LOCK(wrch);
561 				pcaps = chn_getcaps(wrch);
562 			}
563 	    		p->rate_min = max(rcaps? rcaps->minspeed : 0,
564 	                      		  pcaps? pcaps->minspeed : 0);
565 	    		p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
566 	                      		  pcaps? pcaps->maxspeed : 1000000);
567 	    		p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
568 	                     		 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
569 			/* XXX bad on sb16 */
570 	    		p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
571 			 	     (wrch? chn_getformats(wrch) : 0xffffffff);
572 			if (rdch && wrch)
573 				p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
574 			pdev = make_adhoc_dev(&dsp_cdevsw, PCMMKMINOR(PCMUNIT(i_dev), SND_DEV_CTL, 0));
575 	    		p->mixers = 1; /* default: one mixer */
576 	    		p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
577 	    		p->left = p->right = 100;
578 			if (wrch)
579 				CHN_UNLOCK(wrch);
580 			if (rdch)
581 				CHN_UNLOCK(rdch);
582 		}
583 		break;
584 
585     	case AIOSTOP:
586 		if (*arg_i == AIOSYNC_PLAY && wrch)
587 			*arg_i = chn_abort(wrch);
588 		else if (*arg_i == AIOSYNC_CAPTURE && rdch)
589 			*arg_i = chn_abort(rdch);
590 		else {
591 	   	 	printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
592 	    		*arg_i = 0;
593 		}
594 		break;
595 
596     	case AIOSYNC:
597 		printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
598 	    		((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
599 		break;
600 #endif
601 	/*
602 	 * here follow the standard ioctls (filio.h etc.)
603 	 */
604     	case FIONREAD: /* get # bytes to read */
605 /*		if (rdch && rdch->bufhard.dl)
606 			while (chn_rdfeed(rdch) == 0);
607 */		*arg_i = rdch? sndbuf_getready(rdch->bufsoft) : 0;
608 		break;
609 
610     	case FIOASYNC: /*set/clear async i/o */
611 		DEB( printf("FIOASYNC\n") ; )
612 		break;
613 
614     	case SNDCTL_DSP_NONBLOCK:
615     	case FIONBIO: /* set/clear non-blocking i/o */
616 		if (rdch)
617 			rdch->flags &= ~CHN_F_NBIO;
618 		if (wrch)
619 			wrch->flags &= ~CHN_F_NBIO;
620 		if (*arg_i) {
621 		    	if (rdch)
622 				rdch->flags |= CHN_F_NBIO;
623 		    	if (wrch)
624 				wrch->flags |= CHN_F_NBIO;
625 		}
626 		break;
627 
628     	/*
629 	 * Finally, here is the linux-compatible ioctl interface
630 	 */
631 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
632     	case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
633     	case SNDCTL_DSP_GETBLKSIZE:
634 		if (wrch)
635 			*arg_i = sndbuf_getblksz(wrch->bufsoft);
636 		else if (rdch)
637 			*arg_i = sndbuf_getblksz(rdch->bufsoft);
638 		else
639 			*arg_i = 0;
640 		break ;
641 
642     	case SNDCTL_DSP_SETBLKSIZE:
643 		RANGE(*arg_i, 16, 65536);
644 		if (wrch) {
645 			CHN_LOCK(wrch);
646 			chn_setblocksize(wrch, 2, *arg_i);
647 			CHN_UNLOCK(wrch);
648 		}
649 		if (rdch) {
650 			CHN_LOCK(rdch);
651 			chn_setblocksize(rdch, 2, *arg_i);
652 			CHN_UNLOCK(rdch);
653 		}
654 		break;
655 
656     	case SNDCTL_DSP_RESET:
657 		DEB(printf("dsp reset\n"));
658 		if (wrch)
659 			chn_abort(wrch);
660 		if (rdch)
661 			chn_abort(rdch);
662 		break;
663 
664     	case SNDCTL_DSP_SYNC:
665 		DEB(printf("dsp sync\n"));
666 		/* chn_sync may sleep */
667 		if (wrch) {
668 			CHN_LOCK(wrch);
669 			chn_sync(wrch, sndbuf_getsize(wrch->bufsoft) - 4);
670 			CHN_UNLOCK(wrch);
671 		}
672 		break;
673 
674     	case SNDCTL_DSP_SPEED:
675 		/* chn_setspeed may sleep */
676 		tmp = 0;
677 		if (wrch) {
678 			CHN_LOCK(wrch);
679 			ret = chn_setspeed(wrch, *arg_i);
680 			tmp = wrch->speed;
681 			CHN_UNLOCK(wrch);
682 		}
683 		if (rdch && ret == 0) {
684 			CHN_LOCK(rdch);
685 			ret = chn_setspeed(rdch, *arg_i);
686 			if (tmp == 0)
687 				tmp = rdch->speed;
688 			CHN_UNLOCK(rdch);
689 		}
690 		*arg_i = tmp;
691 		break;
692 
693     	case SOUND_PCM_READ_RATE:
694 		*arg_i = wrch? wrch->speed : rdch->speed;
695 		break;
696 
697     	case SNDCTL_DSP_STEREO:
698 		tmp = -1;
699 		*arg_i = (*arg_i)? AFMT_STEREO : 0;
700 		if (wrch) {
701 			CHN_LOCK(wrch);
702 			ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
703 			tmp = (wrch->format & AFMT_STEREO)? 1 : 0;
704 			CHN_UNLOCK(wrch);
705 		}
706 		if (rdch && ret == 0) {
707 			CHN_LOCK(rdch);
708 			ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
709 			if (tmp == -1)
710 				tmp = (rdch->format & AFMT_STEREO)? 1 : 0;
711 			CHN_UNLOCK(rdch);
712 		}
713 		*arg_i = tmp;
714 		break;
715 
716     	case SOUND_PCM_WRITE_CHANNELS:
717 /*	case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
718 		if (*arg_i != 0) {
719 			tmp = 0;
720 			*arg_i = (*arg_i != 1)? AFMT_STEREO : 0;
721 	  		if (wrch) {
722 				CHN_LOCK(wrch);
723 				ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
724 				tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
725 				CHN_UNLOCK(wrch);
726 			}
727 			if (rdch && ret == 0) {
728 				CHN_LOCK(rdch);
729 				ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
730 				if (tmp == 0)
731 					tmp = (rdch->format & AFMT_STEREO)? 2 : 1;
732 				CHN_UNLOCK(rdch);
733 			}
734 			*arg_i = tmp;
735 		} else {
736 			*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 2 : 1;
737 		}
738 		break;
739 
740     	case SOUND_PCM_READ_CHANNELS:
741 		*arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 2 : 1;
742 		break;
743 
744     	case SNDCTL_DSP_GETFMTS:	/* returns a mask of supported fmts */
745 		*arg_i = wrch? chn_getformats(wrch) : chn_getformats(rdch);
746 		break ;
747 
748     	case SNDCTL_DSP_SETFMT:	/* sets _one_ format */
749 		/* XXX locking */
750 		if ((*arg_i != AFMT_QUERY)) {
751 			tmp = 0;
752 			if (wrch) {
753 				CHN_LOCK(wrch);
754 				ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
755 				tmp = wrch->format & ~AFMT_STEREO;
756 				CHN_UNLOCK(wrch);
757 			}
758 			if (rdch && ret == 0) {
759 				CHN_LOCK(rdch);
760 				ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
761 				if (tmp == 0)
762 					tmp = rdch->format & ~AFMT_STEREO;
763 				CHN_UNLOCK(rdch);
764 			}
765 			*arg_i = tmp;
766 		} else
767 			*arg_i = (wrch? wrch->format : rdch->format) & ~AFMT_STEREO;
768 		break;
769 
770     	case SNDCTL_DSP_SETFRAGMENT:
771 		/* XXX locking */
772 		DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
773 		{
774 			u_int32_t fragln = (*arg_i) & 0x0000ffff;
775 			u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
776 			u_int32_t fragsz;
777 
778 			RANGE(fragln, 4, 16);
779 			fragsz = 1 << fragln;
780 
781 			if (maxfrags == 0)
782 				maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
783 			if (maxfrags < 2) {
784 				ret = EINVAL;
785 				break;
786 			}
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 	crit_exit();
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 	int ret, e;
971 
972 	crit_enter();
973 	ret = 0;
974 	getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
975 
976 	if (wrch) {
977 		e = (events & (POLLOUT | POLLWRNORM));
978 		if (e)
979 			ret |= chn_poll(wrch, e, td->td_proc);
980 	}
981 	if (rdch) {
982 		e = (events & (POLLIN | POLLRDNORM));
983 		if (e)
984 			ret |= chn_poll(rdch, e, td->td_proc);
985 	}
986 	relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
987 
988 	crit_exit();
989 	return ret;
990 }
991 
992 static int
993 dsp_mmap(dev_t i_dev, vm_offset_t offset, int nprot)
994 {
995 	struct pcm_channel *wrch = NULL, *rdch = NULL, *c;
996 	int ret;
997 
998 	if (nprot & PROT_EXEC)
999 		return -1;
1000 
1001 	crit_enter();
1002 	getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1003 #if 0
1004 	/*
1005 	 * XXX the linux api uses the nprot to select read/write buffer
1006 	 * our vm system doesn't allow this, so force write buffer
1007 	 */
1008 
1009 	if (wrch && (nprot & PROT_WRITE)) {
1010 		c = wrch;
1011 	} else if (rdch && (nprot & PROT_READ)) {
1012 		c = rdch;
1013 	} else {
1014 		crit_exit();
1015 		return -1;
1016 	}
1017 #else
1018 	c = wrch;
1019 #endif
1020 
1021 	if (c == NULL) {
1022 		relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1023 		crit_exit();
1024 		return -1;
1025 	}
1026 
1027 	if (offset >= sndbuf_getsize(c->bufsoft)) {
1028 		relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1029 		crit_exit();
1030 		return -1;
1031 	}
1032 
1033 	if (!(c->flags & CHN_F_MAPPED))
1034 		c->flags |= CHN_F_MAPPED;
1035 
1036 	ret = atop(vtophys(sndbuf_getbufofs(c->bufsoft, offset)));
1037 	relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1038 
1039 	crit_exit();
1040 	return ret;
1041 }
1042 
1043 int
1044 dsp_register(int unit, int channel)
1045 {
1046 	cdevsw_add(&dsp_cdevsw,
1047 		PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_DSP, 0));
1048 	cdevsw_add(&dsp_cdevsw,
1049 		PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_DSP16, 0));
1050 	cdevsw_add(&dsp_cdevsw,
1051 		PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_AUDIO, 0));
1052 	cdevsw_add(&dsp_cdevsw,
1053 		PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_NORESET, 0));
1054 	make_dev(&dsp_cdevsw, PCMMKMINOR(unit, SND_DEV_DSP, channel),
1055 		 UID_ROOT, GID_WHEEL, 0666, "dsp%d.%d", unit, channel);
1056 	make_dev(&dsp_cdevsw, PCMMKMINOR(unit, SND_DEV_DSP16, channel),
1057 		 UID_ROOT, GID_WHEEL, 0666, "dspW%d.%d", unit, channel);
1058 	make_dev(&dsp_cdevsw, PCMMKMINOR(unit, SND_DEV_AUDIO, channel),
1059 		 UID_ROOT, GID_WHEEL, 0666, "audio%d.%d", unit, channel);
1060 
1061 	return 0;
1062 }
1063 
1064 int
1065 dsp_registerrec(int unit, int channel)
1066 {
1067 	cdevsw_add(&dsp_cdevsw,
1068 		PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_DSPREC, 0));
1069 	make_dev(&dsp_cdevsw, PCMMKMINOR(unit, SND_DEV_DSPREC, channel),
1070 		 UID_ROOT, GID_WHEEL, 0666, "dspr%d.%d", unit, channel);
1071 
1072 	return 0;
1073 }
1074 
1075 int
1076 dsp_unregister(int unit, int channel)
1077 {
1078 	cdevsw_remove(&dsp_cdevsw,
1079 		PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_DSP, 0));
1080 	cdevsw_remove(&dsp_cdevsw,
1081 		PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_DSP16, 0));
1082 	cdevsw_remove(&dsp_cdevsw,
1083 		PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_AUDIO, 0));
1084 	cdevsw_remove(&dsp_cdevsw,
1085 		PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_NORESET, 0));
1086 	return 0;
1087 }
1088 
1089 int
1090 dsp_unregisterrec(int unit, int channel)
1091 {
1092 	cdevsw_remove(&dsp_cdevsw,
1093 		PCMMKMINOR(-1, -1, 0), PCMMKMINOR(unit, SND_DEV_DSPREC, 0));
1094 	return 0;
1095 }
1096 
1097 #ifdef USING_DEVFS
1098 static void
1099 dsp_clone(void *arg, char *name, int namelen, dev_t *dev)
1100 {
1101 	dev_t pdev;
1102 	int i, cont, unit, devtype;
1103 	int devtypes[3] = {SND_DEV_DSP, SND_DEV_DSP16, SND_DEV_AUDIO};
1104 	char *devnames[3] = {"dsp", "dspW", "audio"};
1105 
1106 	if (*dev != NODEV)
1107 		return;
1108 	if (pcm_devclass == NULL)
1109 		return;
1110 
1111 	devtype = 0;
1112 	unit = -1;
1113 	for (i = 0; (i < 3) && (unit == -1); i++) {
1114 		devtype = devtypes[i];
1115 		if (strcmp(name, devnames[i]) == 0) {
1116 			unit = snd_unit;
1117 		} else {
1118 			if (dev_stdclone(name, NULL, devnames[i], &unit) != 1)
1119 				unit = -1;
1120 		}
1121 	}
1122 	if (unit == -1 || unit >= devclass_get_maxunit(pcm_devclass))
1123 		return;
1124 
1125 	cont = 1;
1126 	for (i = 0; cont; i++) {
1127 		pdev = make_adhoc_dev(&dsp_cdevsw, PCMMKMINOR(unit, devtype, i));
1128 		if (pdev->si_flags & SI_NAMED) {
1129 			if ((pdev->si_drv1 == NULL) && (pdev->si_drv2 == NULL)) {
1130 				*dev = pdev;
1131 				return;
1132 			}
1133 		} else {
1134 			cont = 0;
1135 		}
1136 	}
1137 }
1138 
1139 static void
1140 dsp_sysinit(void *p)
1141 {
1142 	dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
1143 }
1144 
1145 static void
1146 dsp_sysuninit(void *p)
1147 {
1148 	if (dsp_ehtag != NULL)
1149 		EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
1150 }
1151 
1152 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
1153 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
1154 #endif
1155 
1156 
1157