dsp.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5 * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
6 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#ifdef HAVE_KERNEL_OPTION_HEADERS
32#include "opt_snd.h"
33#endif
34
35#include <dev/sound/pcm/sound.h>
36#include <sys/ctype.h>
37#include <sys/lock.h>
38#include <sys/rwlock.h>
39#include <sys/sysent.h>
40
41#include <vm/vm.h>
42#include <vm/vm_object.h>
43#include <vm/vm_page.h>
44#include <vm/vm_pager.h>
45
46SND_DECLARE_FILE("$FreeBSD: stable/11/sys/dev/sound/pcm/dsp.c 330897 2018-03-14 03:19:51Z eadler $");
47
48static int dsp_mmap_allow_prot_exec = 0;
49SYSCTL_INT(_hw_snd, OID_AUTO, compat_linux_mmap, CTLFLAG_RWTUN,
50    &dsp_mmap_allow_prot_exec, 0,
51    "linux mmap compatibility (-1=force disable 0=auto 1=force enable)");
52
53static int dsp_basename_clone = 1;
54SYSCTL_INT(_hw_snd, OID_AUTO, basename_clone, CTLFLAG_RWTUN,
55    &dsp_basename_clone, 0,
56    "DSP basename cloning (0: Disable; 1: Enabled)");
57
58struct dsp_cdevinfo {
59	struct pcm_channel *rdch, *wrch;
60	struct pcm_channel *volch;
61	int busy, simplex;
62	TAILQ_ENTRY(dsp_cdevinfo) link;
63};
64
65#define PCM_RDCH(x)		(((struct dsp_cdevinfo *)(x)->si_drv1)->rdch)
66#define PCM_WRCH(x)		(((struct dsp_cdevinfo *)(x)->si_drv1)->wrch)
67#define PCM_VOLCH(x)		(((struct dsp_cdevinfo *)(x)->si_drv1)->volch)
68#define PCM_SIMPLEX(x)		(((struct dsp_cdevinfo *)(x)->si_drv1)->simplex)
69
70#define DSP_CDEVINFO_CACHESIZE	8
71
72#define DSP_REGISTERED(x, y)	(PCM_REGISTERED(x) &&			\
73				 (y) != NULL && (y)->si_drv1 != NULL)
74
75#define OLDPCM_IOCTL
76
77static d_open_t dsp_open;
78static d_close_t dsp_close;
79static d_read_t dsp_read;
80static d_write_t dsp_write;
81static d_ioctl_t dsp_ioctl;
82static d_poll_t dsp_poll;
83static d_mmap_t dsp_mmap;
84static d_mmap_single_t dsp_mmap_single;
85
86struct cdevsw dsp_cdevsw = {
87	.d_version =	D_VERSION,
88	.d_open =	dsp_open,
89	.d_close =	dsp_close,
90	.d_read =	dsp_read,
91	.d_write =	dsp_write,
92	.d_ioctl =	dsp_ioctl,
93	.d_poll =	dsp_poll,
94	.d_mmap =	dsp_mmap,
95	.d_mmap_single = dsp_mmap_single,
96	.d_name =	"dsp",
97};
98
99static eventhandler_tag dsp_ehtag = NULL;
100static int dsp_umax = -1;
101static int dsp_cmax = -1;
102
103static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group);
104static int dsp_oss_syncstart(int sg_id);
105static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy);
106static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled);
107static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
108static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
109static int dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch, int *mask);
110#ifdef OSSV4_EXPERIMENT
111static int dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
112static int dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
113static int dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
114static int dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
115static int dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name);
116#endif
117
118static struct snddev_info *
119dsp_get_info(struct cdev *dev)
120{
121	return (devclass_get_softc(pcm_devclass, PCMUNIT(dev)));
122}
123
124static uint32_t
125dsp_get_flags(struct cdev *dev)
126{
127	device_t bdev;
128
129	bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
130
131	return ((bdev != NULL) ? pcm_getflags(bdev) : 0xffffffff);
132}
133
134static void
135dsp_set_flags(struct cdev *dev, uint32_t flags)
136{
137	device_t bdev;
138
139	bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
140
141	if (bdev != NULL)
142		pcm_setflags(bdev, flags);
143}
144
145/*
146 * return the channels associated with an open device instance.
147 * lock channels specified.
148 */
149static int
150getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch,
151    uint32_t prio)
152{
153	struct snddev_info *d;
154	struct pcm_channel *ch;
155	uint32_t flags;
156
157	if (PCM_SIMPLEX(dev) != 0) {
158		d = dsp_get_info(dev);
159		if (!PCM_REGISTERED(d))
160			return (ENXIO);
161		PCM_LOCK(d);
162		PCM_WAIT(d);
163		PCM_ACQUIRE(d);
164		/*
165		 * Note: order is important -
166		 *       pcm flags -> prio query flags -> wild guess
167		 */
168		ch = NULL;
169		flags = dsp_get_flags(dev);
170		if (flags & SD_F_PRIO_WR) {
171			ch = PCM_RDCH(dev);
172			PCM_RDCH(dev) = NULL;
173		} else if (flags & SD_F_PRIO_RD) {
174			ch = PCM_WRCH(dev);
175			PCM_WRCH(dev) = NULL;
176		} else if (prio & SD_F_PRIO_WR) {
177			ch = PCM_RDCH(dev);
178			PCM_RDCH(dev) = NULL;
179			flags |= SD_F_PRIO_WR;
180		} else if (prio & SD_F_PRIO_RD) {
181			ch = PCM_WRCH(dev);
182			PCM_WRCH(dev) = NULL;
183			flags |= SD_F_PRIO_RD;
184		} else if (PCM_WRCH(dev) != NULL) {
185			ch = PCM_RDCH(dev);
186			PCM_RDCH(dev) = NULL;
187			flags |= SD_F_PRIO_WR;
188		} else if (PCM_RDCH(dev) != NULL) {
189			ch = PCM_WRCH(dev);
190			PCM_WRCH(dev) = NULL;
191			flags |= SD_F_PRIO_RD;
192		}
193		PCM_SIMPLEX(dev) = 0;
194		dsp_set_flags(dev, flags);
195		if (ch != NULL) {
196			CHN_LOCK(ch);
197			pcm_chnref(ch, -1);
198			pcm_chnrelease(ch);
199		}
200		PCM_RELEASE(d);
201		PCM_UNLOCK(d);
202	}
203
204	*rdch = PCM_RDCH(dev);
205	*wrch = PCM_WRCH(dev);
206
207	if (*rdch != NULL && (prio & SD_F_PRIO_RD))
208		CHN_LOCK(*rdch);
209	if (*wrch != NULL && (prio & SD_F_PRIO_WR))
210		CHN_LOCK(*wrch);
211
212	return (0);
213}
214
215/* unlock specified channels */
216static void
217relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch,
218    uint32_t prio)
219{
220	if (wrch != NULL && (prio & SD_F_PRIO_WR))
221		CHN_UNLOCK(wrch);
222	if (rdch != NULL && (prio & SD_F_PRIO_RD))
223		CHN_UNLOCK(rdch);
224}
225
226static void
227dsp_cdevinfo_alloc(struct cdev *dev,
228    struct pcm_channel *rdch, struct pcm_channel *wrch,
229    struct pcm_channel *volch)
230{
231	struct snddev_info *d;
232	struct dsp_cdevinfo *cdi;
233	int simplex;
234
235	d = dsp_get_info(dev);
236
237	KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 == NULL &&
238	    ((rdch == NULL && wrch == NULL) || rdch != wrch),
239	    ("bogus %s(), what are you trying to accomplish here?", __func__));
240	PCM_BUSYASSERT(d);
241	PCM_LOCKASSERT(d);
242
243	simplex = (dsp_get_flags(dev) & SD_F_SIMPLEX) ? 1 : 0;
244
245	/*
246	 * Scan for free instance entry and put it into the end of list.
247	 * Create new one if necessary.
248	 */
249	TAILQ_FOREACH(cdi, &d->dsp_cdevinfo_pool, link) {
250		if (cdi->busy != 0)
251			break;
252		cdi->rdch = rdch;
253		cdi->wrch = wrch;
254		cdi->volch = volch;
255		cdi->simplex = simplex;
256		cdi->busy = 1;
257		TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
258		TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
259		dev->si_drv1 = cdi;
260		return;
261	}
262	PCM_UNLOCK(d);
263	cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
264	PCM_LOCK(d);
265	cdi->rdch = rdch;
266	cdi->wrch = wrch;
267	cdi->volch = volch;
268	cdi->simplex = simplex;
269	cdi->busy = 1;
270	TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
271	dev->si_drv1 = cdi;
272}
273
274static void
275dsp_cdevinfo_free(struct cdev *dev)
276{
277	struct snddev_info *d;
278	struct dsp_cdevinfo *cdi, *tmp;
279	uint32_t flags;
280	int i;
281
282	d = dsp_get_info(dev);
283
284	KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 != NULL &&
285	    PCM_RDCH(dev) == NULL && PCM_WRCH(dev) == NULL &&
286	    PCM_VOLCH(dev) == NULL,
287	    ("bogus %s(), what are you trying to accomplish here?", __func__));
288	PCM_BUSYASSERT(d);
289	PCM_LOCKASSERT(d);
290
291	cdi = dev->si_drv1;
292	dev->si_drv1 = NULL;
293	cdi->rdch = NULL;
294	cdi->wrch = NULL;
295	cdi->volch = NULL;
296	cdi->simplex = 0;
297	cdi->busy = 0;
298
299	/*
300	 * Once it is free, move it back to the beginning of list for
301	 * faster new entry allocation.
302	 */
303	TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
304	TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
305
306	/*
307	 * Scan the list, cache free entries up to DSP_CDEVINFO_CACHESIZE.
308	 * Reset simplex flags.
309	 */
310	flags = dsp_get_flags(dev) & ~SD_F_PRIO_SET;
311	i = DSP_CDEVINFO_CACHESIZE;
312	TAILQ_FOREACH_SAFE(cdi, &d->dsp_cdevinfo_pool, link, tmp) {
313		if (cdi->busy != 0) {
314			if (cdi->simplex == 0) {
315				if (cdi->rdch != NULL)
316					flags |= SD_F_PRIO_RD;
317				if (cdi->wrch != NULL)
318					flags |= SD_F_PRIO_WR;
319			}
320		} else {
321			if (i == 0) {
322				TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
323				free(cdi, M_DEVBUF);
324			} else
325				i--;
326		}
327	}
328	dsp_set_flags(dev, flags);
329}
330
331void
332dsp_cdevinfo_init(struct snddev_info *d)
333{
334	struct dsp_cdevinfo *cdi;
335	int i;
336
337	KASSERT(d != NULL, ("NULL snddev_info"));
338	PCM_BUSYASSERT(d);
339	PCM_UNLOCKASSERT(d);
340
341	TAILQ_INIT(&d->dsp_cdevinfo_pool);
342	for (i = 0; i < DSP_CDEVINFO_CACHESIZE; i++) {
343		cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
344		TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
345	}
346}
347
348void
349dsp_cdevinfo_flush(struct snddev_info *d)
350{
351	struct dsp_cdevinfo *cdi, *tmp;
352
353	KASSERT(d != NULL, ("NULL snddev_info"));
354	PCM_BUSYASSERT(d);
355	PCM_UNLOCKASSERT(d);
356
357	cdi = TAILQ_FIRST(&d->dsp_cdevinfo_pool);
358	while (cdi != NULL) {
359		tmp = TAILQ_NEXT(cdi, link);
360		free(cdi, M_DEVBUF);
361		cdi = tmp;
362	}
363	TAILQ_INIT(&d->dsp_cdevinfo_pool);
364}
365
366/* duplex / simplex cdev type */
367enum {
368	DSP_CDEV_TYPE_RDONLY,		/* simplex read-only (record)   */
369	DSP_CDEV_TYPE_WRONLY,		/* simplex write-only (play)    */
370	DSP_CDEV_TYPE_RDWR		/* duplex read, write, or both  */
371};
372
373enum {
374	DSP_CDEV_VOLCTL_NONE,
375	DSP_CDEV_VOLCTL_READ,
376	DSP_CDEV_VOLCTL_WRITE
377};
378
379#define DSP_F_VALID(x)		((x) & (FREAD | FWRITE))
380#define DSP_F_DUPLEX(x)		(((x) & (FREAD | FWRITE)) == (FREAD | FWRITE))
381#define DSP_F_SIMPLEX(x)	(!DSP_F_DUPLEX(x))
382#define DSP_F_READ(x)		((x) & FREAD)
383#define DSP_F_WRITE(x)		((x) & FWRITE)
384
385static const struct {
386	int type;
387	char *name;
388	char *sep;
389	char *alias;
390	int use_sep;
391	int hw;
392	int max;
393	int volctl;
394	uint32_t fmt, spd;
395	int query;
396} dsp_cdevs[] = {
397	{ SND_DEV_DSP,         "dsp",    ".", NULL, 0, 0, 0, 0,
398	  SND_FORMAT(AFMT_U8, 1, 0),     DSP_DEFAULT_SPEED,
399	  DSP_CDEV_TYPE_RDWR },
400	{ SND_DEV_AUDIO,       "audio",  ".", NULL, 0, 0, 0, 0,
401	  SND_FORMAT(AFMT_MU_LAW, 1, 0), DSP_DEFAULT_SPEED,
402	  DSP_CDEV_TYPE_RDWR },
403	{ SND_DEV_DSP16,       "dspW",   ".", NULL, 0, 0, 0, 0,
404	  SND_FORMAT(AFMT_S16_LE, 1, 0), DSP_DEFAULT_SPEED,
405	  DSP_CDEV_TYPE_RDWR },
406	{ SND_DEV_DSPHW_PLAY,  "dsp",   ".p", NULL, 1, 1, SND_MAXHWCHAN, 1,
407	  SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
408	{ SND_DEV_DSPHW_VPLAY, "dsp",  ".vp", NULL, 1, 1, SND_MAXVCHANS, 1,
409	  SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
410	{ SND_DEV_DSPHW_REC,   "dsp",   ".r", NULL, 1, 1, SND_MAXHWCHAN, 1,
411	  SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
412	{ SND_DEV_DSPHW_VREC,  "dsp",  ".vr", NULL, 1, 1, SND_MAXVCHANS, 1,
413	  SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
414	{ SND_DEV_DSPHW_CD,    "dspcd",  ".", NULL, 0, 0, 0, 0,
415	  SND_FORMAT(AFMT_S16_LE, 2, 0), 44100, DSP_CDEV_TYPE_RDWR   },
416	/* Low priority, OSSv4 aliases. */
417	{ SND_DEV_DSP,      "dsp_ac3",   ".", "dsp", 0, 0, 0, 0,
418	  SND_FORMAT(AFMT_U8, 1, 0),     DSP_DEFAULT_SPEED,
419	  DSP_CDEV_TYPE_RDWR },
420	{ SND_DEV_DSP,     "dsp_mmap",   ".", "dsp", 0, 0, 0, 0,
421	  SND_FORMAT(AFMT_U8, 1, 0),     DSP_DEFAULT_SPEED,
422	  DSP_CDEV_TYPE_RDWR },
423	{ SND_DEV_DSP,  "dsp_multich",   ".", "dsp", 0, 0, 0, 0,
424	  SND_FORMAT(AFMT_U8, 1, 0),     DSP_DEFAULT_SPEED,
425	  DSP_CDEV_TYPE_RDWR },
426	{ SND_DEV_DSP, "dsp_spdifout",   ".", "dsp", 0, 0, 0, 0,
427	  SND_FORMAT(AFMT_U8, 1, 0),     DSP_DEFAULT_SPEED,
428	  DSP_CDEV_TYPE_RDWR },
429	{ SND_DEV_DSP,  "dsp_spdifin",   ".", "dsp", 0, 0, 0, 0,
430	  SND_FORMAT(AFMT_U8, 1, 0),     DSP_DEFAULT_SPEED,
431	  DSP_CDEV_TYPE_RDWR },
432};
433
434#define DSP_FIXUP_ERROR()		do {				\
435	prio = dsp_get_flags(i_dev);					\
436	if (!DSP_F_VALID(flags))					\
437		error = EINVAL;						\
438	if (!DSP_F_DUPLEX(flags) &&					\
439	    ((DSP_F_READ(flags) && d->reccount == 0) ||			\
440	    (DSP_F_WRITE(flags) && d->playcount == 0)))			\
441		error = ENOTSUP;					\
442	else if (!DSP_F_DUPLEX(flags) && (prio & SD_F_SIMPLEX) &&	\
443	    ((DSP_F_READ(flags) && (prio & SD_F_PRIO_WR)) ||		\
444	    (DSP_F_WRITE(flags) && (prio & SD_F_PRIO_RD))))		\
445		error = EBUSY;						\
446	else if (DSP_REGISTERED(d, i_dev))				\
447		error = EBUSY;						\
448} while (0)
449
450static int
451dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
452{
453	struct pcm_channel *rdch, *wrch;
454	struct snddev_info *d;
455	uint32_t fmt, spd, prio, volctl;
456	int i, error, rderror, wrerror, devtype, wdevunit, rdevunit;
457
458	/* Kind of impossible.. */
459	if (i_dev == NULL || td == NULL)
460		return (ENODEV);
461
462	d = dsp_get_info(i_dev);
463	if (!PCM_REGISTERED(d))
464		return (EBADF);
465
466	PCM_GIANT_ENTER(d);
467
468	/* Lock snddev so nobody else can monkey with it. */
469	PCM_LOCK(d);
470	PCM_WAIT(d);
471
472	/*
473	 * Try to acquire cloned device before someone else pick it.
474	 * ENODEV means this is not a cloned droids.
475	 */
476	error = snd_clone_acquire(i_dev);
477	if (!(error == 0 || error == ENODEV)) {
478		DSP_FIXUP_ERROR();
479		PCM_UNLOCK(d);
480		PCM_GIANT_EXIT(d);
481		return (error);
482	}
483
484	error = 0;
485	DSP_FIXUP_ERROR();
486
487	if (error != 0) {
488		(void)snd_clone_release(i_dev);
489		PCM_UNLOCK(d);
490		PCM_GIANT_EXIT(d);
491		return (error);
492	}
493
494	/*
495	 * That is just enough. Acquire and unlock pcm lock so
496	 * the other will just have to wait until we finish doing
497	 * everything.
498	 */
499	PCM_ACQUIRE(d);
500	PCM_UNLOCK(d);
501
502	devtype = PCMDEV(i_dev);
503	wdevunit = -1;
504	rdevunit = -1;
505	fmt = 0;
506	spd = 0;
507	volctl = DSP_CDEV_VOLCTL_NONE;
508
509	for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
510		if (devtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
511			continue;
512		/*
513		 * Volume control only valid for DSPHW devices,
514		 * and it must be opened in opposite direction be it
515		 * simplex or duplex. Anything else will be handled
516		 * as usual.
517		 */
518		if (dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY) {
519			if (dsp_cdevs[i].volctl != 0 &&
520			    DSP_F_READ(flags)) {
521				volctl = DSP_CDEV_VOLCTL_WRITE;
522				flags &= ~FREAD;
523				flags |= FWRITE;
524			}
525			if (DSP_F_READ(flags)) {
526				(void)snd_clone_release(i_dev);
527				PCM_RELEASE_QUICK(d);
528				PCM_GIANT_EXIT(d);
529				return (ENOTSUP);
530			}
531			wdevunit = dev2unit(i_dev);
532		} else if (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY) {
533			if (dsp_cdevs[i].volctl != 0 &&
534			    DSP_F_WRITE(flags)) {
535				volctl = DSP_CDEV_VOLCTL_READ;
536				flags &= ~FWRITE;
537				flags |= FREAD;
538			}
539			if (DSP_F_WRITE(flags)) {
540				(void)snd_clone_release(i_dev);
541				PCM_RELEASE_QUICK(d);
542				PCM_GIANT_EXIT(d);
543				return (ENOTSUP);
544			}
545			rdevunit = dev2unit(i_dev);
546		}
547		fmt = dsp_cdevs[i].fmt;
548		spd = dsp_cdevs[i].spd;
549		break;
550	}
551
552	/* No matching devtype? */
553	if (fmt == 0 || spd == 0)
554		panic("impossible devtype %d", devtype);
555
556	rdch = NULL;
557	wrch = NULL;
558	rderror = 0;
559	wrerror = 0;
560
561	/*
562	 * if we get here, the open request is valid- either:
563	 *   * we were previously not open
564	 *   * we were open for play xor record and the opener wants
565	 *     the non-open direction
566	 */
567	if (DSP_F_READ(flags)) {
568		/* open for read */
569		rderror = pcm_chnalloc(d, &rdch, PCMDIR_REC,
570		    td->td_proc->p_pid, td->td_proc->p_comm, rdevunit);
571
572		if (rderror == 0 && chn_reset(rdch, fmt, spd) != 0)
573			rderror = ENXIO;
574
575		if (volctl == DSP_CDEV_VOLCTL_READ)
576			rderror = 0;
577
578		if (rderror != 0) {
579			if (rdch != NULL)
580				pcm_chnrelease(rdch);
581			if (!DSP_F_DUPLEX(flags)) {
582				(void)snd_clone_release(i_dev);
583				PCM_RELEASE_QUICK(d);
584				PCM_GIANT_EXIT(d);
585				return (rderror);
586			}
587			rdch = NULL;
588		} else if (volctl == DSP_CDEV_VOLCTL_READ) {
589			if (rdch != NULL) {
590				pcm_chnref(rdch, 1);
591				pcm_chnrelease(rdch);
592			}
593		} else {
594			if (flags & O_NONBLOCK)
595				rdch->flags |= CHN_F_NBIO;
596			if (flags & O_EXCL)
597				rdch->flags |= CHN_F_EXCLUSIVE;
598			pcm_chnref(rdch, 1);
599			if (volctl == DSP_CDEV_VOLCTL_NONE)
600				chn_vpc_reset(rdch, SND_VOL_C_PCM, 0);
601		 	CHN_UNLOCK(rdch);
602		}
603	}
604
605	if (DSP_F_WRITE(flags)) {
606		/* open for write */
607		wrerror = pcm_chnalloc(d, &wrch, PCMDIR_PLAY,
608		    td->td_proc->p_pid, td->td_proc->p_comm, wdevunit);
609
610		if (wrerror == 0 && chn_reset(wrch, fmt, spd) != 0)
611			wrerror = ENXIO;
612
613		if (volctl == DSP_CDEV_VOLCTL_WRITE)
614			wrerror = 0;
615
616		if (wrerror != 0) {
617			if (wrch != NULL)
618				pcm_chnrelease(wrch);
619			if (!DSP_F_DUPLEX(flags)) {
620				if (rdch != NULL) {
621					/*
622					 * Lock, deref and release previously
623					 * created record channel
624					 */
625					CHN_LOCK(rdch);
626					pcm_chnref(rdch, -1);
627					pcm_chnrelease(rdch);
628				}
629				(void)snd_clone_release(i_dev);
630				PCM_RELEASE_QUICK(d);
631				PCM_GIANT_EXIT(d);
632				return (wrerror);
633			}
634			wrch = NULL;
635		} else if (volctl == DSP_CDEV_VOLCTL_WRITE) {
636			if (wrch != NULL) {
637				pcm_chnref(wrch, 1);
638				pcm_chnrelease(wrch);
639			}
640		} else {
641			if (flags & O_NONBLOCK)
642				wrch->flags |= CHN_F_NBIO;
643			if (flags & O_EXCL)
644				wrch->flags |= CHN_F_EXCLUSIVE;
645			pcm_chnref(wrch, 1);
646			if (volctl == DSP_CDEV_VOLCTL_NONE)
647				chn_vpc_reset(wrch, SND_VOL_C_PCM, 0);
648			CHN_UNLOCK(wrch);
649		}
650	}
651
652
653	PCM_LOCK(d);
654
655	/*
656	 * We're done. Allocate channels information for this cdev.
657	 */
658	switch (volctl) {
659	case DSP_CDEV_VOLCTL_READ:
660		KASSERT(wrch == NULL, ("wrch=%p not null!", wrch));
661		dsp_cdevinfo_alloc(i_dev, NULL, NULL, rdch);
662		break;
663	case DSP_CDEV_VOLCTL_WRITE:
664		KASSERT(rdch == NULL, ("rdch=%p not null!", rdch));
665		dsp_cdevinfo_alloc(i_dev, NULL, NULL, wrch);
666		break;
667	case DSP_CDEV_VOLCTL_NONE:
668	default:
669		if (wrch == NULL && rdch == NULL) {
670			(void)snd_clone_release(i_dev);
671			PCM_RELEASE(d);
672			PCM_UNLOCK(d);
673			PCM_GIANT_EXIT(d);
674			if (wrerror != 0)
675				return (wrerror);
676			if (rderror != 0)
677				return (rderror);
678			return (EINVAL);
679		}
680		dsp_cdevinfo_alloc(i_dev, rdch, wrch, NULL);
681		if (rdch != NULL)
682			CHN_INSERT_HEAD(d, rdch, channels.pcm.opened);
683		if (wrch != NULL)
684			CHN_INSERT_HEAD(d, wrch, channels.pcm.opened);
685		break;
686	}
687
688	/*
689	 * Increase clone refcount for its automatic garbage collector.
690	 */
691	(void)snd_clone_ref(i_dev);
692
693	PCM_RELEASE(d);
694	PCM_UNLOCK(d);
695
696	PCM_GIANT_LEAVE(d);
697
698	return (0);
699}
700
701static int
702dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
703{
704	struct pcm_channel *rdch, *wrch, *volch;
705	struct snddev_info *d;
706	int sg_ids, rdref, wdref;
707
708	d = dsp_get_info(i_dev);
709	if (!DSP_REGISTERED(d, i_dev))
710		return (EBADF);
711
712	PCM_GIANT_ENTER(d);
713
714	PCM_LOCK(d);
715	PCM_WAIT(d);
716	PCM_ACQUIRE(d);
717
718	rdch = PCM_RDCH(i_dev);
719	wrch = PCM_WRCH(i_dev);
720	volch = PCM_VOLCH(i_dev);
721
722	PCM_RDCH(i_dev) = NULL;
723	PCM_WRCH(i_dev) = NULL;
724	PCM_VOLCH(i_dev) = NULL;
725
726	rdref = -1;
727	wdref = -1;
728
729	if (volch != NULL) {
730		if (volch == rdch)
731			rdref--;
732		else if (volch == wrch)
733			wdref--;
734		else {
735			CHN_LOCK(volch);
736			pcm_chnref(volch, -1);
737			CHN_UNLOCK(volch);
738		}
739	}
740
741	if (rdch != NULL)
742		CHN_REMOVE(d, rdch, channels.pcm.opened);
743	if (wrch != NULL)
744		CHN_REMOVE(d, wrch, channels.pcm.opened);
745
746	if (rdch != NULL || wrch != NULL) {
747		PCM_UNLOCK(d);
748		if (rdch != NULL) {
749			/*
750			 * The channel itself need not be locked because:
751			 *   a)  Adding a channel to a syncgroup happens only
752			 *       in dsp_ioctl(), which cannot run concurrently
753			 *       to dsp_close().
754			 *   b)  The syncmember pointer (sm) is protected by
755			 *       the global syncgroup list lock.
756			 *   c)  A channel can't just disappear, invalidating
757			 *       pointers, unless it's closed/dereferenced
758			 *       first.
759			 */
760			PCM_SG_LOCK();
761			sg_ids = chn_syncdestroy(rdch);
762			PCM_SG_UNLOCK();
763			if (sg_ids != 0)
764				free_unr(pcmsg_unrhdr, sg_ids);
765
766			CHN_LOCK(rdch);
767			pcm_chnref(rdch, rdref);
768			chn_abort(rdch); /* won't sleep */
769			rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
770			    CHN_F_DEAD | CHN_F_EXCLUSIVE);
771			chn_reset(rdch, 0, 0);
772			pcm_chnrelease(rdch);
773		}
774		if (wrch != NULL) {
775			/*
776			 * Please see block above.
777			 */
778			PCM_SG_LOCK();
779			sg_ids = chn_syncdestroy(wrch);
780			PCM_SG_UNLOCK();
781			if (sg_ids != 0)
782				free_unr(pcmsg_unrhdr, sg_ids);
783
784			CHN_LOCK(wrch);
785			pcm_chnref(wrch, wdref);
786			chn_flush(wrch); /* may sleep */
787			wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
788			    CHN_F_DEAD | CHN_F_EXCLUSIVE);
789			chn_reset(wrch, 0, 0);
790			pcm_chnrelease(wrch);
791		}
792		PCM_LOCK(d);
793	}
794
795	dsp_cdevinfo_free(i_dev);
796	/*
797	 * Release clone busy state and unref it so the automatic
798	 * garbage collector will get the hint and do the remaining
799	 * cleanup process.
800	 */
801	(void)snd_clone_release(i_dev);
802
803	/*
804	 * destroy_dev() might sleep, so release pcm lock
805	 * here and rely on pcm cv serialization.
806	 */
807	PCM_UNLOCK(d);
808	(void)snd_clone_unref(i_dev);
809	PCM_LOCK(d);
810
811	PCM_RELEASE(d);
812	PCM_UNLOCK(d);
813
814	PCM_GIANT_LEAVE(d);
815
816	return (0);
817}
818
819static __inline int
820dsp_io_ops(struct cdev *i_dev, struct uio *buf)
821{
822	struct snddev_info *d;
823	struct pcm_channel **ch, *rdch, *wrch;
824	int (*chn_io)(struct pcm_channel *, struct uio *);
825	int prio, ret;
826	pid_t runpid;
827
828	KASSERT(i_dev != NULL && buf != NULL &&
829	    (buf->uio_rw == UIO_READ || buf->uio_rw == UIO_WRITE),
830	    ("%s(): io train wreck!", __func__));
831
832	d = dsp_get_info(i_dev);
833	if (!DSP_REGISTERED(d, i_dev))
834		return (EBADF);
835
836	PCM_GIANT_ENTER(d);
837
838	switch (buf->uio_rw) {
839	case UIO_READ:
840		prio = SD_F_PRIO_RD;
841		ch = &rdch;
842		chn_io = chn_read;
843		break;
844	case UIO_WRITE:
845		prio = SD_F_PRIO_WR;
846		ch = &wrch;
847		chn_io = chn_write;
848		break;
849	default:
850		panic("invalid/corrupted uio direction: %d", buf->uio_rw);
851		break;
852	}
853
854	rdch = NULL;
855	wrch = NULL;
856	runpid = buf->uio_td->td_proc->p_pid;
857
858	getchns(i_dev, &rdch, &wrch, prio);
859
860	if (*ch == NULL || !((*ch)->flags & CHN_F_BUSY)) {
861		PCM_GIANT_EXIT(d);
862		return (EBADF);
863	}
864
865	if (((*ch)->flags & (CHN_F_MMAP | CHN_F_DEAD)) ||
866	    (((*ch)->flags & CHN_F_RUNNING) && (*ch)->pid != runpid)) {
867		relchns(i_dev, rdch, wrch, prio);
868		PCM_GIANT_EXIT(d);
869		return (EINVAL);
870	} else if (!((*ch)->flags & CHN_F_RUNNING)) {
871		(*ch)->flags |= CHN_F_RUNNING;
872		(*ch)->pid = runpid;
873	}
874
875	/*
876	 * chn_read/write must give up channel lock in order to copy bytes
877	 * from/to userland, so up the "in progress" counter to make sure
878	 * someone else doesn't come along and muss up the buffer.
879	 */
880	++(*ch)->inprog;
881	ret = chn_io(*ch, buf);
882	--(*ch)->inprog;
883
884	CHN_BROADCAST(&(*ch)->cv);
885
886	relchns(i_dev, rdch, wrch, prio);
887
888	PCM_GIANT_LEAVE(d);
889
890	return (ret);
891}
892
893static int
894dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
895{
896	return (dsp_io_ops(i_dev, buf));
897}
898
899static int
900dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
901{
902	return (dsp_io_ops(i_dev, buf));
903}
904
905static int
906dsp_get_volume_channel(struct cdev *dev, struct pcm_channel **volch)
907{
908	struct snddev_info *d;
909	struct pcm_channel *c;
910	int unit;
911
912	KASSERT(dev != NULL && volch != NULL,
913	    ("%s(): NULL query dev=%p volch=%p", __func__, dev, volch));
914
915	d = dsp_get_info(dev);
916	if (!PCM_REGISTERED(d)) {
917		*volch = NULL;
918		return (EINVAL);
919	}
920
921	PCM_UNLOCKASSERT(d);
922
923	*volch = NULL;
924
925	c = PCM_VOLCH(dev);
926	if (c != NULL) {
927		if (!(c->feederflags & (1 << FEEDER_VOLUME)))
928			return (-1);
929		*volch = c;
930		return (0);
931	}
932
933	PCM_LOCK(d);
934	PCM_WAIT(d);
935	PCM_ACQUIRE(d);
936
937	unit = dev2unit(dev);
938
939	CHN_FOREACH(c, d, channels.pcm) {
940		CHN_LOCK(c);
941		if (c->unit != unit) {
942			CHN_UNLOCK(c);
943			continue;
944		}
945		*volch = c;
946		pcm_chnref(c, 1);
947		PCM_VOLCH(dev) = c;
948		CHN_UNLOCK(c);
949		PCM_RELEASE(d);
950		PCM_UNLOCK(d);
951		return ((c->feederflags & (1 << FEEDER_VOLUME)) ? 0 : -1);
952	}
953
954	PCM_RELEASE(d);
955	PCM_UNLOCK(d);
956
957	return (EINVAL);
958}
959
960static int
961dsp_ioctl_channel(struct cdev *dev, struct pcm_channel *volch, u_long cmd,
962    caddr_t arg)
963{
964	struct snddev_info *d;
965	struct pcm_channel *rdch, *wrch;
966	int j, devtype, ret;
967
968	d = dsp_get_info(dev);
969	if (!PCM_REGISTERED(d) || !(dsp_get_flags(dev) & SD_F_VPC))
970		return (-1);
971
972	PCM_UNLOCKASSERT(d);
973
974	j = cmd & 0xff;
975
976	rdch = PCM_RDCH(dev);
977	wrch = PCM_WRCH(dev);
978
979	/* No specific channel, look into cache */
980	if (volch == NULL)
981		volch = PCM_VOLCH(dev);
982
983	/* Look harder */
984	if (volch == NULL) {
985		if (j == SOUND_MIXER_RECLEV && rdch != NULL)
986			volch = rdch;
987		else if (j == SOUND_MIXER_PCM && wrch != NULL)
988			volch = wrch;
989	}
990
991	devtype = PCMDEV(dev);
992
993	/* Look super harder */
994	if (volch == NULL &&
995	    (devtype == SND_DEV_DSPHW_PLAY || devtype == SND_DEV_DSPHW_VPLAY ||
996	    devtype == SND_DEV_DSPHW_REC || devtype == SND_DEV_DSPHW_VREC)) {
997		ret = dsp_get_volume_channel(dev, &volch);
998		if (ret != 0)
999			return (ret);
1000		if (volch == NULL)
1001			return (EINVAL);
1002	}
1003
1004	/* Final validation */
1005	if (volch != NULL) {
1006		CHN_LOCK(volch);
1007		if (!(volch->feederflags & (1 << FEEDER_VOLUME))) {
1008			CHN_UNLOCK(volch);
1009			return (-1);
1010		}
1011		if (volch->direction == PCMDIR_PLAY)
1012			wrch = volch;
1013		else
1014			rdch = volch;
1015	}
1016
1017	ret = EINVAL;
1018
1019	if (volch != NULL &&
1020	    ((j == SOUND_MIXER_PCM && volch->direction == PCMDIR_PLAY) ||
1021	    (j == SOUND_MIXER_RECLEV && volch->direction == PCMDIR_REC))) {
1022		if ((cmd & ~0xff) == MIXER_WRITE(0)) {
1023			int left, right, center;
1024
1025			left = *(int *)arg & 0x7f;
1026			right = ((*(int *)arg) >> 8) & 0x7f;
1027			center = (left + right) >> 1;
1028			chn_setvolume_multi(volch, SND_VOL_C_PCM, left, right,
1029			    center);
1030		} else if ((cmd & ~0xff) == MIXER_READ(0)) {
1031			*(int *)arg = CHN_GETVOLUME(volch,
1032				SND_VOL_C_PCM, SND_CHN_T_FL);
1033			*(int *)arg |= CHN_GETVOLUME(volch,
1034				SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
1035		}
1036		ret = 0;
1037	} else if (rdch != NULL || wrch != NULL) {
1038		switch (j) {
1039		case SOUND_MIXER_DEVMASK:
1040		case SOUND_MIXER_CAPS:
1041		case SOUND_MIXER_STEREODEVS:
1042			if ((cmd & ~0xff) == MIXER_READ(0)) {
1043				*(int *)arg = 0;
1044				if (rdch != NULL)
1045					*(int *)arg |= SOUND_MASK_RECLEV;
1046				if (wrch != NULL)
1047					*(int *)arg |= SOUND_MASK_PCM;
1048			}
1049			ret = 0;
1050			break;
1051		case SOUND_MIXER_RECMASK:
1052		case SOUND_MIXER_RECSRC:
1053			if ((cmd & ~0xff) == MIXER_READ(0))
1054				*(int *)arg = 0;
1055			ret = 0;
1056			break;
1057		default:
1058			break;
1059		}
1060	}
1061
1062	if (volch != NULL)
1063		CHN_UNLOCK(volch);
1064
1065	return (ret);
1066}
1067
1068static int
1069dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1070    struct thread *td)
1071{
1072    	struct pcm_channel *chn, *rdch, *wrch;
1073	struct snddev_info *d;
1074	u_long xcmd;
1075	int *arg_i, ret, tmp;
1076
1077	d = dsp_get_info(i_dev);
1078	if (!DSP_REGISTERED(d, i_dev))
1079		return (EBADF);
1080
1081	PCM_GIANT_ENTER(d);
1082
1083	arg_i = (int *)arg;
1084	ret = 0;
1085	xcmd = 0;
1086	chn = NULL;
1087
1088	if (IOCGROUP(cmd) == 'M') {
1089		if (cmd == OSS_GETVERSION) {
1090			*arg_i = SOUND_VERSION;
1091			PCM_GIANT_EXIT(d);
1092			return (0);
1093		}
1094		ret = dsp_ioctl_channel(i_dev, PCM_VOLCH(i_dev), cmd, arg);
1095		if (ret != -1) {
1096			PCM_GIANT_EXIT(d);
1097			return (ret);
1098		}
1099
1100		if (d->mixer_dev != NULL) {
1101			PCM_ACQUIRE_QUICK(d);
1102			ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1103			    MIXER_CMD_DIRECT);
1104			PCM_RELEASE_QUICK(d);
1105		} else
1106			ret = EBADF;
1107
1108		PCM_GIANT_EXIT(d);
1109
1110		return (ret);
1111	}
1112
1113	/*
1114	 * Certain ioctls may be made on any type of device (audio, mixer,
1115	 * and MIDI).  Handle those special cases here.
1116	 */
1117	if (IOCGROUP(cmd) == 'X') {
1118		PCM_ACQUIRE_QUICK(d);
1119		switch(cmd) {
1120		case SNDCTL_SYSINFO:
1121			sound_oss_sysinfo((oss_sysinfo *)arg);
1122			break;
1123		case SNDCTL_CARDINFO:
1124			ret = sound_oss_card_info((oss_card_info *)arg);
1125			break;
1126		case SNDCTL_AUDIOINFO:
1127		case SNDCTL_AUDIOINFO_EX:
1128		case SNDCTL_ENGINEINFO:
1129			ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
1130			break;
1131		case SNDCTL_MIXERINFO:
1132			ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
1133			break;
1134		default:
1135			ret = EINVAL;
1136		}
1137		PCM_RELEASE_QUICK(d);
1138		PCM_GIANT_EXIT(d);
1139		return (ret);
1140	}
1141
1142	getchns(i_dev, &rdch, &wrch, 0);
1143
1144	if (wrch != NULL && (wrch->flags & CHN_F_DEAD))
1145		wrch = NULL;
1146	if (rdch != NULL && (rdch->flags & CHN_F_DEAD))
1147		rdch = NULL;
1148
1149	if (wrch == NULL && rdch == NULL) {
1150		PCM_GIANT_EXIT(d);
1151		return (EINVAL);
1152	}
1153
1154    	switch(cmd) {
1155#ifdef OLDPCM_IOCTL
1156    	/*
1157     	 * we start with the new ioctl interface.
1158     	 */
1159    	case AIONWRITE:	/* how many bytes can write ? */
1160		if (wrch) {
1161			CHN_LOCK(wrch);
1162/*
1163		if (wrch && wrch->bufhard.dl)
1164			while (chn_wrfeed(wrch) == 0);
1165*/
1166			*arg_i = sndbuf_getfree(wrch->bufsoft);
1167			CHN_UNLOCK(wrch);
1168		} else {
1169			*arg_i = 0;
1170			ret = EINVAL;
1171		}
1172		break;
1173
1174    	case AIOSSIZE:     /* set the current blocksize */
1175		{
1176	    		struct snd_size *p = (struct snd_size *)arg;
1177
1178			p->play_size = 0;
1179			p->rec_size = 0;
1180			PCM_ACQUIRE_QUICK(d);
1181	    		if (wrch) {
1182				CHN_LOCK(wrch);
1183				chn_setblocksize(wrch, 2, p->play_size);
1184				p->play_size = sndbuf_getblksz(wrch->bufsoft);
1185				CHN_UNLOCK(wrch);
1186			}
1187	    		if (rdch) {
1188				CHN_LOCK(rdch);
1189				chn_setblocksize(rdch, 2, p->rec_size);
1190				p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1191				CHN_UNLOCK(rdch);
1192			}
1193			PCM_RELEASE_QUICK(d);
1194		}
1195		break;
1196    	case AIOGSIZE:	/* get the current blocksize */
1197		{
1198	    		struct snd_size *p = (struct snd_size *)arg;
1199
1200	    		if (wrch) {
1201				CHN_LOCK(wrch);
1202				p->play_size = sndbuf_getblksz(wrch->bufsoft);
1203				CHN_UNLOCK(wrch);
1204			}
1205	    		if (rdch) {
1206				CHN_LOCK(rdch);
1207				p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1208				CHN_UNLOCK(rdch);
1209			}
1210		}
1211		break;
1212
1213    	case AIOSFMT:
1214    	case AIOGFMT:
1215		{
1216	    		snd_chan_param *p = (snd_chan_param *)arg;
1217
1218			if (cmd == AIOSFMT &&
1219			    ((p->play_format != 0 && p->play_rate == 0) ||
1220			    (p->rec_format != 0 && p->rec_rate == 0))) {
1221				ret = EINVAL;
1222				break;
1223			}
1224			PCM_ACQUIRE_QUICK(d);
1225	    		if (wrch) {
1226				CHN_LOCK(wrch);
1227				if (cmd == AIOSFMT && p->play_format != 0) {
1228					chn_setformat(wrch,
1229					    SND_FORMAT(p->play_format,
1230					    AFMT_CHANNEL(wrch->format),
1231					    AFMT_EXTCHANNEL(wrch->format)));
1232					chn_setspeed(wrch, p->play_rate);
1233				}
1234	    			p->play_rate = wrch->speed;
1235	    			p->play_format = AFMT_ENCODING(wrch->format);
1236				CHN_UNLOCK(wrch);
1237			} else {
1238	    			p->play_rate = 0;
1239	    			p->play_format = 0;
1240	    		}
1241	    		if (rdch) {
1242				CHN_LOCK(rdch);
1243				if (cmd == AIOSFMT && p->rec_format != 0) {
1244					chn_setformat(rdch,
1245					    SND_FORMAT(p->rec_format,
1246					    AFMT_CHANNEL(rdch->format),
1247					    AFMT_EXTCHANNEL(rdch->format)));
1248					chn_setspeed(rdch, p->rec_rate);
1249				}
1250				p->rec_rate = rdch->speed;
1251				p->rec_format = AFMT_ENCODING(rdch->format);
1252				CHN_UNLOCK(rdch);
1253			} else {
1254	    			p->rec_rate = 0;
1255	    			p->rec_format = 0;
1256	    		}
1257			PCM_RELEASE_QUICK(d);
1258		}
1259		break;
1260
1261    	case AIOGCAP:     /* get capabilities */
1262		{
1263	    		snd_capabilities *p = (snd_capabilities *)arg;
1264			struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
1265			struct cdev *pdev;
1266
1267			PCM_LOCK(d);
1268			if (rdch) {
1269				CHN_LOCK(rdch);
1270				rcaps = chn_getcaps(rdch);
1271			}
1272			if (wrch) {
1273				CHN_LOCK(wrch);
1274				pcaps = chn_getcaps(wrch);
1275			}
1276	    		p->rate_min = max(rcaps? rcaps->minspeed : 0,
1277	                      		  pcaps? pcaps->minspeed : 0);
1278	    		p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
1279	                      		  pcaps? pcaps->maxspeed : 1000000);
1280	    		p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
1281	                     		 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
1282			/* XXX bad on sb16 */
1283	    		p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
1284			 	     (wrch? chn_getformats(wrch) : 0xffffffff);
1285			if (rdch && wrch)
1286				p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
1287			pdev = d->mixer_dev;
1288	    		p->mixers = 1; /* default: one mixer */
1289	    		p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
1290	    		p->left = p->right = 100;
1291			if (wrch)
1292				CHN_UNLOCK(wrch);
1293			if (rdch)
1294				CHN_UNLOCK(rdch);
1295			PCM_UNLOCK(d);
1296		}
1297		break;
1298
1299    	case AIOSTOP:
1300		if (*arg_i == AIOSYNC_PLAY && wrch) {
1301			CHN_LOCK(wrch);
1302			*arg_i = chn_abort(wrch);
1303			CHN_UNLOCK(wrch);
1304		} else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
1305			CHN_LOCK(rdch);
1306			*arg_i = chn_abort(rdch);
1307			CHN_UNLOCK(rdch);
1308		} else {
1309	   	 	printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
1310	    		*arg_i = 0;
1311		}
1312		break;
1313
1314    	case AIOSYNC:
1315		printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
1316	    		((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
1317		break;
1318#endif
1319	/*
1320	 * here follow the standard ioctls (filio.h etc.)
1321	 */
1322    	case FIONREAD: /* get # bytes to read */
1323		if (rdch) {
1324			CHN_LOCK(rdch);
1325/*			if (rdch && rdch->bufhard.dl)
1326				while (chn_rdfeed(rdch) == 0);
1327*/
1328			*arg_i = sndbuf_getready(rdch->bufsoft);
1329			CHN_UNLOCK(rdch);
1330		} else {
1331			*arg_i = 0;
1332			ret = EINVAL;
1333		}
1334		break;
1335
1336    	case FIOASYNC: /*set/clear async i/o */
1337		DEB( printf("FIOASYNC\n") ; )
1338		break;
1339
1340    	case SNDCTL_DSP_NONBLOCK: /* set non-blocking i/o */
1341    	case FIONBIO: /* set/clear non-blocking i/o */
1342		if (rdch) {
1343			CHN_LOCK(rdch);
1344			if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1345				rdch->flags |= CHN_F_NBIO;
1346			else
1347				rdch->flags &= ~CHN_F_NBIO;
1348			CHN_UNLOCK(rdch);
1349		}
1350		if (wrch) {
1351			CHN_LOCK(wrch);
1352			if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1353				wrch->flags |= CHN_F_NBIO;
1354			else
1355				wrch->flags &= ~CHN_F_NBIO;
1356			CHN_UNLOCK(wrch);
1357		}
1358		break;
1359
1360    	/*
1361	 * Finally, here is the linux-compatible ioctl interface
1362	 */
1363#define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
1364    	case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
1365    	case SNDCTL_DSP_GETBLKSIZE:
1366		chn = wrch ? wrch : rdch;
1367		if (chn) {
1368			CHN_LOCK(chn);
1369			*arg_i = sndbuf_getblksz(chn->bufsoft);
1370			CHN_UNLOCK(chn);
1371		} else {
1372			*arg_i = 0;
1373			ret = EINVAL;
1374		}
1375		break;
1376
1377    	case SNDCTL_DSP_SETBLKSIZE:
1378		RANGE(*arg_i, 16, 65536);
1379		PCM_ACQUIRE_QUICK(d);
1380		if (wrch) {
1381			CHN_LOCK(wrch);
1382			chn_setblocksize(wrch, 2, *arg_i);
1383			CHN_UNLOCK(wrch);
1384		}
1385		if (rdch) {
1386			CHN_LOCK(rdch);
1387			chn_setblocksize(rdch, 2, *arg_i);
1388			CHN_UNLOCK(rdch);
1389		}
1390		PCM_RELEASE_QUICK(d);
1391		break;
1392
1393    	case SNDCTL_DSP_RESET:
1394		DEB(printf("dsp reset\n"));
1395		if (wrch) {
1396			CHN_LOCK(wrch);
1397			chn_abort(wrch);
1398			chn_resetbuf(wrch);
1399			CHN_UNLOCK(wrch);
1400		}
1401		if (rdch) {
1402			CHN_LOCK(rdch);
1403			chn_abort(rdch);
1404			chn_resetbuf(rdch);
1405			CHN_UNLOCK(rdch);
1406		}
1407		break;
1408
1409    	case SNDCTL_DSP_SYNC:
1410		DEB(printf("dsp sync\n"));
1411		/* chn_sync may sleep */
1412		if (wrch) {
1413			CHN_LOCK(wrch);
1414			chn_sync(wrch, 0);
1415			CHN_UNLOCK(wrch);
1416		}
1417		break;
1418
1419    	case SNDCTL_DSP_SPEED:
1420		/* chn_setspeed may sleep */
1421		tmp = 0;
1422		PCM_ACQUIRE_QUICK(d);
1423		if (wrch) {
1424			CHN_LOCK(wrch);
1425			ret = chn_setspeed(wrch, *arg_i);
1426			tmp = wrch->speed;
1427			CHN_UNLOCK(wrch);
1428		}
1429		if (rdch && ret == 0) {
1430			CHN_LOCK(rdch);
1431			ret = chn_setspeed(rdch, *arg_i);
1432			if (tmp == 0)
1433				tmp = rdch->speed;
1434			CHN_UNLOCK(rdch);
1435		}
1436		PCM_RELEASE_QUICK(d);
1437		*arg_i = tmp;
1438		break;
1439
1440    	case SOUND_PCM_READ_RATE:
1441		chn = wrch ? wrch : rdch;
1442		if (chn) {
1443			CHN_LOCK(chn);
1444			*arg_i = chn->speed;
1445			CHN_UNLOCK(chn);
1446		} else {
1447			*arg_i = 0;
1448			ret = EINVAL;
1449		}
1450		break;
1451
1452    	case SNDCTL_DSP_STEREO:
1453		tmp = -1;
1454		*arg_i = (*arg_i)? 2 : 1;
1455		PCM_ACQUIRE_QUICK(d);
1456		if (wrch) {
1457			CHN_LOCK(wrch);
1458			ret = chn_setformat(wrch,
1459			    SND_FORMAT(wrch->format, *arg_i, 0));
1460			tmp = (AFMT_CHANNEL(wrch->format) > 1)? 1 : 0;
1461			CHN_UNLOCK(wrch);
1462		}
1463		if (rdch && ret == 0) {
1464			CHN_LOCK(rdch);
1465			ret = chn_setformat(rdch,
1466			    SND_FORMAT(rdch->format, *arg_i, 0));
1467			if (tmp == -1)
1468				tmp = (AFMT_CHANNEL(rdch->format) > 1)? 1 : 0;
1469			CHN_UNLOCK(rdch);
1470		}
1471		PCM_RELEASE_QUICK(d);
1472		*arg_i = tmp;
1473		break;
1474
1475    	case SOUND_PCM_WRITE_CHANNELS:
1476/*	case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
1477		if (*arg_i < 0) {
1478			*arg_i = 0;
1479			ret = EINVAL;
1480			break;
1481		}
1482		if (*arg_i != 0) {
1483			struct pcmchan_matrix *m;
1484			uint32_t ext;
1485
1486			tmp = 0;
1487			if (*arg_i > SND_CHN_MAX)
1488				*arg_i = SND_CHN_MAX;
1489
1490			m = feeder_matrix_default_channel_map(*arg_i);
1491			if (m != NULL)
1492				ext = m->ext;
1493			else
1494				ext = 0;
1495
1496			PCM_ACQUIRE_QUICK(d);
1497	  		if (wrch) {
1498				CHN_LOCK(wrch);
1499				ret = chn_setformat(wrch,
1500				    SND_FORMAT(wrch->format, *arg_i, ext));
1501				tmp = AFMT_CHANNEL(wrch->format);
1502				CHN_UNLOCK(wrch);
1503			}
1504			if (rdch && ret == 0) {
1505				CHN_LOCK(rdch);
1506				ret = chn_setformat(rdch,
1507				    SND_FORMAT(rdch->format, *arg_i, ext));
1508				if (tmp == 0)
1509					tmp = AFMT_CHANNEL(rdch->format);
1510				CHN_UNLOCK(rdch);
1511			}
1512			PCM_RELEASE_QUICK(d);
1513			*arg_i = tmp;
1514		} else {
1515			chn = wrch ? wrch : rdch;
1516			CHN_LOCK(chn);
1517			*arg_i = AFMT_CHANNEL(chn->format);
1518			CHN_UNLOCK(chn);
1519		}
1520		break;
1521
1522    	case SOUND_PCM_READ_CHANNELS:
1523		chn = wrch ? wrch : rdch;
1524		if (chn) {
1525			CHN_LOCK(chn);
1526			*arg_i = AFMT_CHANNEL(chn->format);
1527			CHN_UNLOCK(chn);
1528		} else {
1529			*arg_i = 0;
1530			ret = EINVAL;
1531		}
1532		break;
1533
1534    	case SNDCTL_DSP_GETFMTS:	/* returns a mask of supported fmts */
1535		chn = wrch ? wrch : rdch;
1536		if (chn) {
1537			CHN_LOCK(chn);
1538			*arg_i = chn_getformats(chn);
1539			CHN_UNLOCK(chn);
1540		} else {
1541			*arg_i = 0;
1542			ret = EINVAL;
1543		}
1544		break;
1545
1546    	case SNDCTL_DSP_SETFMT:	/* sets _one_ format */
1547		if (*arg_i != AFMT_QUERY) {
1548			tmp = 0;
1549			PCM_ACQUIRE_QUICK(d);
1550			if (wrch) {
1551				CHN_LOCK(wrch);
1552				ret = chn_setformat(wrch, SND_FORMAT(*arg_i,
1553				    AFMT_CHANNEL(wrch->format),
1554				    AFMT_EXTCHANNEL(wrch->format)));
1555				tmp = wrch->format;
1556				CHN_UNLOCK(wrch);
1557			}
1558			if (rdch && ret == 0) {
1559				CHN_LOCK(rdch);
1560				ret = chn_setformat(rdch, SND_FORMAT(*arg_i,
1561				    AFMT_CHANNEL(rdch->format),
1562				    AFMT_EXTCHANNEL(rdch->format)));
1563				if (tmp == 0)
1564					tmp = rdch->format;
1565				CHN_UNLOCK(rdch);
1566			}
1567			PCM_RELEASE_QUICK(d);
1568			*arg_i = AFMT_ENCODING(tmp);
1569		} else {
1570			chn = wrch ? wrch : rdch;
1571			CHN_LOCK(chn);
1572			*arg_i = AFMT_ENCODING(chn->format);
1573			CHN_UNLOCK(chn);
1574		}
1575		break;
1576
1577    	case SNDCTL_DSP_SETFRAGMENT:
1578		DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
1579		{
1580			uint32_t fragln = (*arg_i) & 0x0000ffff;
1581			uint32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
1582			uint32_t fragsz;
1583			uint32_t r_maxfrags, r_fragsz;
1584
1585			RANGE(fragln, 4, 16);
1586			fragsz = 1 << fragln;
1587
1588			if (maxfrags == 0)
1589				maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1590			if (maxfrags < 2)
1591				maxfrags = 2;
1592			if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
1593				maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1594
1595			DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
1596			PCM_ACQUIRE_QUICK(d);
1597		    	if (rdch) {
1598				CHN_LOCK(rdch);
1599				ret = chn_setblocksize(rdch, maxfrags, fragsz);
1600				r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
1601				r_fragsz = sndbuf_getblksz(rdch->bufsoft);
1602				CHN_UNLOCK(rdch);
1603			} else {
1604				r_maxfrags = maxfrags;
1605				r_fragsz = fragsz;
1606			}
1607		    	if (wrch && ret == 0) {
1608				CHN_LOCK(wrch);
1609				ret = chn_setblocksize(wrch, maxfrags, fragsz);
1610 				maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
1611				fragsz = sndbuf_getblksz(wrch->bufsoft);
1612				CHN_UNLOCK(wrch);
1613			} else { /* use whatever came from the read channel */
1614				maxfrags = r_maxfrags;
1615				fragsz = r_fragsz;
1616			}
1617			PCM_RELEASE_QUICK(d);
1618
1619			fragln = 0;
1620			while (fragsz > 1) {
1621				fragln++;
1622				fragsz >>= 1;
1623			}
1624	    		*arg_i = (maxfrags << 16) | fragln;
1625		}
1626		break;
1627
1628    	case SNDCTL_DSP_GETISPACE:
1629		/* return the size of data available in the input queue */
1630		{
1631	    		audio_buf_info *a = (audio_buf_info *)arg;
1632	    		if (rdch) {
1633	        		struct snd_dbuf *bs = rdch->bufsoft;
1634
1635				CHN_LOCK(rdch);
1636				a->bytes = sndbuf_getready(bs);
1637	        		a->fragments = a->bytes / sndbuf_getblksz(bs);
1638	        		a->fragstotal = sndbuf_getblkcnt(bs);
1639	        		a->fragsize = sndbuf_getblksz(bs);
1640				CHN_UNLOCK(rdch);
1641	    		} else
1642				ret = EINVAL;
1643		}
1644		break;
1645
1646    	case SNDCTL_DSP_GETOSPACE:
1647		/* return space available in the output queue */
1648		{
1649	    		audio_buf_info *a = (audio_buf_info *)arg;
1650	    		if (wrch) {
1651	        		struct snd_dbuf *bs = wrch->bufsoft;
1652
1653				CHN_LOCK(wrch);
1654				/* XXX abusive DMA update: chn_wrupdate(wrch); */
1655				a->bytes = sndbuf_getfree(bs);
1656	        		a->fragments = a->bytes / sndbuf_getblksz(bs);
1657	        		a->fragstotal = sndbuf_getblkcnt(bs);
1658	        		a->fragsize = sndbuf_getblksz(bs);
1659				CHN_UNLOCK(wrch);
1660	    		} else
1661				ret = EINVAL;
1662		}
1663		break;
1664
1665    	case SNDCTL_DSP_GETIPTR:
1666		{
1667	    		count_info *a = (count_info *)arg;
1668	    		if (rdch) {
1669	        		struct snd_dbuf *bs = rdch->bufsoft;
1670
1671				CHN_LOCK(rdch);
1672				/* XXX abusive DMA update: chn_rdupdate(rdch); */
1673	        		a->bytes = sndbuf_gettotal(bs);
1674	        		a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
1675	        		a->ptr = sndbuf_getfreeptr(bs);
1676				rdch->blocks = sndbuf_getblocks(bs);
1677				CHN_UNLOCK(rdch);
1678	    		} else
1679				ret = EINVAL;
1680		}
1681		break;
1682
1683    	case SNDCTL_DSP_GETOPTR:
1684		{
1685	    		count_info *a = (count_info *)arg;
1686	    		if (wrch) {
1687	        		struct snd_dbuf *bs = wrch->bufsoft;
1688
1689				CHN_LOCK(wrch);
1690				/* XXX abusive DMA update: chn_wrupdate(wrch); */
1691	        		a->bytes = sndbuf_gettotal(bs);
1692	        		a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
1693	        		a->ptr = sndbuf_getreadyptr(bs);
1694				wrch->blocks = sndbuf_getblocks(bs);
1695				CHN_UNLOCK(wrch);
1696	    		} else
1697				ret = EINVAL;
1698		}
1699		break;
1700
1701    	case SNDCTL_DSP_GETCAPS:
1702		PCM_LOCK(d);
1703		*arg_i = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER;
1704		if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1705			*arg_i |= PCM_CAP_DUPLEX;
1706		PCM_UNLOCK(d);
1707		break;
1708
1709    	case SOUND_PCM_READ_BITS:
1710		chn = wrch ? wrch : rdch;
1711		if (chn) {
1712			CHN_LOCK(chn);
1713			if (chn->format & AFMT_8BIT)
1714				*arg_i = 8;
1715			else if (chn->format & AFMT_16BIT)
1716				*arg_i = 16;
1717			else if (chn->format & AFMT_24BIT)
1718				*arg_i = 24;
1719			else if (chn->format & AFMT_32BIT)
1720				*arg_i = 32;
1721			else
1722				ret = EINVAL;
1723			CHN_UNLOCK(chn);
1724		} else {
1725			*arg_i = 0;
1726			ret = EINVAL;
1727		}
1728		break;
1729
1730    	case SNDCTL_DSP_SETTRIGGER:
1731		if (rdch) {
1732			CHN_LOCK(rdch);
1733			rdch->flags &= ~CHN_F_NOTRIGGER;
1734		    	if (*arg_i & PCM_ENABLE_INPUT)
1735				chn_start(rdch, 1);
1736			else {
1737				chn_abort(rdch);
1738				chn_resetbuf(rdch);
1739				rdch->flags |= CHN_F_NOTRIGGER;
1740			}
1741			CHN_UNLOCK(rdch);
1742		}
1743		if (wrch) {
1744			CHN_LOCK(wrch);
1745			wrch->flags &= ~CHN_F_NOTRIGGER;
1746		    	if (*arg_i & PCM_ENABLE_OUTPUT)
1747				chn_start(wrch, 1);
1748			else {
1749				chn_abort(wrch);
1750				chn_resetbuf(wrch);
1751				wrch->flags |= CHN_F_NOTRIGGER;
1752			}
1753			CHN_UNLOCK(wrch);
1754		}
1755		break;
1756
1757    	case SNDCTL_DSP_GETTRIGGER:
1758		*arg_i = 0;
1759		if (wrch) {
1760			CHN_LOCK(wrch);
1761			if (wrch->flags & CHN_F_TRIGGERED)
1762				*arg_i |= PCM_ENABLE_OUTPUT;
1763			CHN_UNLOCK(wrch);
1764		}
1765		if (rdch) {
1766			CHN_LOCK(rdch);
1767			if (rdch->flags & CHN_F_TRIGGERED)
1768				*arg_i |= PCM_ENABLE_INPUT;
1769			CHN_UNLOCK(rdch);
1770		}
1771		break;
1772
1773	case SNDCTL_DSP_GETODELAY:
1774		if (wrch) {
1775	        	struct snd_dbuf *bs = wrch->bufsoft;
1776
1777			CHN_LOCK(wrch);
1778			/* XXX abusive DMA update: chn_wrupdate(wrch); */
1779			*arg_i = sndbuf_getready(bs);
1780			CHN_UNLOCK(wrch);
1781		} else
1782			ret = EINVAL;
1783		break;
1784
1785    	case SNDCTL_DSP_POST:
1786		if (wrch) {
1787			CHN_LOCK(wrch);
1788			wrch->flags &= ~CHN_F_NOTRIGGER;
1789			chn_start(wrch, 1);
1790			CHN_UNLOCK(wrch);
1791		}
1792		break;
1793
1794	case SNDCTL_DSP_SETDUPLEX:
1795		/*
1796		 * switch to full-duplex mode if card is in half-duplex
1797		 * mode and is able to work in full-duplex mode
1798		 */
1799		PCM_LOCK(d);
1800		if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1801			dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1802		PCM_UNLOCK(d);
1803		break;
1804
1805	/*
1806	 * The following four ioctls are simple wrappers around mixer_ioctl
1807	 * with no further processing.  xcmd is short for "translated
1808	 * command".
1809	 */
1810	case SNDCTL_DSP_GETRECVOL:
1811		if (xcmd == 0) {
1812			xcmd = SOUND_MIXER_READ_RECLEV;
1813			chn = rdch;
1814		}
1815		/* FALLTHROUGH */
1816	case SNDCTL_DSP_SETRECVOL:
1817		if (xcmd == 0) {
1818			xcmd = SOUND_MIXER_WRITE_RECLEV;
1819			chn = rdch;
1820		}
1821		/* FALLTHROUGH */
1822	case SNDCTL_DSP_GETPLAYVOL:
1823		if (xcmd == 0) {
1824			xcmd = SOUND_MIXER_READ_PCM;
1825			chn = wrch;
1826		}
1827		/* FALLTHROUGH */
1828	case SNDCTL_DSP_SETPLAYVOL:
1829		if (xcmd == 0) {
1830			xcmd = SOUND_MIXER_WRITE_PCM;
1831			chn = wrch;
1832		}
1833
1834		ret = dsp_ioctl_channel(i_dev, chn, xcmd, arg);
1835		if (ret != -1) {
1836			PCM_GIANT_EXIT(d);
1837			return (ret);
1838		}
1839
1840		if (d->mixer_dev != NULL) {
1841			PCM_ACQUIRE_QUICK(d);
1842			ret = mixer_ioctl_cmd(d->mixer_dev, xcmd, arg, -1, td,
1843			    MIXER_CMD_DIRECT);
1844			PCM_RELEASE_QUICK(d);
1845		} else
1846			ret = ENOTSUP;
1847
1848		break;
1849
1850	case SNDCTL_DSP_GET_RECSRC_NAMES:
1851	case SNDCTL_DSP_GET_RECSRC:
1852	case SNDCTL_DSP_SET_RECSRC:
1853		if (d->mixer_dev != NULL) {
1854			PCM_ACQUIRE_QUICK(d);
1855			ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1856			    MIXER_CMD_DIRECT);
1857			PCM_RELEASE_QUICK(d);
1858		} else
1859			ret = ENOTSUP;
1860		break;
1861
1862	/*
1863	 * The following 3 ioctls aren't very useful at the moment.  For
1864	 * now, only a single channel is associated with a cdev (/dev/dspN
1865	 * instance), so there's only a single output routing to use (i.e.,
1866	 * the wrch bound to this cdev).
1867	 */
1868	case SNDCTL_DSP_GET_PLAYTGT_NAMES:
1869		{
1870			oss_mixer_enuminfo *ei;
1871			ei = (oss_mixer_enuminfo *)arg;
1872			ei->dev = 0;
1873			ei->ctrl = 0;
1874			ei->version = 0; /* static for now */
1875			ei->strindex[0] = 0;
1876
1877			if (wrch != NULL) {
1878				ei->nvalues = 1;
1879				strlcpy(ei->strings, wrch->name,
1880					sizeof(ei->strings));
1881			} else {
1882				ei->nvalues = 0;
1883				ei->strings[0] = '\0';
1884			}
1885		}
1886		break;
1887	case SNDCTL_DSP_GET_PLAYTGT:
1888	case SNDCTL_DSP_SET_PLAYTGT:	/* yes, they are the same for now */
1889		/*
1890		 * Re: SET_PLAYTGT
1891		 *   OSSv4: "The value that was accepted by the device will
1892		 *   be returned back in the variable pointed by the
1893		 *   argument."
1894		 */
1895		if (wrch != NULL)
1896			*arg_i = 0;
1897		else
1898			ret = EINVAL;
1899		break;
1900
1901	case SNDCTL_DSP_SILENCE:
1902	/*
1903	 * Flush the software (pre-feed) buffer, but try to minimize playback
1904	 * interruption.  (I.e., record unplayed samples with intent to
1905	 * restore by SNDCTL_DSP_SKIP.) Intended for application "pause"
1906	 * functionality.
1907	 */
1908		if (wrch == NULL)
1909			ret = EINVAL;
1910		else {
1911			struct snd_dbuf *bs;
1912			CHN_LOCK(wrch);
1913			while (wrch->inprog != 0)
1914				cv_wait(&wrch->cv, wrch->lock);
1915			bs = wrch->bufsoft;
1916			if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) {
1917				bs->sl = sndbuf_getready(bs);
1918				sndbuf_dispose(bs, bs->shadbuf, sndbuf_getready(bs));
1919				sndbuf_fillsilence(bs);
1920				chn_start(wrch, 0);
1921			}
1922			CHN_UNLOCK(wrch);
1923		}
1924		break;
1925
1926	case SNDCTL_DSP_SKIP:
1927	/*
1928	 * OSSv4 docs: "This ioctl call discards all unplayed samples in the
1929	 * playback buffer by moving the current write position immediately
1930	 * before the point where the device is currently reading the samples."
1931	 */
1932		if (wrch == NULL)
1933			ret = EINVAL;
1934		else {
1935			struct snd_dbuf *bs;
1936			CHN_LOCK(wrch);
1937			while (wrch->inprog != 0)
1938				cv_wait(&wrch->cv, wrch->lock);
1939			bs = wrch->bufsoft;
1940			if ((bs->shadbuf != NULL) && (bs->sl > 0)) {
1941				sndbuf_softreset(bs);
1942				sndbuf_acquire(bs, bs->shadbuf, bs->sl);
1943				bs->sl = 0;
1944				chn_start(wrch, 0);
1945			}
1946			CHN_UNLOCK(wrch);
1947		}
1948		break;
1949
1950	case SNDCTL_DSP_CURRENT_OPTR:
1951	case SNDCTL_DSP_CURRENT_IPTR:
1952	/**
1953	 * @note Changing formats resets the buffer counters, which differs
1954	 * 	 from the 4Front drivers.  However, I don't expect this to be
1955	 * 	 much of a problem.
1956	 *
1957	 * @note In a test where @c CURRENT_OPTR is called immediately after write
1958	 * 	 returns, this driver is about 32K samples behind whereas
1959	 * 	 4Front's is about 8K samples behind.  Should determine source
1960	 * 	 of discrepancy, even if only out of curiosity.
1961	 *
1962	 * @todo Actually test SNDCTL_DSP_CURRENT_IPTR.
1963	 */
1964		chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch;
1965		if (chn == NULL)
1966			ret = EINVAL;
1967		else {
1968			struct snd_dbuf *bs;
1969			/* int tmp; */
1970
1971			oss_count_t *oc = (oss_count_t *)arg;
1972
1973			CHN_LOCK(chn);
1974			bs = chn->bufsoft;
1975#if 0
1976			tmp = (sndbuf_getsize(b) + chn_getptr(chn) - sndbuf_gethwptr(b)) % sndbuf_getsize(b);
1977			oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getalign(b);
1978			oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getalign(b);
1979#else
1980			oc->samples = sndbuf_gettotal(bs) / sndbuf_getalign(bs);
1981			oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getalign(bs);
1982#endif
1983			CHN_UNLOCK(chn);
1984		}
1985		break;
1986
1987	case SNDCTL_DSP_HALT_OUTPUT:
1988	case SNDCTL_DSP_HALT_INPUT:
1989		chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch;
1990		if (chn == NULL)
1991			ret = EINVAL;
1992		else {
1993			CHN_LOCK(chn);
1994			chn_abort(chn);
1995			CHN_UNLOCK(chn);
1996		}
1997		break;
1998
1999	case SNDCTL_DSP_LOW_WATER:
2000	/*
2001	 * Set the number of bytes required to attract attention by
2002	 * select/poll.
2003	 */
2004		if (wrch != NULL) {
2005			CHN_LOCK(wrch);
2006			wrch->lw = (*arg_i > 1) ? *arg_i : 1;
2007			CHN_UNLOCK(wrch);
2008		}
2009		if (rdch != NULL) {
2010			CHN_LOCK(rdch);
2011			rdch->lw = (*arg_i > 1) ? *arg_i : 1;
2012			CHN_UNLOCK(rdch);
2013		}
2014		break;
2015
2016	case SNDCTL_DSP_GETERROR:
2017	/*
2018	 * OSSv4 docs:  "All errors and counters will automatically be
2019	 * cleared to zeroes after the call so each call will return only
2020	 * the errors that occurred after the previous invocation. ... The
2021	 * play_underruns and rec_overrun fields are the only useful fields
2022	 * returned by OSS 4.0."
2023	 */
2024		{
2025			audio_errinfo *ei = (audio_errinfo *)arg;
2026
2027			bzero((void *)ei, sizeof(*ei));
2028
2029			if (wrch != NULL) {
2030				CHN_LOCK(wrch);
2031				ei->play_underruns = wrch->xruns;
2032				wrch->xruns = 0;
2033				CHN_UNLOCK(wrch);
2034			}
2035			if (rdch != NULL) {
2036				CHN_LOCK(rdch);
2037				ei->rec_overruns = rdch->xruns;
2038				rdch->xruns = 0;
2039				CHN_UNLOCK(rdch);
2040			}
2041		}
2042		break;
2043
2044	case SNDCTL_DSP_SYNCGROUP:
2045		PCM_ACQUIRE_QUICK(d);
2046		ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
2047		PCM_RELEASE_QUICK(d);
2048		break;
2049
2050	case SNDCTL_DSP_SYNCSTART:
2051		PCM_ACQUIRE_QUICK(d);
2052		ret = dsp_oss_syncstart(*arg_i);
2053		PCM_RELEASE_QUICK(d);
2054		break;
2055
2056	case SNDCTL_DSP_POLICY:
2057		PCM_ACQUIRE_QUICK(d);
2058		ret = dsp_oss_policy(wrch, rdch, *arg_i);
2059		PCM_RELEASE_QUICK(d);
2060		break;
2061
2062	case SNDCTL_DSP_COOKEDMODE:
2063		PCM_ACQUIRE_QUICK(d);
2064		if (!(dsp_get_flags(i_dev) & SD_F_BITPERFECT))
2065			ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
2066		PCM_RELEASE_QUICK(d);
2067		break;
2068	case SNDCTL_DSP_GET_CHNORDER:
2069		PCM_ACQUIRE_QUICK(d);
2070		ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
2071		PCM_RELEASE_QUICK(d);
2072		break;
2073	case SNDCTL_DSP_SET_CHNORDER:
2074		PCM_ACQUIRE_QUICK(d);
2075		ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
2076		PCM_RELEASE_QUICK(d);
2077		break;
2078	case SNDCTL_DSP_GETCHANNELMASK:		/* XXX vlc */
2079		PCM_ACQUIRE_QUICK(d);
2080		ret = dsp_oss_getchannelmask(wrch, rdch, (int *)arg);
2081		PCM_RELEASE_QUICK(d);
2082		break;
2083	case SNDCTL_DSP_BIND_CHANNEL:		/* XXX what?!? */
2084		ret = EINVAL;
2085		break;
2086#ifdef	OSSV4_EXPERIMENT
2087	/*
2088	 * XXX The following ioctls are not yet supported and just return
2089	 * EINVAL.
2090	 */
2091	case SNDCTL_DSP_GETOPEAKS:
2092	case SNDCTL_DSP_GETIPEAKS:
2093		chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch;
2094		if (chn == NULL)
2095			ret = EINVAL;
2096		else {
2097			oss_peaks_t *op = (oss_peaks_t *)arg;
2098			int lpeak, rpeak;
2099
2100			CHN_LOCK(chn);
2101			ret = chn_getpeaks(chn, &lpeak, &rpeak);
2102			if (ret == -1)
2103				ret = EINVAL;
2104			else {
2105				(*op)[0] = lpeak;
2106				(*op)[1] = rpeak;
2107			}
2108			CHN_UNLOCK(chn);
2109		}
2110		break;
2111
2112	/*
2113	 * XXX Once implemented, revisit this for proper cv protection
2114	 *     (if necessary).
2115	 */
2116	case SNDCTL_GETLABEL:
2117		ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg);
2118		break;
2119	case SNDCTL_SETLABEL:
2120		ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg);
2121		break;
2122	case SNDCTL_GETSONG:
2123		ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg);
2124		break;
2125	case SNDCTL_SETSONG:
2126		ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg);
2127		break;
2128	case SNDCTL_SETNAME:
2129		ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg);
2130		break;
2131#if 0
2132	/**
2133	 * @note The S/PDIF interface ioctls, @c SNDCTL_DSP_READCTL and
2134	 * @c SNDCTL_DSP_WRITECTL have been omitted at the suggestion of
2135	 * 4Front Technologies.
2136	 */
2137	case SNDCTL_DSP_READCTL:
2138	case SNDCTL_DSP_WRITECTL:
2139		ret = EINVAL;
2140		break;
2141#endif	/* !0 (explicitly omitted ioctls) */
2142
2143#endif	/* !OSSV4_EXPERIMENT */
2144    	case SNDCTL_DSP_MAPINBUF:
2145    	case SNDCTL_DSP_MAPOUTBUF:
2146    	case SNDCTL_DSP_SETSYNCRO:
2147		/* undocumented */
2148
2149    	case SNDCTL_DSP_SUBDIVIDE:
2150    	case SOUND_PCM_WRITE_FILTER:
2151    	case SOUND_PCM_READ_FILTER:
2152		/* dunno what these do, don't sound important */
2153
2154    	default:
2155		DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
2156		ret = EINVAL;
2157		break;
2158    	}
2159
2160	PCM_GIANT_LEAVE(d);
2161
2162    	return (ret);
2163}
2164
2165static int
2166dsp_poll(struct cdev *i_dev, int events, struct thread *td)
2167{
2168	struct snddev_info *d;
2169	struct pcm_channel *wrch, *rdch;
2170	int ret, e;
2171
2172	d = dsp_get_info(i_dev);
2173	if (!DSP_REGISTERED(d, i_dev))
2174		return (EBADF);
2175
2176	PCM_GIANT_ENTER(d);
2177
2178	wrch = NULL;
2179	rdch = NULL;
2180	ret = 0;
2181
2182	getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2183
2184	if (wrch != NULL && !(wrch->flags & CHN_F_DEAD)) {
2185		e = (events & (POLLOUT | POLLWRNORM));
2186		if (e)
2187			ret |= chn_poll(wrch, e, td);
2188	}
2189
2190	if (rdch != NULL && !(rdch->flags & CHN_F_DEAD)) {
2191		e = (events & (POLLIN | POLLRDNORM));
2192		if (e)
2193			ret |= chn_poll(rdch, e, td);
2194	}
2195
2196	relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2197
2198	PCM_GIANT_LEAVE(d);
2199
2200	return (ret);
2201}
2202
2203static int
2204dsp_mmap(struct cdev *i_dev, vm_ooffset_t offset, vm_paddr_t *paddr,
2205    int nprot, vm_memattr_t *memattr)
2206{
2207
2208	/* XXX memattr is not honored */
2209	*paddr = vtophys(offset);
2210	return (0);
2211}
2212
2213static int
2214dsp_mmap_single(struct cdev *i_dev, vm_ooffset_t *offset,
2215    vm_size_t size, struct vm_object **object, int nprot)
2216{
2217	struct snddev_info *d;
2218	struct pcm_channel *wrch, *rdch, *c;
2219
2220	/*
2221	 * Reject PROT_EXEC by default. It just doesn't makes sense.
2222	 * Unfortunately, we have to give up this one due to linux_mmap
2223	 * changes.
2224	 *
2225	 * http://lists.freebsd.org/pipermail/freebsd-emulation/2007-June/003698.html
2226	 *
2227	 */
2228#ifdef SV_ABI_LINUX
2229	if ((nprot & PROT_EXEC) && (dsp_mmap_allow_prot_exec < 0 ||
2230	    (dsp_mmap_allow_prot_exec == 0 &&
2231	    SV_CURPROC_ABI() != SV_ABI_LINUX)))
2232#else
2233	if ((nprot & PROT_EXEC) && dsp_mmap_allow_prot_exec < 1)
2234#endif
2235		return (EINVAL);
2236
2237	/*
2238	 * PROT_READ (alone) selects the input buffer.
2239	 * PROT_WRITE (alone) selects the output buffer.
2240	 * PROT_WRITE|PROT_READ together select the output buffer.
2241	 */
2242	if ((nprot & (PROT_READ | PROT_WRITE)) == 0)
2243		return (EINVAL);
2244
2245	d = dsp_get_info(i_dev);
2246	if (!DSP_REGISTERED(d, i_dev))
2247		return (EINVAL);
2248
2249	PCM_GIANT_ENTER(d);
2250
2251	getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2252
2253	c = ((nprot & PROT_WRITE) != 0) ? wrch : rdch;
2254	if (c == NULL || (c->flags & CHN_F_MMAP_INVALID) ||
2255	    (*offset  + size) > sndbuf_getsize(c->bufsoft) ||
2256	    (wrch != NULL && (wrch->flags & CHN_F_MMAP_INVALID)) ||
2257	    (rdch != NULL && (rdch->flags & CHN_F_MMAP_INVALID))) {
2258		relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2259		PCM_GIANT_EXIT(d);
2260		return (EINVAL);
2261	}
2262
2263	if (wrch != NULL)
2264		wrch->flags |= CHN_F_MMAP;
2265	if (rdch != NULL)
2266		rdch->flags |= CHN_F_MMAP;
2267
2268	*offset = (uintptr_t)sndbuf_getbufofs(c->bufsoft, *offset);
2269	relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2270	*object = vm_pager_allocate(OBJT_DEVICE, i_dev,
2271	    size, nprot, *offset, curthread->td_ucred);
2272
2273	PCM_GIANT_LEAVE(d);
2274
2275	if (*object == NULL)
2276		 return (EINVAL);
2277	return (0);
2278}
2279
2280/* So much for dev_stdclone() */
2281static int
2282dsp_stdclone(char *name, char *namep, char *sep, int use_sep, int *u, int *c)
2283{
2284	size_t len;
2285
2286	len = strlen(namep);
2287
2288	if (bcmp(name, namep, len) != 0)
2289		return (ENODEV);
2290
2291	name += len;
2292
2293	if (isdigit(*name) == 0)
2294		return (ENODEV);
2295
2296	len = strlen(sep);
2297
2298	if (*name == '0' && !(name[1] == '\0' || bcmp(name + 1, sep, len) == 0))
2299		return (ENODEV);
2300
2301	for (*u = 0; isdigit(*name) != 0; name++) {
2302		*u *= 10;
2303		*u += *name - '0';
2304		if (*u > dsp_umax)
2305			return (ENODEV);
2306	}
2307
2308	if (*name == '\0')
2309		return ((use_sep == 0) ? 0 : ENODEV);
2310
2311	if (bcmp(name, sep, len) != 0 || isdigit(name[len]) == 0)
2312		return (ENODEV);
2313
2314	name += len;
2315
2316	if (*name == '0' && name[1] != '\0')
2317		return (ENODEV);
2318
2319	for (*c = 0; isdigit(*name) != 0; name++) {
2320		*c *= 10;
2321		*c += *name - '0';
2322		if (*c > dsp_cmax)
2323			return (ENODEV);
2324	}
2325
2326	if (*name != '\0')
2327		return (ENODEV);
2328
2329	return (0);
2330}
2331
2332static void
2333dsp_clone(void *arg,
2334    struct ucred *cred,
2335    char *name, int namelen, struct cdev **dev)
2336{
2337	struct snddev_info *d;
2338	struct snd_clone_entry *ce;
2339	struct pcm_channel *c;
2340	int i, unit, udcmask, cunit, devtype, devhw, devcmax, tumax;
2341	char *devname, *devcmp, *devsep;
2342
2343	KASSERT(dsp_umax >= 0 && dsp_cmax >= 0, ("Uninitialized unit!"));
2344
2345	if (*dev != NULL)
2346		return;
2347
2348	unit = -1;
2349	cunit = -1;
2350	devtype = -1;
2351	devhw = 0;
2352	devcmax = -1;
2353	tumax = -1;
2354	devname = NULL;
2355	devsep = NULL;
2356
2357	for (i = 0; unit == -1 &&
2358	    i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2359		devtype = dsp_cdevs[i].type;
2360		devcmp = dsp_cdevs[i].name;
2361		devsep = dsp_cdevs[i].sep;
2362		devname = dsp_cdevs[i].alias;
2363		if (devname == NULL)
2364			devname = devcmp;
2365		devhw = dsp_cdevs[i].hw;
2366		devcmax = dsp_cdevs[i].max - 1;
2367		if (strcmp(name, devcmp) == 0) {
2368			if (dsp_basename_clone != 0)
2369				unit = snd_unit;
2370		} else if (dsp_stdclone(name, devcmp, devsep,
2371		    dsp_cdevs[i].use_sep, &unit, &cunit) != 0) {
2372			unit = -1;
2373			cunit = -1;
2374		}
2375	}
2376
2377	d = devclass_get_softc(pcm_devclass, unit);
2378	if (!PCM_REGISTERED(d) || d->clones == NULL)
2379		return;
2380
2381	/* XXX Need Giant magic entry ??? */
2382
2383	PCM_LOCK(d);
2384	if (snd_clone_disabled(d->clones)) {
2385		PCM_UNLOCK(d);
2386		return;
2387	}
2388
2389	PCM_WAIT(d);
2390	PCM_ACQUIRE(d);
2391	PCM_UNLOCK(d);
2392
2393	udcmask = snd_u2unit(unit) | snd_d2unit(devtype);
2394
2395	if (devhw != 0) {
2396		KASSERT(devcmax <= dsp_cmax,
2397		    ("overflow: devcmax=%d, dsp_cmax=%d", devcmax, dsp_cmax));
2398		if (cunit > devcmax) {
2399			PCM_RELEASE_QUICK(d);
2400			return;
2401		}
2402		udcmask |= snd_c2unit(cunit);
2403		CHN_FOREACH(c, d, channels.pcm) {
2404			CHN_LOCK(c);
2405			if (c->unit != udcmask) {
2406				CHN_UNLOCK(c);
2407				continue;
2408			}
2409			CHN_UNLOCK(c);
2410			udcmask &= ~snd_c2unit(cunit);
2411			/*
2412			 * Temporarily increase clone maxunit to overcome
2413			 * vchan flexibility.
2414			 *
2415			 * # sysctl dev.pcm.0.play.vchans=256
2416			 * dev.pcm.0.play.vchans: 1 -> 256
2417			 * # cat /dev/zero > /dev/dsp0.vp255 &
2418			 * [1] 17296
2419			 * # sysctl dev.pcm.0.play.vchans=0
2420			 * dev.pcm.0.play.vchans: 256 -> 1
2421			 * # fg
2422			 * [1]  + running    cat /dev/zero > /dev/dsp0.vp255
2423			 * ^C
2424			 * # cat /dev/zero > /dev/dsp0.vp255
2425			 * zsh: operation not supported: /dev/dsp0.vp255
2426			 */
2427			tumax = snd_clone_getmaxunit(d->clones);
2428			if (cunit > tumax)
2429				snd_clone_setmaxunit(d->clones, cunit);
2430			else
2431				tumax = -1;
2432			goto dsp_clone_alloc;
2433		}
2434		/*
2435		 * Ok, so we're requesting unallocated vchan, but still
2436		 * within maximum vchan limit.
2437		 */
2438		if (((devtype == SND_DEV_DSPHW_VPLAY && d->pvchancount > 0) ||
2439		    (devtype == SND_DEV_DSPHW_VREC && d->rvchancount > 0)) &&
2440		    cunit < snd_maxautovchans) {
2441			udcmask &= ~snd_c2unit(cunit);
2442			tumax = snd_clone_getmaxunit(d->clones);
2443			if (cunit > tumax)
2444				snd_clone_setmaxunit(d->clones, cunit);
2445			else
2446				tumax = -1;
2447			goto dsp_clone_alloc;
2448		}
2449		PCM_RELEASE_QUICK(d);
2450		return;
2451	}
2452
2453dsp_clone_alloc:
2454	ce = snd_clone_alloc(d->clones, dev, &cunit, udcmask);
2455	if (tumax != -1)
2456		snd_clone_setmaxunit(d->clones, tumax);
2457	if (ce != NULL) {
2458		udcmask |= snd_c2unit(cunit);
2459		*dev = make_dev(&dsp_cdevsw, PCMMINOR(udcmask),
2460		    UID_ROOT, GID_WHEEL, 0666, "%s%d%s%d",
2461		    devname, unit, devsep, cunit);
2462		snd_clone_register(ce, *dev);
2463	}
2464
2465	PCM_RELEASE_QUICK(d);
2466
2467	if (*dev != NULL)
2468		dev_ref(*dev);
2469}
2470
2471static void
2472dsp_sysinit(void *p)
2473{
2474	if (dsp_ehtag != NULL)
2475		return;
2476	/* initialize unit numbering */
2477	snd_unit_init();
2478	dsp_umax = PCMMAXUNIT;
2479	dsp_cmax = PCMMAXCHAN;
2480	dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
2481}
2482
2483static void
2484dsp_sysuninit(void *p)
2485{
2486	if (dsp_ehtag == NULL)
2487		return;
2488	EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
2489	dsp_ehtag = NULL;
2490}
2491
2492SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
2493SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
2494
2495char *
2496dsp_unit2name(char *buf, size_t len, int unit)
2497{
2498	int i, dtype;
2499
2500	KASSERT(buf != NULL && len != 0,
2501	    ("bogus buf=%p len=%ju", buf, (uintmax_t)len));
2502
2503	dtype = snd_unit2d(unit);
2504
2505	for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2506		if (dtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
2507			continue;
2508		snprintf(buf, len, "%s%d%s%d", dsp_cdevs[i].name,
2509		    snd_unit2u(unit), dsp_cdevs[i].sep, snd_unit2c(unit));
2510		return (buf);
2511	}
2512
2513	return (NULL);
2514}
2515
2516/**
2517 * @brief Handler for SNDCTL_AUDIOINFO.
2518 *
2519 * Gathers information about the audio device specified in ai->dev.  If
2520 * ai->dev == -1, then this function gathers information about the current
2521 * device.  If the call comes in on a non-audio device and ai->dev == -1,
2522 * return EINVAL.
2523 *
2524 * This routine is supposed to go practically straight to the hardware,
2525 * getting capabilities directly from the sound card driver, side-stepping
2526 * the intermediate channel interface.
2527 *
2528 * Note, however, that the usefulness of this command is significantly
2529 * decreased when requesting info about any device other than the one serving
2530 * the request. While each snddev_channel refers to a specific device node,
2531 * the converse is *not* true.  Currently, when a sound device node is opened,
2532 * the sound subsystem scans for an available audio channel (or channels, if
2533 * opened in read+write) and then assigns them to the si_drv[12] private
2534 * data fields.  As a result, any information returned linking a channel to
2535 * a specific character device isn't necessarily accurate.
2536 *
2537 * @note
2538 * Calling threads must not hold any snddev_info or pcm_channel locks.
2539 *
2540 * @param dev		device on which the ioctl was issued
2541 * @param ai		ioctl request data container
2542 *
2543 * @retval 0		success
2544 * @retval EINVAL	ai->dev specifies an invalid device
2545 *
2546 * @todo Verify correctness of Doxygen tags.  ;)
2547 */
2548int
2549dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
2550{
2551	struct pcmchan_caps *caps;
2552	struct pcm_channel *ch;
2553	struct snddev_info *d;
2554	uint32_t fmts;
2555	int i, nchan, *rates, minch, maxch;
2556	char *devname, buf[CHN_NAMELEN];
2557
2558	/*
2559	 * If probing the device that received the ioctl, make sure it's a
2560	 * DSP device.  (Users may use this ioctl with /dev/mixer and
2561	 * /dev/midi.)
2562	 */
2563	if (ai->dev == -1 && i_dev->si_devsw != &dsp_cdevsw)
2564		return (EINVAL);
2565
2566	ch = NULL;
2567	devname = NULL;
2568	nchan = 0;
2569	bzero(buf, sizeof(buf));
2570
2571	/*
2572	 * Search for the requested audio device (channel).  Start by
2573	 * iterating over pcm devices.
2574	 */
2575	for (i = 0; pcm_devclass != NULL &&
2576	    i < devclass_get_maxunit(pcm_devclass); i++) {
2577		d = devclass_get_softc(pcm_devclass, i);
2578		if (!PCM_REGISTERED(d))
2579			continue;
2580
2581		/* XXX Need Giant magic entry ??? */
2582
2583		/* See the note in function docblock */
2584		PCM_UNLOCKASSERT(d);
2585		PCM_LOCK(d);
2586
2587		CHN_FOREACH(ch, d, channels.pcm) {
2588			CHN_UNLOCKASSERT(ch);
2589			CHN_LOCK(ch);
2590			if (ai->dev == -1) {
2591				if (DSP_REGISTERED(d, i_dev) &&
2592				    (ch == PCM_RDCH(i_dev) ||	/* record ch */
2593				    ch == PCM_WRCH(i_dev))) {	/* playback ch */
2594					devname = dsp_unit2name(buf,
2595					    sizeof(buf), ch->unit);
2596				}
2597			} else if (ai->dev == nchan) {
2598				devname = dsp_unit2name(buf, sizeof(buf),
2599				    ch->unit);
2600			}
2601			if (devname != NULL)
2602				break;
2603			CHN_UNLOCK(ch);
2604			++nchan;
2605		}
2606
2607		if (devname != NULL) {
2608			/*
2609			 * At this point, the following synchronization stuff
2610			 * has happened:
2611			 * - a specific PCM device is locked.
2612			 * - a specific audio channel has been locked, so be
2613			 *   sure to unlock when exiting;
2614			 */
2615
2616			caps = chn_getcaps(ch);
2617
2618			/*
2619			 * With all handles collected, zero out the user's
2620			 * container and begin filling in its fields.
2621			 */
2622			bzero((void *)ai, sizeof(oss_audioinfo));
2623
2624			ai->dev = nchan;
2625			strlcpy(ai->name, ch->name,  sizeof(ai->name));
2626
2627			if ((ch->flags & CHN_F_BUSY) == 0)
2628				ai->busy = 0;
2629			else
2630				ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
2631
2632			/**
2633			 * @note
2634			 * @c cmd - OSSv4 docs: "Only supported under Linux at
2635			 *    this moment." Cop-out, I know, but I'll save
2636			 *    running around in the process table for later.
2637			 *    Is there a risk of leaking information?
2638			 */
2639			ai->pid = ch->pid;
2640
2641			/*
2642			 * These flags stolen from SNDCTL_DSP_GETCAPS handler.
2643			 * Note, however, that a single channel operates in
2644			 * only one direction, so PCM_CAP_DUPLEX is out.
2645			 */
2646			/**
2647			 * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep
2648			 *       these in pcmchan::caps?
2649			 */
2650			ai->caps = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER |
2651			    ((ch->direction == PCMDIR_PLAY) ? PCM_CAP_OUTPUT : PCM_CAP_INPUT);
2652
2653			/*
2654			 * Collect formats supported @b natively by the
2655			 * device.  Also determine min/max channels.  (I.e.,
2656			 * mono, stereo, or both?)
2657			 *
2658			 * If any channel is stereo, maxch = 2;
2659			 * if all channels are stereo, minch = 2, too;
2660			 * if any channel is mono, minch = 1;
2661			 * and if all channels are mono, maxch = 1.
2662			 */
2663			minch = 0;
2664			maxch = 0;
2665			fmts = 0;
2666			for (i = 0; caps->fmtlist[i]; i++) {
2667				fmts |= caps->fmtlist[i];
2668				if (AFMT_CHANNEL(caps->fmtlist[i]) > 1) {
2669					minch = (minch == 0) ? 2 : minch;
2670					maxch = 2;
2671				} else {
2672					minch = 1;
2673					maxch = (maxch == 0) ? 1 : maxch;
2674				}
2675			}
2676
2677			if (ch->direction == PCMDIR_PLAY)
2678				ai->oformats = fmts;
2679			else
2680				ai->iformats = fmts;
2681
2682			/**
2683			 * @note
2684			 * @c magic - OSSv4 docs: "Reserved for internal use
2685			 *    by OSS."
2686			 *
2687			 * @par
2688			 * @c card_number - OSSv4 docs: "Number of the sound
2689			 *    card where this device belongs or -1 if this
2690			 *    information is not available.  Applications
2691			 *    should normally not use this field for any
2692			 *    purpose."
2693			 */
2694			ai->card_number = -1;
2695			/**
2696			 * @todo @c song_name - depends first on
2697			 *          SNDCTL_[GS]ETSONG @todo @c label - depends
2698			 *          on SNDCTL_[GS]ETLABEL
2699			 * @todo @c port_number - routing information?
2700			 */
2701			ai->port_number = -1;
2702			ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1;
2703			/**
2704			 * @note
2705			 * @c real_device - OSSv4 docs:  "Obsolete."
2706			 */
2707			ai->real_device = -1;
2708			strlcpy(ai->devnode, "/dev/", sizeof(ai->devnode));
2709			strlcat(ai->devnode, devname, sizeof(ai->devnode));
2710			ai->enabled = device_is_attached(d->dev) ? 1 : 0;
2711			/**
2712			 * @note
2713			 * @c flags - OSSv4 docs: "Reserved for future use."
2714			 *
2715			 * @note
2716			 * @c binding - OSSv4 docs: "Reserved for future use."
2717			 *
2718			 * @todo @c handle - haven't decided how to generate
2719			 *       this yet; bus, vendor, device IDs?
2720			 */
2721			ai->min_rate = caps->minspeed;
2722			ai->max_rate = caps->maxspeed;
2723
2724			ai->min_channels = minch;
2725			ai->max_channels = maxch;
2726
2727			ai->nrates = chn_getrates(ch, &rates);
2728			if (ai->nrates > OSS_MAX_SAMPLE_RATES)
2729				ai->nrates = OSS_MAX_SAMPLE_RATES;
2730
2731			for (i = 0; i < ai->nrates; i++)
2732				ai->rates[i] = rates[i];
2733
2734			ai->next_play_engine = 0;
2735			ai->next_rec_engine = 0;
2736
2737			CHN_UNLOCK(ch);
2738		}
2739
2740		PCM_UNLOCK(d);
2741
2742		if (devname != NULL)
2743			return (0);
2744	}
2745
2746	/* Exhausted the search -- nothing is locked, so return. */
2747	return (EINVAL);
2748}
2749
2750/**
2751 * @brief Assigns a PCM channel to a sync group.
2752 *
2753 * Sync groups are used to enable audio operations on multiple devices
2754 * simultaneously.  They may be used with any number of devices and may
2755 * span across applications.  Devices are added to groups with
2756 * the SNDCTL_DSP_SYNCGROUP ioctl, and operations are triggered with the
2757 * SNDCTL_DSP_SYNCSTART ioctl.
2758 *
2759 * If the @c id field of the @c group parameter is set to zero, then a new
2760 * sync group is created.  Otherwise, wrch and rdch (if set) are added to
2761 * the group specified.
2762 *
2763 * @todo As far as memory allocation, should we assume that things are
2764 * 	 okay and allocate with M_WAITOK before acquiring channel locks,
2765 * 	 freeing later if not?
2766 *
2767 * @param wrch	output channel associated w/ device (if any)
2768 * @param rdch	input channel associated w/ device (if any)
2769 * @param group Sync group parameters
2770 *
2771 * @retval 0		success
2772 * @retval non-zero	error to be propagated upstream
2773 */
2774static int
2775dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
2776{
2777	struct pcmchan_syncmember *smrd, *smwr;
2778	struct pcmchan_syncgroup *sg;
2779	int ret, sg_ids[3];
2780
2781	smrd = NULL;
2782	smwr = NULL;
2783	sg = NULL;
2784	ret = 0;
2785
2786	/*
2787	 * Free_unr() may sleep, so store released syncgroup IDs until after
2788	 * all locks are released.
2789	 */
2790	sg_ids[0] = sg_ids[1] = sg_ids[2] = 0;
2791
2792	PCM_SG_LOCK();
2793
2794	/*
2795	 * - Insert channel(s) into group's member list.
2796	 * - Set CHN_F_NOTRIGGER on channel(s).
2797	 * - Stop channel(s).
2798	 */
2799
2800	/*
2801	 * If device's channels are already mapped to a group, unmap them.
2802	 */
2803	if (wrch) {
2804		CHN_LOCK(wrch);
2805		sg_ids[0] = chn_syncdestroy(wrch);
2806	}
2807
2808	if (rdch) {
2809		CHN_LOCK(rdch);
2810		sg_ids[1] = chn_syncdestroy(rdch);
2811	}
2812
2813	/*
2814	 * Verify that mode matches character device properites.
2815	 *  - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
2816	 *  - Bail if PCM_ENABLE_INPUT && rdch == NULL.
2817	 */
2818	if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) ||
2819	    ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) {
2820		ret = EINVAL;
2821		goto out;
2822	}
2823
2824	/*
2825	 * An id of zero indicates the user wants to create a new
2826	 * syncgroup.
2827	 */
2828	if (group->id == 0) {
2829		sg = (struct pcmchan_syncgroup *)malloc(sizeof(*sg), M_DEVBUF, M_NOWAIT);
2830		if (sg != NULL) {
2831			SLIST_INIT(&sg->members);
2832			sg->id = alloc_unr(pcmsg_unrhdr);
2833
2834			group->id = sg->id;
2835			SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link);
2836		} else
2837			ret = ENOMEM;
2838	} else {
2839		SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2840			if (sg->id == group->id)
2841				break;
2842		}
2843		if (sg == NULL)
2844			ret = EINVAL;
2845	}
2846
2847	/* Couldn't create or find a syncgroup.  Fail. */
2848	if (sg == NULL)
2849		goto out;
2850
2851	/*
2852	 * Allocate a syncmember, assign it and a channel together, and
2853	 * insert into syncgroup.
2854	 */
2855	if (group->mode & PCM_ENABLE_INPUT) {
2856		smrd = (struct pcmchan_syncmember *)malloc(sizeof(*smrd), M_DEVBUF, M_NOWAIT);
2857		if (smrd == NULL) {
2858			ret = ENOMEM;
2859			goto out;
2860		}
2861
2862		SLIST_INSERT_HEAD(&sg->members, smrd, link);
2863		smrd->parent = sg;
2864		smrd->ch = rdch;
2865
2866		chn_abort(rdch);
2867		rdch->flags |= CHN_F_NOTRIGGER;
2868		rdch->sm = smrd;
2869	}
2870
2871	if (group->mode & PCM_ENABLE_OUTPUT) {
2872		smwr = (struct pcmchan_syncmember *)malloc(sizeof(*smwr), M_DEVBUF, M_NOWAIT);
2873		if (smwr == NULL) {
2874			ret = ENOMEM;
2875			goto out;
2876		}
2877
2878		SLIST_INSERT_HEAD(&sg->members, smwr, link);
2879		smwr->parent = sg;
2880		smwr->ch = wrch;
2881
2882		chn_abort(wrch);
2883		wrch->flags |= CHN_F_NOTRIGGER;
2884		wrch->sm = smwr;
2885	}
2886
2887
2888out:
2889	if (ret != 0) {
2890		if (smrd != NULL)
2891			free(smrd, M_DEVBUF);
2892		if ((sg != NULL) && SLIST_EMPTY(&sg->members)) {
2893			sg_ids[2] = sg->id;
2894			SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2895			free(sg, M_DEVBUF);
2896		}
2897
2898		if (wrch)
2899			wrch->sm = NULL;
2900		if (rdch)
2901			rdch->sm = NULL;
2902	}
2903
2904	if (wrch)
2905		CHN_UNLOCK(wrch);
2906	if (rdch)
2907		CHN_UNLOCK(rdch);
2908
2909	PCM_SG_UNLOCK();
2910
2911	if (sg_ids[0])
2912		free_unr(pcmsg_unrhdr, sg_ids[0]);
2913	if (sg_ids[1])
2914		free_unr(pcmsg_unrhdr, sg_ids[1]);
2915	if (sg_ids[2])
2916		free_unr(pcmsg_unrhdr, sg_ids[2]);
2917
2918	return (ret);
2919}
2920
2921/**
2922 * @brief Launch a sync group into action
2923 *
2924 * Sync groups are established via SNDCTL_DSP_SYNCGROUP.  This function
2925 * iterates over all members, triggering them along the way.
2926 *
2927 * @note Caller must not hold any channel locks.
2928 *
2929 * @param sg_id	sync group identifier
2930 *
2931 * @retval 0	success
2932 * @retval non-zero	error worthy of propagating upstream to user
2933 */
2934static int
2935dsp_oss_syncstart(int sg_id)
2936{
2937	struct pcmchan_syncmember *sm, *sm_tmp;
2938	struct pcmchan_syncgroup *sg;
2939	struct pcm_channel *c;
2940	int ret, needlocks;
2941
2942	/* Get the synclists lock */
2943	PCM_SG_LOCK();
2944
2945	do {
2946		ret = 0;
2947		needlocks = 0;
2948
2949		/* Search for syncgroup by ID */
2950		SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2951			if (sg->id == sg_id)
2952				break;
2953		}
2954
2955		/* Return EINVAL if not found */
2956		if (sg == NULL) {
2957			ret = EINVAL;
2958			break;
2959		}
2960
2961		/* Any removals resulting in an empty group should've handled this */
2962		KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup"));
2963
2964		/*
2965		 * Attempt to lock all member channels - if any are already
2966		 * locked, unlock those acquired, sleep for a bit, and try
2967		 * again.
2968		 */
2969		SLIST_FOREACH(sm, &sg->members, link) {
2970			if (CHN_TRYLOCK(sm->ch) == 0) {
2971				int timo = hz * 5/1000;
2972				if (timo < 1)
2973					timo = 1;
2974
2975				/* Release all locked channels so far, retry */
2976				SLIST_FOREACH(sm_tmp, &sg->members, link) {
2977					/* sm is the member already locked */
2978					if (sm == sm_tmp)
2979						break;
2980					CHN_UNLOCK(sm_tmp->ch);
2981				}
2982
2983				/** @todo Is PRIBIO correct/ */
2984				ret = msleep(sm, &snd_pcm_syncgroups_mtx,
2985				    PRIBIO | PCATCH, "pcmsg", timo);
2986				if (ret == EINTR || ret == ERESTART)
2987					break;
2988
2989				needlocks = 1;
2990				ret = 0; /* Assumes ret == EAGAIN... */
2991			}
2992		}
2993	} while (needlocks && ret == 0);
2994
2995	/* Proceed only if no errors encountered. */
2996	if (ret == 0) {
2997		/* Launch channels */
2998		while ((sm = SLIST_FIRST(&sg->members)) != NULL) {
2999			SLIST_REMOVE_HEAD(&sg->members, link);
3000
3001			c = sm->ch;
3002			c->sm = NULL;
3003			chn_start(c, 1);
3004			c->flags &= ~CHN_F_NOTRIGGER;
3005			CHN_UNLOCK(c);
3006
3007			free(sm, M_DEVBUF);
3008		}
3009
3010		SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
3011		free(sg, M_DEVBUF);
3012	}
3013
3014	PCM_SG_UNLOCK();
3015
3016	/*
3017	 * Free_unr() may sleep, so be sure to give up the syncgroup lock
3018	 * first.
3019	 */
3020	if (ret == 0)
3021		free_unr(pcmsg_unrhdr, sg_id);
3022
3023	return (ret);
3024}
3025
3026/**
3027 * @brief Handler for SNDCTL_DSP_POLICY
3028 *
3029 * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment
3030 * size and count like with SNDCTL_DSP_SETFRAGMENT.  Instead of the user
3031 * specifying those two parameters, s/he simply selects a number from 0..10
3032 * which corresponds to a buffer size.  Smaller numbers request smaller
3033 * buffers with lower latencies (at greater overhead from more frequent
3034 * interrupts), while greater numbers behave in the opposite manner.
3035 *
3036 * The 4Front spec states that a value of 5 should be the default.  However,
3037 * this implementation deviates slightly by using a linear scale without
3038 * consulting drivers.  I.e., even though drivers may have different default
3039 * buffer sizes, a policy argument of 5 will have the same result across
3040 * all drivers.
3041 *
3042 * See http://manuals.opensound.com/developer/SNDCTL_DSP_POLICY.html for
3043 * more information.
3044 *
3045 * @todo When SNDCTL_DSP_COOKEDMODE is supported, it'll be necessary to
3046 * 	 work with hardware drivers directly.
3047 *
3048 * @note PCM channel arguments must not be locked by caller.
3049 *
3050 * @param wrch	Pointer to opened playback channel (optional; may be NULL)
3051 * @param rdch	" recording channel (optional; may be NULL)
3052 * @param policy Integer from [0:10]
3053 *
3054 * @retval 0	constant (for now)
3055 */
3056static int
3057dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
3058{
3059	int ret;
3060
3061	if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX)
3062		return (EIO);
3063
3064	/* Default: success */
3065	ret = 0;
3066
3067	if (rdch) {
3068		CHN_LOCK(rdch);
3069		ret = chn_setlatency(rdch, policy);
3070		CHN_UNLOCK(rdch);
3071	}
3072
3073	if (wrch && ret == 0) {
3074		CHN_LOCK(wrch);
3075		ret = chn_setlatency(wrch, policy);
3076		CHN_UNLOCK(wrch);
3077	}
3078
3079	if (ret)
3080		ret = EIO;
3081
3082	return (ret);
3083}
3084
3085/**
3086 * @brief Enable or disable "cooked" mode
3087 *
3088 * This is a handler for @c SNDCTL_DSP_COOKEDMODE.  When in cooked mode, which
3089 * is the default, the sound system handles rate and format conversions
3090 * automatically (ex: user writing 11025Hz/8 bit/unsigned but card only
3091 * operates with 44100Hz/16bit/signed samples).
3092 *
3093 * Disabling cooked mode is intended for applications wanting to mmap()
3094 * a sound card's buffer space directly, bypassing the FreeBSD 2-stage
3095 * feeder architecture, presumably to gain as much control over audio
3096 * hardware as possible.
3097 *
3098 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_COOKEDMODE.html
3099 * for more details.
3100 *
3101 * @param wrch		playback channel (optional; may be NULL)
3102 * @param rdch		recording channel (optional; may be NULL)
3103 * @param enabled	0 = raw mode, 1 = cooked mode
3104 *
3105 * @retval EINVAL	Operation not yet supported.
3106 */
3107static int
3108dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
3109{
3110
3111	/*
3112	 * XXX I just don't get it. Why don't they call it
3113	 * "BITPERFECT" ~ SNDCTL_DSP_BITPERFECT !?!?.
3114	 * This is just plain so confusing, incoherent,
3115	 * <insert any non-printable characters here>.
3116	 */
3117	if (!(enabled == 1 || enabled == 0))
3118		return (EINVAL);
3119
3120	/*
3121	 * I won't give in. I'm inverting its logic here and now.
3122	 * Brag all you want, but "BITPERFECT" should be the better
3123	 * term here.
3124	 */
3125	enabled ^= 0x00000001;
3126
3127	if (wrch != NULL) {
3128		CHN_LOCK(wrch);
3129		wrch->flags &= ~CHN_F_BITPERFECT;
3130		wrch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3131		CHN_UNLOCK(wrch);
3132	}
3133
3134	if (rdch != NULL) {
3135		CHN_LOCK(rdch);
3136		rdch->flags &= ~CHN_F_BITPERFECT;
3137		rdch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3138		CHN_UNLOCK(rdch);
3139	}
3140
3141	return (0);
3142}
3143
3144/**
3145 * @brief Retrieve channel interleaving order
3146 *
3147 * This is the handler for @c SNDCTL_DSP_GET_CHNORDER.
3148 *
3149 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html
3150 * for more details.
3151 *
3152 * @note As the ioctl definition is still under construction, FreeBSD
3153 * 	 does not currently support SNDCTL_DSP_GET_CHNORDER.
3154 *
3155 * @param wrch	playback channel (optional; may be NULL)
3156 * @param rdch	recording channel (optional; may be NULL)
3157 * @param map	channel map (result will be stored there)
3158 *
3159 * @retval EINVAL	Operation not yet supported.
3160 */
3161static int
3162dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3163{
3164	struct pcm_channel *ch;
3165	int ret;
3166
3167	ch = (wrch != NULL) ? wrch : rdch;
3168	if (ch != NULL) {
3169		CHN_LOCK(ch);
3170		ret = chn_oss_getorder(ch, map);
3171		CHN_UNLOCK(ch);
3172	} else
3173		ret = EINVAL;
3174
3175	return (ret);
3176}
3177
3178/**
3179 * @brief Specify channel interleaving order
3180 *
3181 * This is the handler for @c SNDCTL_DSP_SET_CHNORDER.
3182 *
3183 * @note As the ioctl definition is still under construction, FreeBSD
3184 * 	 does not currently support @c SNDCTL_DSP_SET_CHNORDER.
3185 *
3186 * @param wrch	playback channel (optional; may be NULL)
3187 * @param rdch	recording channel (optional; may be NULL)
3188 * @param map	channel map
3189 *
3190 * @retval EINVAL	Operation not yet supported.
3191 */
3192static int
3193dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3194{
3195	int ret;
3196
3197	ret = 0;
3198
3199	if (wrch != NULL) {
3200		CHN_LOCK(wrch);
3201		ret = chn_oss_setorder(wrch, map);
3202		CHN_UNLOCK(wrch);
3203	}
3204
3205	if (ret == 0 && rdch != NULL) {
3206		CHN_LOCK(rdch);
3207		ret = chn_oss_setorder(rdch, map);
3208		CHN_UNLOCK(rdch);
3209	}
3210
3211	return (ret);
3212}
3213
3214static int
3215dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch,
3216    int *mask)
3217{
3218	struct pcm_channel *ch;
3219	uint32_t chnmask;
3220	int ret;
3221
3222	chnmask = 0;
3223	ch = (wrch != NULL) ? wrch : rdch;
3224
3225	if (ch != NULL) {
3226		CHN_LOCK(ch);
3227		ret = chn_oss_getmask(ch, &chnmask);
3228		CHN_UNLOCK(ch);
3229	} else
3230		ret = EINVAL;
3231
3232	if (ret == 0)
3233		*mask = chnmask;
3234
3235	return (ret);
3236}
3237
3238#ifdef OSSV4_EXPERIMENT
3239/**
3240 * @brief Retrieve an audio device's label
3241 *
3242 * This is a handler for the @c SNDCTL_GETLABEL ioctl.
3243 *
3244 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3245 * for more details.
3246 *
3247 * From Hannu@4Front:  "For example ossxmix (just like some HW mixer
3248 * consoles) can show variable "labels" for certain controls. By default
3249 * the application name (say quake) is shown as the label but
3250 * applications may change the labels themselves."
3251 *
3252 * @note As the ioctl definition is still under construction, FreeBSD
3253 * 	 does not currently support @c SNDCTL_GETLABEL.
3254 *
3255 * @param wrch	playback channel (optional; may be NULL)
3256 * @param rdch	recording channel (optional; may be NULL)
3257 * @param label	label gets copied here
3258 *
3259 * @retval EINVAL	Operation not yet supported.
3260 */
3261static int
3262dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3263{
3264	return (EINVAL);
3265}
3266
3267/**
3268 * @brief Specify an audio device's label
3269 *
3270 * This is a handler for the @c SNDCTL_SETLABEL ioctl.  Please see the
3271 * comments for @c dsp_oss_getlabel immediately above.
3272 *
3273 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
3274 * for more details.
3275 *
3276 * @note As the ioctl definition is still under construction, FreeBSD
3277 * 	 does not currently support SNDCTL_SETLABEL.
3278 *
3279 * @param wrch	playback channel (optional; may be NULL)
3280 * @param rdch	recording channel (optional; may be NULL)
3281 * @param label	label gets copied from here
3282 *
3283 * @retval EINVAL	Operation not yet supported.
3284 */
3285static int
3286dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3287{
3288	return (EINVAL);
3289}
3290
3291/**
3292 * @brief Retrieve name of currently played song
3293 *
3294 * This is a handler for the @c SNDCTL_GETSONG ioctl.  Audio players could
3295 * tell the system the name of the currently playing song, which would be
3296 * visible in @c /dev/sndstat.
3297 *
3298 * See @c http://manuals.opensound.com/developer/SNDCTL_GETSONG.html
3299 * for more details.
3300 *
3301 * @note As the ioctl definition is still under construction, FreeBSD
3302 * 	 does not currently support SNDCTL_GETSONG.
3303 *
3304 * @param wrch	playback channel (optional; may be NULL)
3305 * @param rdch	recording channel (optional; may be NULL)
3306 * @param song	song name gets copied here
3307 *
3308 * @retval EINVAL	Operation not yet supported.
3309 */
3310static int
3311dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3312{
3313	return (EINVAL);
3314}
3315
3316/**
3317 * @brief Retrieve name of currently played song
3318 *
3319 * This is a handler for the @c SNDCTL_SETSONG ioctl.  Audio players could
3320 * tell the system the name of the currently playing song, which would be
3321 * visible in @c /dev/sndstat.
3322 *
3323 * See @c http://manuals.opensound.com/developer/SNDCTL_SETSONG.html
3324 * for more details.
3325 *
3326 * @note As the ioctl definition is still under construction, FreeBSD
3327 * 	 does not currently support SNDCTL_SETSONG.
3328 *
3329 * @param wrch	playback channel (optional; may be NULL)
3330 * @param rdch	recording channel (optional; may be NULL)
3331 * @param song	song name gets copied from here
3332 *
3333 * @retval EINVAL	Operation not yet supported.
3334 */
3335static int
3336dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3337{
3338	return (EINVAL);
3339}
3340
3341/**
3342 * @brief Rename a device
3343 *
3344 * This is a handler for the @c SNDCTL_SETNAME ioctl.
3345 *
3346 * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for
3347 * more details.
3348 *
3349 * From Hannu@4Front:  "This call is used to change the device name
3350 * reported in /dev/sndstat and ossinfo. So instead of  using some generic
3351 * 'OSS loopback audio (MIDI) driver' the device may be given a meaningfull
3352 * name depending on the current context (for example 'OSS virtual wave table
3353 * synth' or 'VoIP link to London')."
3354 *
3355 * @note As the ioctl definition is still under construction, FreeBSD
3356 * 	 does not currently support SNDCTL_SETNAME.
3357 *
3358 * @param wrch	playback channel (optional; may be NULL)
3359 * @param rdch	recording channel (optional; may be NULL)
3360 * @param name	new device name gets copied from here
3361 *
3362 * @retval EINVAL	Operation not yet supported.
3363 */
3364static int
3365dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
3366{
3367	return (EINVAL);
3368}
3369#endif	/* !OSSV4_EXPERIMENT */
3370