xref: /freebsd/sys/dev/sound/pcm/sound.c (revision cc1efc23c8f94991ae65f43c9a311a7e43918480)
1098ca2bdSWarner Losh /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
490da2b28SAriff Abdullah  * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
590da2b28SAriff Abdullah  * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
690da2b28SAriff Abdullah  * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
740ac6e17SJoel Dahl  * Copyright (c) 1997 Luigi Rizzo
8987e5972SCameron Grant  * All rights reserved.
9987e5972SCameron Grant  *
10987e5972SCameron Grant  * Redistribution and use in source and binary forms, with or without
11987e5972SCameron Grant  * modification, are permitted provided that the following conditions
12987e5972SCameron Grant  * are met:
13987e5972SCameron Grant  * 1. Redistributions of source code must retain the above copyright
14987e5972SCameron Grant  *    notice, this list of conditions and the following disclaimer.
15987e5972SCameron Grant  * 2. Redistributions in binary form must reproduce the above copyright
16987e5972SCameron Grant  *    notice, this list of conditions and the following disclaimer in the
17987e5972SCameron Grant  *    documentation and/or other materials provided with the distribution.
18987e5972SCameron Grant  *
19987e5972SCameron Grant  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20987e5972SCameron Grant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21987e5972SCameron Grant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22987e5972SCameron Grant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23987e5972SCameron Grant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24987e5972SCameron Grant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25987e5972SCameron Grant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26987e5972SCameron Grant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27987e5972SCameron Grant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28987e5972SCameron Grant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29987e5972SCameron Grant  * SUCH DAMAGE.
30987e5972SCameron Grant  */
31987e5972SCameron Grant 
3290da2b28SAriff Abdullah #ifdef HAVE_KERNEL_OPTION_HEADERS
3390da2b28SAriff Abdullah #include "opt_snd.h"
3490da2b28SAriff Abdullah #endif
3590da2b28SAriff Abdullah 
36ef9308b1SCameron Grant #include <dev/sound/pcm/sound.h>
37a580b31aSAriff Abdullah #include <dev/sound/pcm/ac97.h>
38285648f9SCameron Grant #include <dev/sound/pcm/vchan.h>
395ee30e27SMathew Kanner #include <dev/sound/pcm/dsp.h>
4090da2b28SAriff Abdullah #include <dev/sound/pcm/sndstat.h>
41bba4862cSAriff Abdullah #include <dev/sound/version.h>
42b611c801SAlexander Leidinger #include <sys/limits.h>
437c438dbeSCameron Grant #include <sys/sysctl.h>
44285648f9SCameron Grant 
4567b1dce3SCameron Grant #include "feeder_if.h"
4667b1dce3SCameron Grant 
4767b1dce3SCameron Grant SND_DECLARE_FILE("$FreeBSD$");
4867b1dce3SCameron Grant 
49d95502a8SCameron Grant devclass_t pcm_devclass;
5082db23e2SCameron Grant 
51b8a36395SCameron Grant int pcm_veto_load = 1;
52b8a36395SCameron Grant 
53f3685841SAriff Abdullah int snd_unit = -1;
54cbe7d6a3SCameron Grant 
55cbebc90dSAlexander Motin static int snd_unit_auto = -1;
56af3b2549SHans Petter Selasky SYSCTL_INT(_hw_snd, OID_AUTO, default_auto, CTLFLAG_RWTUN,
57838d3589SAriff Abdullah     &snd_unit_auto, 0, "assign default unit to a newly attached device");
58ad8612b9SAriff Abdullah 
59bba4862cSAriff Abdullah int snd_maxautovchans = 16;
60987e5972SCameron Grant 
617029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, snd, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
627029da5cSPawel Biernacki     "Sound driver");
6382db23e2SCameron Grant 
6432a0e5d5SHans Petter Selasky static void pcm_sysinit(device_t);
6532a0e5d5SHans Petter Selasky 
66bba4862cSAriff Abdullah /*
67bba4862cSAriff Abdullah  * XXX I've had enough with people not telling proper version/arch
68bba4862cSAriff Abdullah  *     while reporting problems, not after 387397913213th questions/requests.
69bba4862cSAriff Abdullah  */
70a9bdb5d3SAndriy Gapon static char snd_driver_version[] =
71bba4862cSAriff Abdullah     __XSTRING(SND_DRV_VERSION)"/"MACHINE_ARCH;
72bba4862cSAriff Abdullah SYSCTL_STRING(_hw_snd, OID_AUTO, version, CTLFLAG_RD, &snd_driver_version,
7390da2b28SAriff Abdullah     0, "driver version/arch");
74bba4862cSAriff Abdullah 
75b611c801SAlexander Leidinger /**
76b611c801SAlexander Leidinger  * @brief Unit number allocator for syncgroup IDs
77b611c801SAlexander Leidinger  */
78b611c801SAlexander Leidinger struct unrhdr *pcmsg_unrhdr = NULL;
79b611c801SAlexander Leidinger 
8090da2b28SAriff Abdullah static int
8190da2b28SAriff Abdullah sndstat_prepare_pcm(SNDSTAT_PREPARE_PCM_ARGS)
8290da2b28SAriff Abdullah {
8390da2b28SAriff Abdullah 	SNDSTAT_PREPARE_PCM_BEGIN();
8490da2b28SAriff Abdullah 	SNDSTAT_PREPARE_PCM_END();
8590da2b28SAriff Abdullah }
8667b1dce3SCameron Grant 
8737209180SCameron Grant void *
882c69ba87SJohn Baldwin snd_mtxcreate(const char *desc, const char *type)
8937209180SCameron Grant {
9037209180SCameron Grant 	struct mtx *m;
9137209180SCameron Grant 
92a163d034SWarner Losh 	m = malloc(sizeof(*m), M_DEVBUF, M_WAITOK | M_ZERO);
9312e524a2SDon Lewis 	mtx_init(m, desc, type, MTX_DEF);
9412e524a2SDon Lewis 	return m;
9512e524a2SDon Lewis }
9612e524a2SDon Lewis 
9737209180SCameron Grant void
9837209180SCameron Grant snd_mtxfree(void *m)
9937209180SCameron Grant {
10037209180SCameron Grant 	struct mtx *mtx = m;
10137209180SCameron Grant 
10237209180SCameron Grant 	mtx_destroy(mtx);
10337209180SCameron Grant 	free(mtx, M_DEVBUF);
10437209180SCameron Grant }
10537209180SCameron Grant 
10637209180SCameron Grant void
10737209180SCameron Grant snd_mtxassert(void *m)
10837209180SCameron Grant {
109f00f162aSCameron Grant #ifdef INVARIANTS
11037209180SCameron Grant 	struct mtx *mtx = m;
11137209180SCameron Grant 
11237209180SCameron Grant 	mtx_assert(mtx, MA_OWNED);
11337209180SCameron Grant #endif
11437209180SCameron Grant }
11537209180SCameron Grant 
11637209180SCameron Grant int
11737209180SCameron Grant snd_setup_intr(device_t dev, struct resource *res, int flags, driver_intr_t hand, void *param, void **cookiep)
11837209180SCameron Grant {
119e4e61333SAriff Abdullah 	struct snddev_info *d;
12090da2b28SAriff Abdullah 
12137209180SCameron Grant 	flags &= INTR_MPSAFE;
12246700f12SPeter Wemm 	flags |= INTR_TYPE_AV;
123e4e61333SAriff Abdullah 	d = device_get_softc(dev);
124e4e61333SAriff Abdullah 	if (d != NULL && (flags & INTR_MPSAFE))
125e4e61333SAriff Abdullah 		d->flags |= SD_F_MPSAFE;
126e4e61333SAriff Abdullah 
127775fcb6eSBaptiste Daroussin 	return bus_setup_intr(dev, res, flags, NULL, hand, param, cookiep);
12837209180SCameron Grant }
12937209180SCameron Grant 
130bba4862cSAriff Abdullah static void
131bba4862cSAriff Abdullah pcm_clonereset(struct snddev_info *d)
132a1d444e1SAriff Abdullah {
133bba4862cSAriff Abdullah 	int cmax;
134bba4862cSAriff Abdullah 
135e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
136bba4862cSAriff Abdullah 
137bba4862cSAriff Abdullah 	cmax = d->playcount + d->reccount - 1;
138bba4862cSAriff Abdullah 	if (d->pvchancount > 0)
13990da2b28SAriff Abdullah 		cmax += max(d->pvchancount, snd_maxautovchans) - 1;
140bba4862cSAriff Abdullah 	if (d->rvchancount > 0)
14190da2b28SAriff Abdullah 		cmax += max(d->rvchancount, snd_maxautovchans) - 1;
142bba4862cSAriff Abdullah 	if (cmax > PCMMAXCLONE)
143bba4862cSAriff Abdullah 		cmax = PCMMAXCLONE;
144bba4862cSAriff Abdullah 	(void)snd_clone_gc(d->clones);
145bba4862cSAriff Abdullah 	(void)snd_clone_setmaxunit(d->clones, cmax);
146bba4862cSAriff Abdullah }
147bba4862cSAriff Abdullah 
14890da2b28SAriff Abdullah int
149bba4862cSAriff Abdullah pcm_setvchans(struct snddev_info *d, int direction, int newcnt, int num)
150bba4862cSAriff Abdullah {
151bba4862cSAriff Abdullah 	struct pcm_channel *c, *ch, *nch;
15290da2b28SAriff Abdullah 	struct pcmchan_caps *caps;
15390da2b28SAriff Abdullah 	int i, err, vcnt;
154bba4862cSAriff Abdullah 
155e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
156a1d444e1SAriff Abdullah 
157bba4862cSAriff Abdullah 	if ((direction == PCMDIR_PLAY && d->playcount < 1) ||
158e4e61333SAriff Abdullah 	    (direction == PCMDIR_REC && d->reccount < 1))
159e4e61333SAriff Abdullah 		return (ENODEV);
160a580b31aSAriff Abdullah 
161e4e61333SAriff Abdullah 	if (!(d->flags & SD_F_AUTOVCHAN))
162e4e61333SAriff Abdullah 		return (EINVAL);
163a1d444e1SAriff Abdullah 
164e4e61333SAriff Abdullah 	if (newcnt < 0 || newcnt > SND_MAXVCHANS)
165e4e61333SAriff Abdullah 		return (E2BIG);
166a1d444e1SAriff Abdullah 
167bba4862cSAriff Abdullah 	if (direction == PCMDIR_PLAY)
168bba4862cSAriff Abdullah 		vcnt = d->pvchancount;
169bba4862cSAriff Abdullah 	else if (direction == PCMDIR_REC)
170bba4862cSAriff Abdullah 		vcnt = d->rvchancount;
171e4e61333SAriff Abdullah 	else
172e4e61333SAriff Abdullah 		return (EINVAL);
173a1d444e1SAriff Abdullah 
174a1d444e1SAriff Abdullah 	if (newcnt > vcnt) {
175bba4862cSAriff Abdullah 		KASSERT(num == -1 ||
176bba4862cSAriff Abdullah 		    (num >= 0 && num < SND_MAXVCHANS && (newcnt - 1) == vcnt),
177bba4862cSAriff Abdullah 		    ("bogus vchan_create() request num=%d newcnt=%d vcnt=%d",
178bba4862cSAriff Abdullah 		    num, newcnt, vcnt));
179a1d444e1SAriff Abdullah 		/* add new vchans - find a parent channel first */
180e4e61333SAriff Abdullah 		ch = NULL;
181bba4862cSAriff Abdullah 		CHN_FOREACH(c, d, channels.pcm) {
182a1d444e1SAriff Abdullah 			CHN_LOCK(c);
183bba4862cSAriff Abdullah 			if (c->direction == direction &&
184bba4862cSAriff Abdullah 			    ((c->flags & CHN_F_HAS_VCHAN) || (vcnt == 0 &&
18590da2b28SAriff Abdullah 			    c->refcount < 1 &&
186e4e61333SAriff Abdullah 			    !(c->flags & (CHN_F_BUSY | CHN_F_VIRTUAL))))) {
18790da2b28SAriff Abdullah 				/*
18890da2b28SAriff Abdullah 				 * Reuse hw channel with vchans already
18990da2b28SAriff Abdullah 				 * created.
19090da2b28SAriff Abdullah 				 */
19190da2b28SAriff Abdullah 				if (c->flags & CHN_F_HAS_VCHAN) {
192e4e61333SAriff Abdullah 					ch = c;
193e4e61333SAriff Abdullah 					break;
194e4e61333SAriff Abdullah 				}
19590da2b28SAriff Abdullah 				/*
19690da2b28SAriff Abdullah 				 * No vchans ever created, look for
19790da2b28SAriff Abdullah 				 * channels with supported formats.
19890da2b28SAriff Abdullah 				 */
19990da2b28SAriff Abdullah 				caps = chn_getcaps(c);
20090da2b28SAriff Abdullah 				if (caps == NULL) {
20190da2b28SAriff Abdullah 					CHN_UNLOCK(c);
20290da2b28SAriff Abdullah 					continue;
20390da2b28SAriff Abdullah 				}
20490da2b28SAriff Abdullah 				for (i = 0; caps->fmtlist[i] != 0; i++) {
20590da2b28SAriff Abdullah 					if (caps->fmtlist[i] & AFMT_CONVERTIBLE)
20690da2b28SAriff Abdullah 						break;
20790da2b28SAriff Abdullah 				}
20890da2b28SAriff Abdullah 				if (caps->fmtlist[i] != 0) {
20990da2b28SAriff Abdullah 					ch = c;
21090da2b28SAriff Abdullah 				    	break;
21190da2b28SAriff Abdullah 				}
21290da2b28SAriff Abdullah 			}
213a1d444e1SAriff Abdullah 			CHN_UNLOCK(c);
214a1d444e1SAriff Abdullah 		}
215e4e61333SAriff Abdullah 		if (ch == NULL)
216e4e61333SAriff Abdullah 			return (EBUSY);
217e4e61333SAriff Abdullah 		ch->flags |= CHN_F_BUSY;
218e4e61333SAriff Abdullah 		err = 0;
219a1d444e1SAriff Abdullah 		while (err == 0 && newcnt > vcnt) {
220e4e61333SAriff Abdullah 			err = vchan_create(ch, num);
221bba4862cSAriff Abdullah 			if (err == 0)
222a1d444e1SAriff Abdullah 				vcnt++;
223bba4862cSAriff Abdullah 			else if (err == E2BIG && newcnt > vcnt)
224bba4862cSAriff Abdullah 				device_printf(d->dev,
225bba4862cSAriff Abdullah 				    "%s: err=%d Maximum channel reached.\n",
226bba4862cSAriff Abdullah 				    __func__, err);
227a1d444e1SAriff Abdullah 		}
228a1d444e1SAriff Abdullah 		if (vcnt == 0)
229e4e61333SAriff Abdullah 			ch->flags &= ~CHN_F_BUSY;
230e4e61333SAriff Abdullah 		CHN_UNLOCK(ch);
231e4e61333SAriff Abdullah 		if (err != 0)
232e4e61333SAriff Abdullah 			return (err);
233e4e61333SAriff Abdullah 		else
234bba4862cSAriff Abdullah 			pcm_clonereset(d);
235a1d444e1SAriff Abdullah 	} else if (newcnt < vcnt) {
236bba4862cSAriff Abdullah 		KASSERT(num == -1,
237bba4862cSAriff Abdullah 		    ("bogus vchan_destroy() request num=%d", num));
238bba4862cSAriff Abdullah 		CHN_FOREACH(c, d, channels.pcm) {
239a1d444e1SAriff Abdullah 			CHN_LOCK(c);
240bba4862cSAriff Abdullah 			if (c->direction != direction ||
241bba4862cSAriff Abdullah 			    CHN_EMPTY(c, children) ||
242bba4862cSAriff Abdullah 			    !(c->flags & CHN_F_HAS_VCHAN)) {
243a1d444e1SAriff Abdullah 				CHN_UNLOCK(c);
244bba4862cSAriff Abdullah 				continue;
245a1d444e1SAriff Abdullah 			}
246bba4862cSAriff Abdullah 			CHN_FOREACH_SAFE(ch, c, nch, children) {
247bba4862cSAriff Abdullah 				CHN_LOCK(ch);
24890da2b28SAriff Abdullah 				if (vcnt == 1 && c->refcount > 0) {
249bba4862cSAriff Abdullah 					CHN_UNLOCK(ch);
25090da2b28SAriff Abdullah 					break;
25190da2b28SAriff Abdullah 				}
25290da2b28SAriff Abdullah 				if (!(ch->flags & CHN_F_BUSY) &&
25390da2b28SAriff Abdullah 				    ch->refcount < 1) {
254bba4862cSAriff Abdullah 					err = vchan_destroy(ch);
255a1d444e1SAriff Abdullah 					if (err == 0)
256a1d444e1SAriff Abdullah 						vcnt--;
257bba4862cSAriff Abdullah 				} else
258bba4862cSAriff Abdullah 					CHN_UNLOCK(ch);
259e4e61333SAriff Abdullah 				if (vcnt == newcnt)
260bba4862cSAriff Abdullah 					break;
261a1d444e1SAriff Abdullah 			}
262bba4862cSAriff Abdullah 			CHN_UNLOCK(c);
263bba4862cSAriff Abdullah 			break;
264bba4862cSAriff Abdullah 		}
265bba4862cSAriff Abdullah 		pcm_clonereset(d);
266bba4862cSAriff Abdullah 	}
267a1d444e1SAriff Abdullah 
268e4e61333SAriff Abdullah 	return (0);
269a1d444e1SAriff Abdullah }
270a1d444e1SAriff Abdullah 
2713fdb3676SAriff Abdullah /* return error status and a locked channel */
2723fdb3676SAriff Abdullah int
2733fdb3676SAriff Abdullah pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction,
27490da2b28SAriff Abdullah     pid_t pid, char *comm, int devunit)
275285648f9SCameron Grant {
276285648f9SCameron Grant 	struct pcm_channel *c;
27790da2b28SAriff Abdullah 	int err, vchancount, vchan_num;
278bba4862cSAriff Abdullah 
279bba4862cSAriff Abdullah 	KASSERT(d != NULL && ch != NULL && (devunit == -1 ||
280bba4862cSAriff Abdullah 	    !(devunit & ~(SND_U_MASK | SND_D_MASK | SND_C_MASK))) &&
281bba4862cSAriff Abdullah 	    (direction == PCMDIR_PLAY || direction == PCMDIR_REC),
282e4e61333SAriff Abdullah 	    ("%s(): invalid d=%p ch=%p direction=%d pid=%d devunit=%d",
283bba4862cSAriff Abdullah 	    __func__, d, ch, direction, pid, devunit));
284e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
285bba4862cSAriff Abdullah 
286bba4862cSAriff Abdullah 	/* Double check again. */
287bba4862cSAriff Abdullah 	if (devunit != -1) {
288bba4862cSAriff Abdullah 		switch (snd_unit2d(devunit)) {
289bba4862cSAriff Abdullah 		case SND_DEV_DSPHW_PLAY:
290bba4862cSAriff Abdullah 		case SND_DEV_DSPHW_VPLAY:
291bba4862cSAriff Abdullah 			if (direction != PCMDIR_PLAY)
29290da2b28SAriff Abdullah 				return (ENOTSUP);
293bba4862cSAriff Abdullah 			break;
294bba4862cSAriff Abdullah 		case SND_DEV_DSPHW_REC:
295bba4862cSAriff Abdullah 		case SND_DEV_DSPHW_VREC:
296bba4862cSAriff Abdullah 			if (direction != PCMDIR_REC)
29790da2b28SAriff Abdullah 				return (ENOTSUP);
298bba4862cSAriff Abdullah 			break;
299bba4862cSAriff Abdullah 		default:
300bba4862cSAriff Abdullah 			if (!(direction == PCMDIR_PLAY ||
301bba4862cSAriff Abdullah 			    direction == PCMDIR_REC))
30290da2b28SAriff Abdullah 				return (ENOTSUP);
303bba4862cSAriff Abdullah 			break;
304bba4862cSAriff Abdullah 		}
305bba4862cSAriff Abdullah 	}
306285648f9SCameron Grant 
30790da2b28SAriff Abdullah 	*ch = NULL;
30890da2b28SAriff Abdullah 	vchan_num = 0;
30990da2b28SAriff Abdullah 	vchancount = (direction == PCMDIR_PLAY) ? d->pvchancount :
31090da2b28SAriff Abdullah 	    d->rvchancount;
31190da2b28SAriff Abdullah 
3123fdb3676SAriff Abdullah retry_chnalloc:
31390da2b28SAriff Abdullah 	err = ENOTSUP;
314f637a36cSCameron Grant 	/* scan for a free channel */
315bba4862cSAriff Abdullah 	CHN_FOREACH(c, d, channels.pcm) {
31649c5e6e2SCameron Grant 		CHN_LOCK(c);
31790da2b28SAriff Abdullah 		if (devunit == -1 && c->direction == direction &&
31890da2b28SAriff Abdullah 		    (c->flags & CHN_F_VIRTUAL)) {
31990da2b28SAriff Abdullah 			if (vchancount < snd_maxautovchans &&
32090da2b28SAriff Abdullah 			    vchan_num < CHN_CHAN(c)) {
32190da2b28SAriff Abdullah 			    	CHN_UNLOCK(c);
32290da2b28SAriff Abdullah 				goto vchan_alloc;
32390da2b28SAriff Abdullah 			}
32490da2b28SAriff Abdullah 			vchan_num++;
32590da2b28SAriff Abdullah 		}
326bba4862cSAriff Abdullah 		if (c->direction == direction && !(c->flags & CHN_F_BUSY) &&
327bba4862cSAriff Abdullah 		    (devunit == -1 || devunit == -2 || c->unit == devunit)) {
328285648f9SCameron Grant 			c->flags |= CHN_F_BUSY;
329b8f0d9e0SCameron Grant 			c->pid = pid;
33090da2b28SAriff Abdullah 			strlcpy(c->comm, (comm != NULL) ? comm :
33190da2b28SAriff Abdullah 			    CHN_COMM_UNKNOWN, sizeof(c->comm));
3323fdb3676SAriff Abdullah 			*ch = c;
333bba4862cSAriff Abdullah 			return (0);
334bba4862cSAriff Abdullah 		} else if (c->unit == devunit) {
3353fdb3676SAriff Abdullah 			if (c->direction != direction)
33690da2b28SAriff Abdullah 				err = ENOTSUP;
3373fdb3676SAriff Abdullah 			else if (c->flags & CHN_F_BUSY)
3383fdb3676SAriff Abdullah 				err = EBUSY;
3393fdb3676SAriff Abdullah 			else
3403fdb3676SAriff Abdullah 				err = EINVAL;
3413fdb3676SAriff Abdullah 			CHN_UNLOCK(c);
342bba4862cSAriff Abdullah 			return (err);
343bba4862cSAriff Abdullah 		} else if ((devunit == -1 || devunit == -2) &&
344bba4862cSAriff Abdullah 		    c->direction == direction && (c->flags & CHN_F_BUSY))
345a1d444e1SAriff Abdullah 			err = EBUSY;
34649c5e6e2SCameron Grant 		CHN_UNLOCK(c);
347285648f9SCameron Grant 	}
348f637a36cSCameron Grant 
349e4e61333SAriff Abdullah 	if (devunit == -2)
350e4e61333SAriff Abdullah 		return (err);
351e4e61333SAriff Abdullah 
35290da2b28SAriff Abdullah vchan_alloc:
353f637a36cSCameron Grant 	/* no channel available */
354e4e61333SAriff Abdullah 	if (devunit == -1 || snd_unit2d(devunit) == SND_DEV_DSPHW_VPLAY ||
355e4e61333SAriff Abdullah 	    snd_unit2d(devunit) == SND_DEV_DSPHW_VREC) {
356bba4862cSAriff Abdullah 		if (!(vchancount > 0 && vchancount < snd_maxautovchans) &&
357bba4862cSAriff Abdullah 		    (devunit == -1 || snd_unit2c(devunit) < snd_maxautovchans))
358bba4862cSAriff Abdullah 			return (err);
359bba4862cSAriff Abdullah 		err = pcm_setvchans(d, direction, vchancount + 1,
360bba4862cSAriff Abdullah 		    (devunit == -1) ? -1 : snd_unit2c(devunit));
361a1d444e1SAriff Abdullah 		if (err == 0) {
362bba4862cSAriff Abdullah 			if (devunit == -1)
363bba4862cSAriff Abdullah 				devunit = -2;
3643fdb3676SAriff Abdullah 			goto retry_chnalloc;
365f637a36cSCameron Grant 		}
366f637a36cSCameron Grant 	}
367f637a36cSCameron Grant 
368bba4862cSAriff Abdullah 	return (err);
369285648f9SCameron Grant }
370285648f9SCameron Grant 
371b8f0d9e0SCameron Grant /* release a locked channel and unlock it */
372285648f9SCameron Grant int
373b8f0d9e0SCameron Grant pcm_chnrelease(struct pcm_channel *c)
374285648f9SCameron Grant {
375e4e61333SAriff Abdullah 	PCM_BUSYASSERT(c->parentsnddev);
376b8f0d9e0SCameron Grant 	CHN_LOCKASSERT(c);
377e4e61333SAriff Abdullah 
378285648f9SCameron Grant 	c->flags &= ~CHN_F_BUSY;
379b8f0d9e0SCameron Grant 	c->pid = -1;
38090da2b28SAriff Abdullah 	strlcpy(c->comm, CHN_COMM_UNUSED, sizeof(c->comm));
38149c5e6e2SCameron Grant 	CHN_UNLOCK(c);
382e4e61333SAriff Abdullah 
383e4e61333SAriff Abdullah 	return (0);
384285648f9SCameron Grant }
385285648f9SCameron Grant 
386285648f9SCameron Grant int
387285648f9SCameron Grant pcm_chnref(struct pcm_channel *c, int ref)
388285648f9SCameron Grant {
389e4e61333SAriff Abdullah 	PCM_BUSYASSERT(c->parentsnddev);
390b8f0d9e0SCameron Grant 	CHN_LOCKASSERT(c);
391e4e61333SAriff Abdullah 
392285648f9SCameron Grant 	c->refcount += ref;
393e4e61333SAriff Abdullah 
394e4e61333SAriff Abdullah 	return (c->refcount);
395285648f9SCameron Grant }
396285648f9SCameron Grant 
39767b1dce3SCameron Grant int
39867b1dce3SCameron Grant pcm_inprog(struct snddev_info *d, int delta)
39967b1dce3SCameron Grant {
40090da2b28SAriff Abdullah 	PCM_LOCKASSERT(d);
401a527dbc7SCameron Grant 
402a527dbc7SCameron Grant 	d->inprog += delta;
403e4e61333SAriff Abdullah 
404e4e61333SAriff Abdullah 	return (d->inprog);
40567b1dce3SCameron Grant }
40667b1dce3SCameron Grant 
40767b1dce3SCameron Grant static void
40867b1dce3SCameron Grant pcm_setmaxautovchans(struct snddev_info *d, int num)
40967b1dce3SCameron Grant {
410e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
411e4e61333SAriff Abdullah 
412bba4862cSAriff Abdullah 	if (num < 0)
413bba4862cSAriff Abdullah 		return;
414bba4862cSAriff Abdullah 
415bba4862cSAriff Abdullah 	if (num >= 0 && d->pvchancount > num)
416bba4862cSAriff Abdullah 		(void)pcm_setvchans(d, PCMDIR_PLAY, num, -1);
417bba4862cSAriff Abdullah 	else if (num > 0 && d->pvchancount == 0)
418bba4862cSAriff Abdullah 		(void)pcm_setvchans(d, PCMDIR_PLAY, 1, -1);
419bba4862cSAriff Abdullah 
420bba4862cSAriff Abdullah 	if (num >= 0 && d->rvchancount > num)
421bba4862cSAriff Abdullah 		(void)pcm_setvchans(d, PCMDIR_REC, num, -1);
422bba4862cSAriff Abdullah 	else if (num > 0 && d->rvchancount == 0)
423bba4862cSAriff Abdullah 		(void)pcm_setvchans(d, PCMDIR_REC, 1, -1);
424bba4862cSAriff Abdullah 
425bba4862cSAriff Abdullah 	pcm_clonereset(d);
42667b1dce3SCameron Grant }
42767b1dce3SCameron Grant 
42833dbf14aSCameron Grant static int
429851a904aSAlexander Leidinger sysctl_hw_snd_default_unit(SYSCTL_HANDLER_ARGS)
43033dbf14aSCameron Grant {
431b8f0d9e0SCameron Grant 	struct snddev_info *d;
43233dbf14aSCameron Grant 	int error, unit;
43333dbf14aSCameron Grant 
43433dbf14aSCameron Grant 	unit = snd_unit;
435041b706bSDavid Malone 	error = sysctl_handle_int(oidp, &unit, 0, req);
43633dbf14aSCameron Grant 	if (error == 0 && req->newptr != NULL) {
437b8f0d9e0SCameron Grant 		d = devclass_get_softc(pcm_devclass, unit);
438e4e61333SAriff Abdullah 		if (!PCM_REGISTERED(d) || CHN_EMPTY(d, channels.pcm))
439b8f0d9e0SCameron Grant 			return EINVAL;
44033dbf14aSCameron Grant 		snd_unit = unit;
441cbebc90dSAlexander Motin 		snd_unit_auto = 0;
44233dbf14aSCameron Grant 	}
44333dbf14aSCameron Grant 	return (error);
44433dbf14aSCameron Grant }
445851a904aSAlexander Leidinger /* XXX: do we need a way to let the user change the default unit? */
446b7d6c6b5SEitan Adler SYSCTL_PROC(_hw_snd, OID_AUTO, default_unit,
4477029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT, 0,
4487029da5cSPawel Biernacki     sizeof(int), sysctl_hw_snd_default_unit, "I",
449b7d6c6b5SEitan Adler     "default sound device");
450987e5972SCameron Grant 
451cd9766c5SCameron Grant static int
45267b1dce3SCameron Grant sysctl_hw_snd_maxautovchans(SYSCTL_HANDLER_ARGS)
453cd9766c5SCameron Grant {
45467b1dce3SCameron Grant 	struct snddev_info *d;
45567b1dce3SCameron Grant 	int i, v, error;
456cd9766c5SCameron Grant 
45767b1dce3SCameron Grant 	v = snd_maxautovchans;
458041b706bSDavid Malone 	error = sysctl_handle_int(oidp, &v, 0, req);
459cd9766c5SCameron Grant 	if (error == 0 && req->newptr != NULL) {
460bba4862cSAriff Abdullah 		if (v < 0)
461bba4862cSAriff Abdullah 			v = 0;
462bba4862cSAriff Abdullah 		if (v > SND_MAXVCHANS)
463bba4862cSAriff Abdullah 			v = SND_MAXVCHANS;
464bba4862cSAriff Abdullah 		snd_maxautovchans = v;
4659c271f79SAriff Abdullah 		for (i = 0; pcm_devclass != NULL &&
4669c271f79SAriff Abdullah 		    i < devclass_get_maxunit(pcm_devclass); i++) {
46767b1dce3SCameron Grant 			d = devclass_get_softc(pcm_devclass, i);
468e4e61333SAriff Abdullah 			if (!PCM_REGISTERED(d))
46967b1dce3SCameron Grant 				continue;
470e4e61333SAriff Abdullah 			PCM_ACQUIRE_QUICK(d);
47167b1dce3SCameron Grant 			pcm_setmaxautovchans(d, v);
472e4e61333SAriff Abdullah 			PCM_RELEASE_QUICK(d);
47367b1dce3SCameron Grant 		}
47467b1dce3SCameron Grant 	}
475cd9766c5SCameron Grant 	return (error);
476cd9766c5SCameron Grant }
4777029da5cSPawel Biernacki SYSCTL_PROC(_hw_snd, OID_AUTO, maxautovchans,
4787029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
4797029da5cSPawel Biernacki     sysctl_hw_snd_maxautovchans, "I",
4807029da5cSPawel Biernacki     "maximum virtual channel");
481f637a36cSCameron Grant 
482285648f9SCameron Grant struct pcm_channel *
483bba4862cSAriff Abdullah pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo)
484987e5972SCameron Grant {
485bba4862cSAriff Abdullah 	struct pcm_channel *ch;
486bba4862cSAriff Abdullah 	int direction, err, rpnum, *pnum, max;
487bba4862cSAriff Abdullah 	int udc, device, chan;
488bba4862cSAriff Abdullah 	char *dirs, *devname, buf[CHN_NAMELEN];
489bba4862cSAriff Abdullah 
490e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
49190da2b28SAriff Abdullah 	PCM_LOCKASSERT(d);
492bba4862cSAriff Abdullah 	KASSERT(num >= -1, ("invalid num=%d", num));
493bba4862cSAriff Abdullah 
494987e5972SCameron Grant 
495285648f9SCameron Grant 	switch (dir) {
496285648f9SCameron Grant 	case PCMDIR_PLAY:
497285648f9SCameron Grant 		dirs = "play";
498a3193a9cSDon Lewis 		direction = PCMDIR_PLAY;
499a67fe5c1SCameron Grant 		pnum = &d->playcount;
500bba4862cSAriff Abdullah 		device = SND_DEV_DSPHW_PLAY;
501bba4862cSAriff Abdullah 		max = SND_MAXHWCHAN;
502285648f9SCameron Grant 		break;
503bba4862cSAriff Abdullah 	case PCMDIR_PLAY_VIRTUAL:
504bba4862cSAriff Abdullah 		dirs = "virtual";
505bba4862cSAriff Abdullah 		direction = PCMDIR_PLAY;
506bba4862cSAriff Abdullah 		pnum = &d->pvchancount;
507bba4862cSAriff Abdullah 		device = SND_DEV_DSPHW_VPLAY;
508bba4862cSAriff Abdullah 		max = SND_MAXVCHANS;
509bba4862cSAriff Abdullah 		break;
510285648f9SCameron Grant 	case PCMDIR_REC:
511285648f9SCameron Grant 		dirs = "record";
512a3193a9cSDon Lewis 		direction = PCMDIR_REC;
513a67fe5c1SCameron Grant 		pnum = &d->reccount;
514bba4862cSAriff Abdullah 		device = SND_DEV_DSPHW_REC;
515bba4862cSAriff Abdullah 		max = SND_MAXHWCHAN;
516285648f9SCameron Grant 		break;
517bba4862cSAriff Abdullah 	case PCMDIR_REC_VIRTUAL:
518285648f9SCameron Grant 		dirs = "virtual";
519bba4862cSAriff Abdullah 		direction = PCMDIR_REC;
520bba4862cSAriff Abdullah 		pnum = &d->rvchancount;
521bba4862cSAriff Abdullah 		device = SND_DEV_DSPHW_VREC;
522bba4862cSAriff Abdullah 		max = SND_MAXVCHANS;
523285648f9SCameron Grant 		break;
524285648f9SCameron Grant 	default:
525e4e61333SAriff Abdullah 		return (NULL);
5269c326820SCameron Grant 	}
527285648f9SCameron Grant 
528bba4862cSAriff Abdullah 	chan = (num == -1) ? 0 : num;
529285648f9SCameron Grant 
530e4e61333SAriff Abdullah 	if (*pnum >= max || chan >= max)
531e4e61333SAriff Abdullah 		return (NULL);
532bba4862cSAriff Abdullah 
5333fdb3676SAriff Abdullah 	rpnum = 0;
534bba4862cSAriff Abdullah 
535bba4862cSAriff Abdullah 	CHN_FOREACH(ch, d, channels.pcm) {
536bba4862cSAriff Abdullah 		if (CHN_DEV(ch) != device)
5373fdb3676SAriff Abdullah 			continue;
538bba4862cSAriff Abdullah 		if (chan == CHN_CHAN(ch)) {
539bba4862cSAriff Abdullah 			if (num != -1) {
5403fdb3676SAriff Abdullah 				device_printf(d->dev,
541bba4862cSAriff Abdullah 				    "channel num=%d allocated!\n", chan);
542e4e61333SAriff Abdullah 				return (NULL);
543bba4862cSAriff Abdullah 			}
544bba4862cSAriff Abdullah 			chan++;
545bba4862cSAriff Abdullah 			if (chan >= max) {
546bba4862cSAriff Abdullah 				device_printf(d->dev,
547bba4862cSAriff Abdullah 				    "chan=%d > %d\n", chan, max);
548e4e61333SAriff Abdullah 				return (NULL);
549bba4862cSAriff Abdullah 			}
5503fdb3676SAriff Abdullah 		}
5513fdb3676SAriff Abdullah 		rpnum++;
5523fdb3676SAriff Abdullah 	}
553bba4862cSAriff Abdullah 
5543fdb3676SAriff Abdullah 	if (*pnum != rpnum) {
5553fdb3676SAriff Abdullah 		device_printf(d->dev,
556bba4862cSAriff Abdullah 		    "%s(): WARNING: pnum screwed : dirs=%s pnum=%d rpnum=%d\n",
5573fdb3676SAriff Abdullah 		    __func__, dirs, *pnum, rpnum);
558e4e61333SAriff Abdullah 		return (NULL);
5593fdb3676SAriff Abdullah 	}
560a67fe5c1SCameron Grant 
561bba4862cSAriff Abdullah 	udc = snd_mkunit(device_get_unit(d->dev), device, chan);
562bba4862cSAriff Abdullah 	devname = dsp_unit2name(buf, sizeof(buf), udc);
563bba4862cSAriff Abdullah 
564bba4862cSAriff Abdullah 	if (devname == NULL) {
565bba4862cSAriff Abdullah 		device_printf(d->dev,
566bba4862cSAriff Abdullah 		    "Failed to query device name udc=0x%08x\n", udc);
567e4e61333SAriff Abdullah 		return (NULL);
568bba4862cSAriff Abdullah 	}
569bba4862cSAriff Abdullah 
57090da2b28SAriff Abdullah 	PCM_UNLOCK(d);
571bba4862cSAriff Abdullah 	ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
572bba4862cSAriff Abdullah 	ch->methods = kobj_create(cls, M_DEVBUF, M_WAITOK | M_ZERO);
573bba4862cSAriff Abdullah 	ch->unit = udc;
574285648f9SCameron Grant 	ch->pid = -1;
57590da2b28SAriff Abdullah 	strlcpy(ch->comm, CHN_COMM_UNUSED, sizeof(ch->comm));
576285648f9SCameron Grant 	ch->parentsnddev = d;
577285648f9SCameron Grant 	ch->parentchannel = parent;
578436c9b65SScott Long 	ch->dev = d->dev;
579bba4862cSAriff Abdullah 	ch->trigger = PCMTRIG_STOP;
580bba4862cSAriff Abdullah 	snprintf(ch->name, sizeof(ch->name), "%s:%s:%s",
581bba4862cSAriff Abdullah 	    device_get_nameunit(ch->dev), dirs, devname);
582285648f9SCameron Grant 
583a3193a9cSDon Lewis 	err = chn_init(ch, devinfo, dir, direction);
58490da2b28SAriff Abdullah 	PCM_LOCK(d);
5850f55ac6cSCameron Grant 	if (err) {
586bba4862cSAriff Abdullah 		device_printf(d->dev, "chn_init(%s) failed: err = %d\n",
587bba4862cSAriff Abdullah 		    ch->name, err);
588285648f9SCameron Grant 		kobj_delete(ch->methods, M_DEVBUF);
589285648f9SCameron Grant 		free(ch, M_DEVBUF);
590e4e61333SAriff Abdullah 		return (NULL);
591bbb5bf3dSCameron Grant 	}
592285648f9SCameron Grant 
593e4e61333SAriff Abdullah 	return (ch);
594285648f9SCameron Grant }
595285648f9SCameron Grant 
596285648f9SCameron Grant int
597285648f9SCameron Grant pcm_chn_destroy(struct pcm_channel *ch)
598285648f9SCameron Grant {
599a67fe5c1SCameron Grant 	struct snddev_info *d;
600285648f9SCameron Grant 	int err;
601285648f9SCameron Grant 
602a67fe5c1SCameron Grant 	d = ch->parentsnddev;
603e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
604e4e61333SAriff Abdullah 
605285648f9SCameron Grant 	err = chn_kill(ch);
606285648f9SCameron Grant 	if (err) {
607e4e61333SAriff Abdullah 		device_printf(ch->dev, "chn_kill(%s) failed, err = %d\n",
608e4e61333SAriff Abdullah 		    ch->name, err);
609e4e61333SAriff Abdullah 		return (err);
610285648f9SCameron Grant 	}
611285648f9SCameron Grant 
612285648f9SCameron Grant 	kobj_delete(ch->methods, M_DEVBUF);
613285648f9SCameron Grant 	free(ch, M_DEVBUF);
614285648f9SCameron Grant 
615e4e61333SAriff Abdullah 	return (0);
616285648f9SCameron Grant }
617285648f9SCameron Grant 
618285648f9SCameron Grant int
6195ee30e27SMathew Kanner pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch)
620285648f9SCameron Grant {
621e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
62290da2b28SAriff Abdullah 	PCM_LOCKASSERT(d);
623bba4862cSAriff Abdullah 	KASSERT(ch != NULL && (ch->direction == PCMDIR_PLAY ||
624bba4862cSAriff Abdullah 	    ch->direction == PCMDIR_REC), ("Invalid pcm channel"));
625285648f9SCameron Grant 
62690da2b28SAriff Abdullah 	CHN_INSERT_SORT_ASCEND(d, ch, channels.pcm);
6273fdb3676SAriff Abdullah 
628e4e61333SAriff Abdullah 	switch (CHN_DEV(ch)) {
629e4e61333SAriff Abdullah 	case SND_DEV_DSPHW_PLAY:
630e4e61333SAriff Abdullah 		d->playcount++;
631e4e61333SAriff Abdullah 		break;
632e4e61333SAriff Abdullah 	case SND_DEV_DSPHW_VPLAY:
633e4e61333SAriff Abdullah 		d->pvchancount++;
634e4e61333SAriff Abdullah 		break;
635e4e61333SAriff Abdullah 	case SND_DEV_DSPHW_REC:
636e4e61333SAriff Abdullah 		d->reccount++;
637e4e61333SAriff Abdullah 		break;
638e4e61333SAriff Abdullah 	case SND_DEV_DSPHW_VREC:
639e4e61333SAriff Abdullah 		d->rvchancount++;
640e4e61333SAriff Abdullah 		break;
641e4e61333SAriff Abdullah 	default:
642e4e61333SAriff Abdullah 		break;
643e4e61333SAriff Abdullah 	}
644b8f0d9e0SCameron Grant 
645e4e61333SAriff Abdullah 	d->devcount++;
646e4e61333SAriff Abdullah 
647e4e61333SAriff Abdullah 	return (0);
64833dbf14aSCameron Grant }
64933dbf14aSCameron Grant 
650285648f9SCameron Grant int
6515ee30e27SMathew Kanner pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch)
65233dbf14aSCameron Grant {
653bba4862cSAriff Abdullah 	struct pcm_channel *tmp;
65433dbf14aSCameron Grant 
655e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
65690da2b28SAriff Abdullah 	PCM_LOCKASSERT(d);
657e4e61333SAriff Abdullah 
658bba4862cSAriff Abdullah 	tmp = NULL;
659a527dbc7SCameron Grant 
660bba4862cSAriff Abdullah 	CHN_FOREACH(tmp, d, channels.pcm) {
661bba4862cSAriff Abdullah 		if (tmp == ch)
662bba4862cSAriff Abdullah 			break;
66333dbf14aSCameron Grant 	}
664bba4862cSAriff Abdullah 
665bba4862cSAriff Abdullah 	if (tmp != ch)
666e4e61333SAriff Abdullah 		return (EINVAL);
66767beb5a5SCameron Grant 
668bba4862cSAriff Abdullah 	CHN_REMOVE(d, ch, channels.pcm);
669e4e61333SAriff Abdullah 
670bba4862cSAriff Abdullah 	switch (CHN_DEV(ch)) {
671bba4862cSAriff Abdullah 	case SND_DEV_DSPHW_PLAY:
67267beb5a5SCameron Grant 		d->playcount--;
673bba4862cSAriff Abdullah 		break;
674bba4862cSAriff Abdullah 	case SND_DEV_DSPHW_VPLAY:
675bba4862cSAriff Abdullah 		d->pvchancount--;
676bba4862cSAriff Abdullah 		break;
677bba4862cSAriff Abdullah 	case SND_DEV_DSPHW_REC:
678bba4862cSAriff Abdullah 		d->reccount--;
679bba4862cSAriff Abdullah 		break;
680bba4862cSAriff Abdullah 	case SND_DEV_DSPHW_VREC:
681bba4862cSAriff Abdullah 		d->rvchancount--;
682bba4862cSAriff Abdullah 		break;
683bba4862cSAriff Abdullah 	default:
684bba4862cSAriff Abdullah 		break;
685bba4862cSAriff Abdullah 	}
686285648f9SCameron Grant 
687e4e61333SAriff Abdullah 	d->devcount--;
688e4e61333SAriff Abdullah 
689e4e61333SAriff Abdullah 	return (0);
690987e5972SCameron Grant }
691987e5972SCameron Grant 
692987e5972SCameron Grant int
693285648f9SCameron Grant pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
694285648f9SCameron Grant {
695285648f9SCameron Grant 	struct snddev_info *d = device_get_softc(dev);
69667b1dce3SCameron Grant 	struct pcm_channel *ch;
69767b1dce3SCameron Grant 	int err;
698285648f9SCameron Grant 
699e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
700e4e61333SAriff Abdullah 
70190da2b28SAriff Abdullah 	PCM_LOCK(d);
702bba4862cSAriff Abdullah 	ch = pcm_chn_create(d, NULL, cls, dir, -1, devinfo);
703285648f9SCameron Grant 	if (!ch) {
704e4e61333SAriff Abdullah 		device_printf(d->dev, "pcm_chn_create(%s, %d, %p) failed\n",
705e4e61333SAriff Abdullah 		    cls->name, dir, devinfo);
70690da2b28SAriff Abdullah 		PCM_UNLOCK(d);
707e4e61333SAriff Abdullah 		return (ENODEV);
708285648f9SCameron Grant 	}
709cd9766c5SCameron Grant 
7105ee30e27SMathew Kanner 	err = pcm_chn_add(d, ch);
71190da2b28SAriff Abdullah 	PCM_UNLOCK(d);
712285648f9SCameron Grant 	if (err) {
713e4e61333SAriff Abdullah 		device_printf(d->dev, "pcm_chn_add(%s) failed, err=%d\n",
714e4e61333SAriff Abdullah 		    ch->name, err);
715285648f9SCameron Grant 		pcm_chn_destroy(ch);
716cd9766c5SCameron Grant 	}
717cd9766c5SCameron Grant 
718e4e61333SAriff Abdullah 	return (err);
719285648f9SCameron Grant }
720285648f9SCameron Grant 
721285648f9SCameron Grant static int
722285648f9SCameron Grant pcm_killchan(device_t dev)
723285648f9SCameron Grant {
724285648f9SCameron Grant 	struct snddev_info *d = device_get_softc(dev);
725e33bee07SOlivier Houchard 	struct pcm_channel *ch;
726e4e61333SAriff Abdullah 	int error;
727e4e61333SAriff Abdullah 
728e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
729285648f9SCameron Grant 
730bba4862cSAriff Abdullah 	ch = CHN_FIRST(d, channels.pcm);
731285648f9SCameron Grant 
73290da2b28SAriff Abdullah 	PCM_LOCK(d);
733bba4862cSAriff Abdullah 	error = pcm_chn_remove(d, ch);
73490da2b28SAriff Abdullah 	PCM_UNLOCK(d);
735e33bee07SOlivier Houchard 	if (error)
736e33bee07SOlivier Houchard 		return (error);
737e33bee07SOlivier Houchard 	return (pcm_chn_destroy(ch));
738285648f9SCameron Grant }
739285648f9SCameron Grant 
740cbebc90dSAlexander Motin static int
741cbebc90dSAlexander Motin pcm_best_unit(int old)
742cbebc90dSAlexander Motin {
743cbebc90dSAlexander Motin 	struct snddev_info *d;
744cbebc90dSAlexander Motin 	int i, best, bestprio, prio;
745cbebc90dSAlexander Motin 
746cbebc90dSAlexander Motin 	best = -1;
747cbebc90dSAlexander Motin 	bestprio = -100;
748cbebc90dSAlexander Motin 	for (i = 0; pcm_devclass != NULL &&
749cbebc90dSAlexander Motin 	    i < devclass_get_maxunit(pcm_devclass); i++) {
750cbebc90dSAlexander Motin 		d = devclass_get_softc(pcm_devclass, i);
751cbebc90dSAlexander Motin 		if (!PCM_REGISTERED(d))
752cbebc90dSAlexander Motin 			continue;
753cbebc90dSAlexander Motin 		prio = 0;
754cbebc90dSAlexander Motin 		if (d->playcount == 0)
755cbebc90dSAlexander Motin 			prio -= 10;
756cbebc90dSAlexander Motin 		if (d->reccount == 0)
757cbebc90dSAlexander Motin 			prio -= 2;
758cbebc90dSAlexander Motin 		if (prio > bestprio || (prio == bestprio && i == old)) {
759cbebc90dSAlexander Motin 			best = i;
760cbebc90dSAlexander Motin 			bestprio = prio;
761cbebc90dSAlexander Motin 		}
762cbebc90dSAlexander Motin 	}
763cbebc90dSAlexander Motin 	return (best);
764cbebc90dSAlexander Motin }
765cbebc90dSAlexander Motin 
766285648f9SCameron Grant int
767987e5972SCameron Grant pcm_setstatus(device_t dev, char *str)
768987e5972SCameron Grant {
76966ef8af5SCameron Grant 	struct snddev_info *d = device_get_softc(dev);
77049c5e6e2SCameron Grant 
77132a0e5d5SHans Petter Selasky 	/* should only be called once */
77232a0e5d5SHans Petter Selasky 	if (d->flags & SD_F_REGISTERED)
77332a0e5d5SHans Petter Selasky 		return (EINVAL);
77432a0e5d5SHans Petter Selasky 
775e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
776bba4862cSAriff Abdullah 
777e4e61333SAriff Abdullah 	if (d->playcount == 0 || d->reccount == 0)
778e4e61333SAriff Abdullah 		d->flags |= SD_F_SIMPLEX;
779e4e61333SAriff Abdullah 
78032a0e5d5SHans Petter Selasky 	if (d->playcount > 0 || d->reccount > 0)
781e4e61333SAriff Abdullah 		d->flags |= SD_F_AUTOVCHAN;
782e4e61333SAriff Abdullah 
783e4e61333SAriff Abdullah 	pcm_setmaxautovchans(d, snd_maxautovchans);
784bba4862cSAriff Abdullah 
785a580b31aSAriff Abdullah 	strlcpy(d->status, str, SND_STATUSLEN);
786bba4862cSAriff Abdullah 
78790da2b28SAriff Abdullah 	PCM_LOCK(d);
788e4e61333SAriff Abdullah 
789bba4862cSAriff Abdullah 	/* Last stage, enable cloning. */
790e4e61333SAriff Abdullah 	if (d->clones != NULL)
791bba4862cSAriff Abdullah 		(void)snd_clone_enable(d->clones);
792e4e61333SAriff Abdullah 
793e4e61333SAriff Abdullah 	/* Done, we're ready.. */
794e4e61333SAriff Abdullah 	d->flags |= SD_F_REGISTERED;
795e4e61333SAriff Abdullah 
796e4e61333SAriff Abdullah 	PCM_RELEASE(d);
797bba4862cSAriff Abdullah 
79890da2b28SAriff Abdullah 	PCM_UNLOCK(d);
799bba4862cSAriff Abdullah 
80032a0e5d5SHans Petter Selasky 	/*
80132a0e5d5SHans Petter Selasky 	 * Create all sysctls once SD_F_REGISTERED is set else
80232a0e5d5SHans Petter Selasky 	 * tunable sysctls won't work:
80332a0e5d5SHans Petter Selasky 	 */
80432a0e5d5SHans Petter Selasky 	pcm_sysinit(dev);
80532a0e5d5SHans Petter Selasky 
806cbebc90dSAlexander Motin 	if (snd_unit_auto < 0)
807cbebc90dSAlexander Motin 		snd_unit_auto = (snd_unit < 0) ? 1 : 0;
808cbebc90dSAlexander Motin 	if (snd_unit < 0 || snd_unit_auto > 1)
809f3685841SAriff Abdullah 		snd_unit = device_get_unit(dev);
810cbebc90dSAlexander Motin 	else if (snd_unit_auto == 1)
811cbebc90dSAlexander Motin 		snd_unit = pcm_best_unit(snd_unit);
812f3685841SAriff Abdullah 
813e4e61333SAriff Abdullah 	return (0);
814987e5972SCameron Grant }
815987e5972SCameron Grant 
816a1d444e1SAriff Abdullah uint32_t
817987e5972SCameron Grant pcm_getflags(device_t dev)
818987e5972SCameron Grant {
81966ef8af5SCameron Grant 	struct snddev_info *d = device_get_softc(dev);
82049c5e6e2SCameron Grant 
821987e5972SCameron Grant 	return d->flags;
822987e5972SCameron Grant }
823987e5972SCameron Grant 
824987e5972SCameron Grant void
825a1d444e1SAriff Abdullah pcm_setflags(device_t dev, uint32_t val)
826987e5972SCameron Grant {
82766ef8af5SCameron Grant 	struct snddev_info *d = device_get_softc(dev);
828d95502a8SCameron Grant 
829987e5972SCameron Grant 	d->flags = val;
830987e5972SCameron Grant }
831987e5972SCameron Grant 
83239004e69SCameron Grant void *
83339004e69SCameron Grant pcm_getdevinfo(device_t dev)
83439004e69SCameron Grant {
83566ef8af5SCameron Grant 	struct snddev_info *d = device_get_softc(dev);
83649c5e6e2SCameron Grant 
83739004e69SCameron Grant 	return d->devinfo;
83839004e69SCameron Grant }
83939004e69SCameron Grant 
840a67fe5c1SCameron Grant unsigned int
841d55d96f6SAlexander Leidinger pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz)
842a67fe5c1SCameron Grant {
843a67fe5c1SCameron Grant 	struct snddev_info *d = device_get_softc(dev);
8444e60be34SCameron Grant 	int sz, x;
845a67fe5c1SCameron Grant 
846a67fe5c1SCameron Grant 	sz = 0;
8474e60be34SCameron Grant 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "buffersize", &sz) == 0) {
8484e60be34SCameron Grant 		x = sz;
849d55d96f6SAlexander Leidinger 		RANGE(sz, minbufsz, maxbufsz);
8504e60be34SCameron Grant 		if (x != sz)
851d55d96f6SAlexander Leidinger 			device_printf(dev, "'buffersize=%d' hint is out of range (%d-%d), using %d\n", x, minbufsz, maxbufsz, sz);
852d55d96f6SAlexander Leidinger 		x = minbufsz;
8534e60be34SCameron Grant 		while (x < sz)
8544e60be34SCameron Grant 			x <<= 1;
8554e60be34SCameron Grant 		if (x > sz)
8564e60be34SCameron Grant 			x >>= 1;
8574e60be34SCameron Grant 		if (x != sz) {
8584e60be34SCameron Grant 			device_printf(dev, "'buffersize=%d' hint is not a power of 2, using %d\n", sz, x);
8594e60be34SCameron Grant 			sz = x;
8604e60be34SCameron Grant 		}
8614e60be34SCameron Grant 	} else {
862a67fe5c1SCameron Grant 		sz = deflt;
8634e60be34SCameron Grant 	}
8644e60be34SCameron Grant 
865a67fe5c1SCameron Grant 	d->bufsz = sz;
866a67fe5c1SCameron Grant 
867a67fe5c1SCameron Grant 	return sz;
868a67fe5c1SCameron Grant }
869a67fe5c1SCameron Grant 
87090da2b28SAriff Abdullah static int
87190da2b28SAriff Abdullah sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)
87290da2b28SAriff Abdullah {
87390da2b28SAriff Abdullah 	struct snddev_info *d;
87490da2b28SAriff Abdullah 	int err, val;
87590da2b28SAriff Abdullah 
87690da2b28SAriff Abdullah 	d = oidp->oid_arg1;
87790da2b28SAriff Abdullah 	if (!PCM_REGISTERED(d))
87890da2b28SAriff Abdullah 		return (ENODEV);
87990da2b28SAriff Abdullah 
88090da2b28SAriff Abdullah 	PCM_LOCK(d);
88190da2b28SAriff Abdullah 	PCM_WAIT(d);
88290da2b28SAriff Abdullah 	val = (d->flags & SD_F_BITPERFECT) ? 1 : 0;
88390da2b28SAriff Abdullah 	PCM_ACQUIRE(d);
88490da2b28SAriff Abdullah 	PCM_UNLOCK(d);
88590da2b28SAriff Abdullah 
88690da2b28SAriff Abdullah 	err = sysctl_handle_int(oidp, &val, 0, req);
88790da2b28SAriff Abdullah 
88890da2b28SAriff Abdullah 	if (err == 0 && req->newptr != NULL) {
88990da2b28SAriff Abdullah 		if (!(val == 0 || val == 1)) {
89090da2b28SAriff Abdullah 			PCM_RELEASE_QUICK(d);
89190da2b28SAriff Abdullah 			return (EINVAL);
89290da2b28SAriff Abdullah 		}
89390da2b28SAriff Abdullah 
89490da2b28SAriff Abdullah 		PCM_LOCK(d);
89590da2b28SAriff Abdullah 
89690da2b28SAriff Abdullah 		d->flags &= ~SD_F_BITPERFECT;
89790da2b28SAriff Abdullah 		d->flags |= (val != 0) ? SD_F_BITPERFECT : 0;
89890da2b28SAriff Abdullah 
89990da2b28SAriff Abdullah 		PCM_RELEASE(d);
90090da2b28SAriff Abdullah 		PCM_UNLOCK(d);
90190da2b28SAriff Abdullah 	} else
90290da2b28SAriff Abdullah 		PCM_RELEASE_QUICK(d);
90390da2b28SAriff Abdullah 
90490da2b28SAriff Abdullah 	return (err);
90590da2b28SAriff Abdullah }
90690da2b28SAriff Abdullah 
90790da2b28SAriff Abdullah #ifdef SND_DEBUG
908bba4862cSAriff Abdullah static int
909bba4862cSAriff Abdullah sysctl_dev_pcm_clone_flags(SYSCTL_HANDLER_ARGS)
910bba4862cSAriff Abdullah {
911bba4862cSAriff Abdullah 	struct snddev_info *d;
912bba4862cSAriff Abdullah 	uint32_t flags;
913bba4862cSAriff Abdullah 	int err;
914bba4862cSAriff Abdullah 
915bba4862cSAriff Abdullah 	d = oidp->oid_arg1;
916e4e61333SAriff Abdullah 	if (!PCM_REGISTERED(d) || d->clones == NULL)
917bba4862cSAriff Abdullah 		return (ENODEV);
918bba4862cSAriff Abdullah 
919e4e61333SAriff Abdullah 	PCM_ACQUIRE_QUICK(d);
920e4e61333SAriff Abdullah 
921bba4862cSAriff Abdullah 	flags = snd_clone_getflags(d->clones);
922041b706bSDavid Malone 	err = sysctl_handle_int(oidp, &flags, 0, req);
923bba4862cSAriff Abdullah 
924bba4862cSAriff Abdullah 	if (err == 0 && req->newptr != NULL) {
925e4e61333SAriff Abdullah 		if (flags & ~SND_CLONE_MASK)
926bba4862cSAriff Abdullah 			err = EINVAL;
927e4e61333SAriff Abdullah 		else
928bba4862cSAriff Abdullah 			(void)snd_clone_setflags(d->clones, flags);
929bba4862cSAriff Abdullah 	}
930e4e61333SAriff Abdullah 
931e4e61333SAriff Abdullah 	PCM_RELEASE_QUICK(d);
932bba4862cSAriff Abdullah 
933bba4862cSAriff Abdullah 	return (err);
934bba4862cSAriff Abdullah }
935bba4862cSAriff Abdullah 
936bba4862cSAriff Abdullah static int
937bba4862cSAriff Abdullah sysctl_dev_pcm_clone_deadline(SYSCTL_HANDLER_ARGS)
938bba4862cSAriff Abdullah {
939bba4862cSAriff Abdullah 	struct snddev_info *d;
940bba4862cSAriff Abdullah 	int err, deadline;
941bba4862cSAriff Abdullah 
942bba4862cSAriff Abdullah 	d = oidp->oid_arg1;
943e4e61333SAriff Abdullah 	if (!PCM_REGISTERED(d) || d->clones == NULL)
944bba4862cSAriff Abdullah 		return (ENODEV);
945bba4862cSAriff Abdullah 
946e4e61333SAriff Abdullah 	PCM_ACQUIRE_QUICK(d);
947e4e61333SAriff Abdullah 
948bba4862cSAriff Abdullah 	deadline = snd_clone_getdeadline(d->clones);
949041b706bSDavid Malone 	err = sysctl_handle_int(oidp, &deadline, 0, req);
950bba4862cSAriff Abdullah 
951bba4862cSAriff Abdullah 	if (err == 0 && req->newptr != NULL) {
952bba4862cSAriff Abdullah 		if (deadline < 0)
953bba4862cSAriff Abdullah 			err = EINVAL;
954e4e61333SAriff Abdullah 		else
955bba4862cSAriff Abdullah 			(void)snd_clone_setdeadline(d->clones, deadline);
956bba4862cSAriff Abdullah 	}
957e4e61333SAriff Abdullah 
958e4e61333SAriff Abdullah 	PCM_RELEASE_QUICK(d);
959bba4862cSAriff Abdullah 
960bba4862cSAriff Abdullah 	return (err);
961bba4862cSAriff Abdullah }
962bba4862cSAriff Abdullah 
963bba4862cSAriff Abdullah static int
964bba4862cSAriff Abdullah sysctl_dev_pcm_clone_gc(SYSCTL_HANDLER_ARGS)
965bba4862cSAriff Abdullah {
966bba4862cSAriff Abdullah 	struct snddev_info *d;
967bba4862cSAriff Abdullah 	int err, val;
968bba4862cSAriff Abdullah 
969bba4862cSAriff Abdullah 	d = oidp->oid_arg1;
970e4e61333SAriff Abdullah 	if (!PCM_REGISTERED(d) || d->clones == NULL)
971bba4862cSAriff Abdullah 		return (ENODEV);
972bba4862cSAriff Abdullah 
973bba4862cSAriff Abdullah 	val = 0;
974041b706bSDavid Malone 	err = sysctl_handle_int(oidp, &val, 0, req);
975bba4862cSAriff Abdullah 
976bba4862cSAriff Abdullah 	if (err == 0 && req->newptr != NULL && val != 0) {
977e4e61333SAriff Abdullah 		PCM_ACQUIRE_QUICK(d);
978e4e61333SAriff Abdullah 		val = snd_clone_gc(d->clones);
979e4e61333SAriff Abdullah 		PCM_RELEASE_QUICK(d);
980e4e61333SAriff Abdullah 		if (bootverbose != 0 || snd_verbose > 3)
981e4e61333SAriff Abdullah 			device_printf(d->dev, "clone gc: pruned=%d\n", val);
982bba4862cSAriff Abdullah 	}
983bba4862cSAriff Abdullah 
984bba4862cSAriff Abdullah 	return (err);
985bba4862cSAriff Abdullah }
986bba4862cSAriff Abdullah 
987bba4862cSAriff Abdullah static int
988bba4862cSAriff Abdullah sysctl_hw_snd_clone_gc(SYSCTL_HANDLER_ARGS)
989bba4862cSAriff Abdullah {
990bba4862cSAriff Abdullah 	struct snddev_info *d;
991bba4862cSAriff Abdullah 	int i, err, val;
992bba4862cSAriff Abdullah 
993bba4862cSAriff Abdullah 	val = 0;
994041b706bSDavid Malone 	err = sysctl_handle_int(oidp, &val, 0, req);
995bba4862cSAriff Abdullah 
996bba4862cSAriff Abdullah 	if (err == 0 && req->newptr != NULL && val != 0) {
9979c271f79SAriff Abdullah 		for (i = 0; pcm_devclass != NULL &&
9989c271f79SAriff Abdullah 		    i < devclass_get_maxunit(pcm_devclass); i++) {
999bba4862cSAriff Abdullah 			d = devclass_get_softc(pcm_devclass, i);
1000e4e61333SAriff Abdullah 			if (!PCM_REGISTERED(d) || d->clones == NULL)
1001bba4862cSAriff Abdullah 				continue;
1002e4e61333SAriff Abdullah 			PCM_ACQUIRE_QUICK(d);
1003e4e61333SAriff Abdullah 			val = snd_clone_gc(d->clones);
1004e4e61333SAriff Abdullah 			PCM_RELEASE_QUICK(d);
1005e4e61333SAriff Abdullah 			if (bootverbose != 0 || snd_verbose > 3)
1006e4e61333SAriff Abdullah 				device_printf(d->dev, "clone gc: pruned=%d\n",
1007e4e61333SAriff Abdullah 				    val);
1008bba4862cSAriff Abdullah 		}
1009bba4862cSAriff Abdullah 	}
1010bba4862cSAriff Abdullah 
1011bba4862cSAriff Abdullah 	return (err);
1012bba4862cSAriff Abdullah }
10137029da5cSPawel Biernacki SYSCTL_PROC(_hw_snd, OID_AUTO, clone_gc,
10147029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
10157029da5cSPawel Biernacki     sysctl_hw_snd_clone_gc, "I",
1016bba4862cSAriff Abdullah     "global clone garbage collector");
1017bba4862cSAriff Abdullah #endif
1018bba4862cSAriff Abdullah 
101932a0e5d5SHans Petter Selasky static void
102032a0e5d5SHans Petter Selasky pcm_sysinit(device_t dev)
102132a0e5d5SHans Petter Selasky {
102232a0e5d5SHans Petter Selasky   	struct snddev_info *d = device_get_softc(dev);
102332a0e5d5SHans Petter Selasky 
102432a0e5d5SHans Petter Selasky   	/* XXX: an user should be able to set this with a control tool, the
102532a0e5d5SHans Petter Selasky 	   sysadmin then needs min+max sysctls for this */
102632a0e5d5SHans Petter Selasky 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
102732a0e5d5SHans Petter Selasky 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
102832a0e5d5SHans Petter Selasky             OID_AUTO, "buffersize", CTLFLAG_RD, &d->bufsz, 0, "allocated buffer size");
102932a0e5d5SHans Petter Selasky 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
103032a0e5d5SHans Petter Selasky 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
10317029da5cSPawel Biernacki 	    "bitperfect", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, d,
10327029da5cSPawel Biernacki 	    sizeof(d), sysctl_dev_pcm_bitperfect, "I",
103332a0e5d5SHans Petter Selasky 	    "bit-perfect playback/recording (0=disable, 1=enable)");
103432a0e5d5SHans Petter Selasky #ifdef SND_DEBUG
103532a0e5d5SHans Petter Selasky 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
103632a0e5d5SHans Petter Selasky 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
10377029da5cSPawel Biernacki 	    "clone_flags", CTLTYPE_UINT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
10387029da5cSPawel Biernacki 	    d, sizeof(d), sysctl_dev_pcm_clone_flags, "IU",
103932a0e5d5SHans Petter Selasky 	    "clone flags");
104032a0e5d5SHans Petter Selasky 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
104132a0e5d5SHans Petter Selasky 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
10427029da5cSPawel Biernacki 	    "clone_deadline", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
10437029da5cSPawel Biernacki 	    d, sizeof(d), sysctl_dev_pcm_clone_deadline, "I",
104432a0e5d5SHans Petter Selasky 	    "clone expiration deadline (ms)");
104532a0e5d5SHans Petter Selasky 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
104632a0e5d5SHans Petter Selasky 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
10477029da5cSPawel Biernacki 	    "clone_gc",
10487029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, d, sizeof(d),
10497029da5cSPawel Biernacki 	    sysctl_dev_pcm_clone_gc, "I", "clone garbage collector");
105032a0e5d5SHans Petter Selasky #endif
105132a0e5d5SHans Petter Selasky 	if (d->flags & SD_F_AUTOVCHAN)
105232a0e5d5SHans Petter Selasky 		vchan_initsys(dev);
105332a0e5d5SHans Petter Selasky 	if (d->flags & SD_F_EQ)
105432a0e5d5SHans Petter Selasky 		feeder_eq_initsys(dev);
105532a0e5d5SHans Petter Selasky }
105632a0e5d5SHans Petter Selasky 
1057987e5972SCameron Grant int
1058987e5972SCameron Grant pcm_register(device_t dev, void *devinfo, int numplay, int numrec)
1059987e5972SCameron Grant {
1060bba4862cSAriff Abdullah 	struct snddev_info *d;
106190da2b28SAriff Abdullah 	int i;
1062987e5972SCameron Grant 
1063b8a36395SCameron Grant 	if (pcm_veto_load) {
1064b8a36395SCameron Grant 		device_printf(dev, "disabled due to an error while initialising: %d\n", pcm_veto_load);
1065b8a36395SCameron Grant 
1066b8a36395SCameron Grant 		return EINVAL;
1067b8a36395SCameron Grant 	}
1068b8a36395SCameron Grant 
1069bba4862cSAriff Abdullah 	if (device_get_unit(dev) > PCMMAXUNIT) {
1070bba4862cSAriff Abdullah 		device_printf(dev, "PCMMAXUNIT reached : unit=%d > %d\n",
1071bba4862cSAriff Abdullah 		    device_get_unit(dev), PCMMAXUNIT);
1072bba4862cSAriff Abdullah 		device_printf(dev,
1073bba4862cSAriff Abdullah 		    "Use 'hw.snd.maxunit' tunable to raise the limit.\n");
1074bba4862cSAriff Abdullah 		return ENODEV;
1075bba4862cSAriff Abdullah 	}
1076bba4862cSAriff Abdullah 
1077bba4862cSAriff Abdullah 	d = device_get_softc(dev);
1078e4e61333SAriff Abdullah 	d->dev = dev;
10792c69ba87SJohn Baldwin 	d->lock = snd_mtxcreate(device_get_nameunit(dev), "sound cdev");
1080e4e61333SAriff Abdullah 	cv_init(&d->cv, device_get_nameunit(dev));
1081e4e61333SAriff Abdullah 	PCM_ACQUIRE_QUICK(d);
1082e4e61333SAriff Abdullah 	dsp_cdevinfo_init(d);
10837233ababSAlexander Leidinger #if 0
10847233ababSAlexander Leidinger 	/*
10857233ababSAlexander Leidinger 	 * d->flags should be cleared by the allocator of the softc.
10867233ababSAlexander Leidinger 	 * We cannot clear this field here because several devices set
10877233ababSAlexander Leidinger 	 * this flag before calling pcm_register().
10887233ababSAlexander Leidinger 	 */
1089cd9766c5SCameron Grant 	d->flags = 0;
10907233ababSAlexander Leidinger #endif
109190da2b28SAriff Abdullah 	i = 0;
109290da2b28SAriff Abdullah 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
109390da2b28SAriff Abdullah 	    "vpc", &i) != 0 || i != 0)
109490da2b28SAriff Abdullah 		d->flags |= SD_F_VPC;
109590da2b28SAriff Abdullah 
109690da2b28SAriff Abdullah 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
109790da2b28SAriff Abdullah 	    "bitperfect", &i) == 0 && i != 0)
109890da2b28SAriff Abdullah 		d->flags |= SD_F_BITPERFECT;
109990da2b28SAriff Abdullah 
1100987e5972SCameron Grant 	d->devinfo = devinfo;
1101f637a36cSCameron Grant 	d->devcount = 0;
1102506a5308SCameron Grant 	d->reccount = 0;
1103a67fe5c1SCameron Grant 	d->playcount = 0;
1104bba4862cSAriff Abdullah 	d->pvchancount = 0;
1105bba4862cSAriff Abdullah 	d->rvchancount = 0;
1106bba4862cSAriff Abdullah 	d->pvchanrate = 0;
1107bba4862cSAriff Abdullah 	d->pvchanformat = 0;
1108bba4862cSAriff Abdullah 	d->rvchanrate = 0;
1109bba4862cSAriff Abdullah 	d->rvchanformat = 0;
1110d95502a8SCameron Grant 	d->inprog = 0;
1111833f7023SCameron Grant 
1112bba4862cSAriff Abdullah 	/*
1113bba4862cSAriff Abdullah 	 * Create clone manager, disabled by default. Cloning will be
111490da2b28SAriff Abdullah 	 * enabled during final stage of driver initialization through
1115bba4862cSAriff Abdullah 	 * pcm_setstatus().
1116bba4862cSAriff Abdullah 	 */
1117e4e61333SAriff Abdullah 	d->clones = snd_clone_create(SND_U_MASK | SND_D_MASK, PCMMAXCLONE,
1118e4e61333SAriff Abdullah 	    SND_CLONE_DEADLINE_DEFAULT, SND_CLONE_WAITOK |
1119bba4862cSAriff Abdullah 	    SND_CLONE_GC_ENABLE | SND_CLONE_GC_UNREF |
1120bba4862cSAriff Abdullah 	    SND_CLONE_GC_LASTREF | SND_CLONE_GC_EXPIRED);
1121bba4862cSAriff Abdullah 
1122bba4862cSAriff Abdullah 	CHN_INIT(d, channels.pcm);
1123bba4862cSAriff Abdullah 	CHN_INIT(d, channels.pcm.busy);
112490da2b28SAriff Abdullah 	CHN_INIT(d, channels.pcm.opened);
112545550658SPoul-Henning Kamp 
1126e4e61333SAriff Abdullah 	/* XXX This is incorrect, but lets play along for now. */
1127a1d444e1SAriff Abdullah 	if ((numplay == 0 || numrec == 0) && numplay != numrec)
1128285648f9SCameron Grant 		d->flags |= SD_F_SIMPLEX;
1129d95502a8SCameron Grant 
1130bba4862cSAriff Abdullah 	sysctl_ctx_init(&d->play_sysctl_ctx);
1131bba4862cSAriff Abdullah 	d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx,
1132bba4862cSAriff Abdullah 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "play",
11337029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "playback channels node");
1134bba4862cSAriff Abdullah 	sysctl_ctx_init(&d->rec_sysctl_ctx);
1135bba4862cSAriff Abdullah 	d->rec_sysctl_tree = SYSCTL_ADD_NODE(&d->rec_sysctl_ctx,
1136bba4862cSAriff Abdullah 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "rec",
11377029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "record channels node");
1138e4e61333SAriff Abdullah 
113932a0e5d5SHans Petter Selasky 	if (numplay > 0 || numrec > 0)
1140cd9766c5SCameron Grant 		d->flags |= SD_F_AUTOVCHAN;
114190da2b28SAriff Abdullah 
114267b1dce3SCameron Grant 	sndstat_register(dev, d->status, sndstat_prepare_pcm);
1143e4e61333SAriff Abdullah 
1144987e5972SCameron Grant 	return 0;
1145987e5972SCameron Grant }
1146987e5972SCameron Grant 
114733dbf14aSCameron Grant int
114833dbf14aSCameron Grant pcm_unregister(device_t dev)
11497c438dbeSCameron Grant {
1150e4e61333SAriff Abdullah 	struct snddev_info *d;
1151a67fe5c1SCameron Grant 	struct pcm_channel *ch;
1152bba4862cSAriff Abdullah 	struct thread *td;
11537c438dbeSCameron Grant 
1154bba4862cSAriff Abdullah 	td = curthread;
1155e4e61333SAriff Abdullah 	d = device_get_softc(dev);
1156e4e61333SAriff Abdullah 
1157e4e61333SAriff Abdullah 	if (!PCM_ALIVE(d)) {
1158e4e61333SAriff Abdullah 		device_printf(dev, "unregister: device not configured\n");
1159e4e61333SAriff Abdullah 		return (0);
1160e4e61333SAriff Abdullah 	}
1161bba4862cSAriff Abdullah 
116290da2b28SAriff Abdullah 	PCM_LOCK(d);
1163e4e61333SAriff Abdullah 	PCM_WAIT(d);
1164e4e61333SAriff Abdullah 
1165*cc1efc23SHans Petter Selasky 	d->flags |= SD_F_DETACHING;
1166*cc1efc23SHans Petter Selasky 
1167e4e61333SAriff Abdullah 	if (d->inprog != 0) {
11685c25132aSGeorge C A Reid 		device_printf(dev, "unregister: operation in progress\n");
116990da2b28SAriff Abdullah 		PCM_UNLOCK(d);
1170e4e61333SAriff Abdullah 		return (EBUSY);
11715c25132aSGeorge C A Reid 	}
11725ee30e27SMathew Kanner 
1173e4e61333SAriff Abdullah 	PCM_ACQUIRE(d);
117490da2b28SAriff Abdullah 	PCM_UNLOCK(d);
1175e4e61333SAriff Abdullah 
1176e4e61333SAriff Abdullah 	CHN_FOREACH(ch, d, channels.pcm) {
1177e4e61333SAriff Abdullah 		CHN_LOCK(ch);
1178e4e61333SAriff Abdullah 		if (ch->refcount > 0) {
1179e4e61333SAriff Abdullah 			device_printf(dev,
1180e4e61333SAriff Abdullah 			    "unregister: channel %s busy (pid %d)\n",
1181e4e61333SAriff Abdullah 			    ch->name, ch->pid);
1182e4e61333SAriff Abdullah 			CHN_UNLOCK(ch);
1183e4e61333SAriff Abdullah 			PCM_RELEASE_QUICK(d);
1184e4e61333SAriff Abdullah 			return (EBUSY);
1185285648f9SCameron Grant 		}
1186e4e61333SAriff Abdullah 		CHN_UNLOCK(ch);
1187c9b53085SCameron Grant 	}
11885ee30e27SMathew Kanner 
1189bba4862cSAriff Abdullah 	if (d->clones != NULL) {
1190bba4862cSAriff Abdullah 		if (snd_clone_busy(d->clones) != 0) {
1191bba4862cSAriff Abdullah 			device_printf(dev, "unregister: clone busy\n");
1192e4e61333SAriff Abdullah 			PCM_RELEASE_QUICK(d);
1193e4e61333SAriff Abdullah 			return (EBUSY);
1194e4e61333SAriff Abdullah 		} else {
119590da2b28SAriff Abdullah 			PCM_LOCK(d);
1196bba4862cSAriff Abdullah 			(void)snd_clone_disable(d->clones);
119790da2b28SAriff Abdullah 			PCM_UNLOCK(d);
1198e4e61333SAriff Abdullah 		}
1199bba4862cSAriff Abdullah 	}
1200bba4862cSAriff Abdullah 
1201b4221868SAriff Abdullah 	if (mixer_uninit(dev) == EBUSY) {
12027233ababSAlexander Leidinger 		device_printf(dev, "unregister: mixer busy\n");
120390da2b28SAriff Abdullah 		PCM_LOCK(d);
1204bba4862cSAriff Abdullah 		if (d->clones != NULL)
1205bba4862cSAriff Abdullah 			(void)snd_clone_enable(d->clones);
1206e4e61333SAriff Abdullah 		PCM_RELEASE(d);
120790da2b28SAriff Abdullah 		PCM_UNLOCK(d);
1208e4e61333SAriff Abdullah 		return (EBUSY);
12097233ababSAlexander Leidinger 	}
12107233ababSAlexander Leidinger 
12118461d581SHans Petter Selasky 	/* remove /dev/sndstat entry first */
12128461d581SHans Petter Selasky 	sndstat_unregister(dev);
12138461d581SHans Petter Selasky 
121490da2b28SAriff Abdullah 	PCM_LOCK(d);
1215e4e61333SAriff Abdullah 	d->flags |= SD_F_DYING;
1216e4e61333SAriff Abdullah 	d->flags &= ~SD_F_REGISTERED;
121790da2b28SAriff Abdullah 	PCM_UNLOCK(d);
1218e4e61333SAriff Abdullah 
1219e4e61333SAriff Abdullah 	/*
1220e4e61333SAriff Abdullah 	 * No lock being held, so this thing can be flushed without
1221e4e61333SAriff Abdullah 	 * stucking into devdrn oblivion.
1222e4e61333SAriff Abdullah 	 */
1223bba4862cSAriff Abdullah 	if (d->clones != NULL) {
1224bba4862cSAriff Abdullah 		snd_clone_destroy(d->clones);
1225bba4862cSAriff Abdullah 		d->clones = NULL;
12267982e7a4SAriff Abdullah 	}
1227bba4862cSAriff Abdullah 
1228bba4862cSAriff Abdullah 	if (d->play_sysctl_tree != NULL) {
1229bba4862cSAriff Abdullah 		sysctl_ctx_free(&d->play_sysctl_ctx);
1230bba4862cSAriff Abdullah 		d->play_sysctl_tree = NULL;
1231bba4862cSAriff Abdullah 	}
1232bba4862cSAriff Abdullah 	if (d->rec_sysctl_tree != NULL) {
1233bba4862cSAriff Abdullah 		sysctl_ctx_free(&d->rec_sysctl_ctx);
1234bba4862cSAriff Abdullah 		d->rec_sysctl_tree = NULL;
1235a1d444e1SAriff Abdullah 	}
1236bba4862cSAriff Abdullah 
1237bba4862cSAriff Abdullah 	while (!CHN_EMPTY(d, channels.pcm))
1238285648f9SCameron Grant 		pcm_killchan(dev);
12397c438dbeSCameron Grant 
1240e4e61333SAriff Abdullah 	dsp_cdevinfo_flush(d);
1241e4e61333SAriff Abdullah 
124290da2b28SAriff Abdullah 	PCM_LOCK(d);
1243e4e61333SAriff Abdullah 	PCM_RELEASE(d);
1244e4e61333SAriff Abdullah 	cv_destroy(&d->cv);
124590da2b28SAriff Abdullah 	PCM_UNLOCK(d);
124649c5e6e2SCameron Grant 	snd_mtxfree(d->lock);
1247bba4862cSAriff Abdullah 
1248bba4862cSAriff Abdullah 	if (snd_unit == device_get_unit(dev)) {
1249cbebc90dSAlexander Motin 		snd_unit = pcm_best_unit(-1);
1250cbebc90dSAlexander Motin 		if (snd_unit_auto == 0)
1251cbebc90dSAlexander Motin 			snd_unit_auto = 1;
1252e4e61333SAriff Abdullah 	}
1253bba4862cSAriff Abdullah 
1254e4e61333SAriff Abdullah 	return (0);
125533dbf14aSCameron Grant }
12567c438dbeSCameron Grant 
125767b1dce3SCameron Grant /************************************************************************/
125867b1dce3SCameron Grant 
1259b611c801SAlexander Leidinger /**
1260b611c801SAlexander Leidinger  * @brief	Handle OSSv4 SNDCTL_SYSINFO ioctl.
1261b611c801SAlexander Leidinger  *
1262b611c801SAlexander Leidinger  * @param si	Pointer to oss_sysinfo struct where information about the
1263b611c801SAlexander Leidinger  * 		sound subsystem will be written/copied.
1264b611c801SAlexander Leidinger  *
1265b611c801SAlexander Leidinger  * This routine returns information about the sound system, such as the
1266b611c801SAlexander Leidinger  * current OSS version, number of audio, MIDI, and mixer drivers, etc.
1267b611c801SAlexander Leidinger  * Also includes a bitmask showing which of the above types of devices
1268b611c801SAlexander Leidinger  * are open (busy).
1269b611c801SAlexander Leidinger  *
1270b611c801SAlexander Leidinger  * @note
1271b611c801SAlexander Leidinger  * Calling threads must not hold any snddev_info or pcm_channel locks.
1272b611c801SAlexander Leidinger  *
1273b611c801SAlexander Leidinger  * @author	Ryan Beasley <ryanb@FreeBSD.org>
1274b611c801SAlexander Leidinger  */
1275b611c801SAlexander Leidinger void
1276b611c801SAlexander Leidinger sound_oss_sysinfo(oss_sysinfo *si)
1277b611c801SAlexander Leidinger {
1278b611c801SAlexander Leidinger 	static char si_product[] = "FreeBSD native OSS ABI";
1279b611c801SAlexander Leidinger 	static char si_version[] = __XSTRING(__FreeBSD_version);
1280c412d503SAlexander Motin 	static char si_license[] = "BSD";
1281b611c801SAlexander Leidinger 	static int intnbits = sizeof(int) * 8;	/* Better suited as macro?
1282b611c801SAlexander Leidinger 						   Must pester a C guru. */
1283b611c801SAlexander Leidinger 
1284b611c801SAlexander Leidinger 	struct snddev_info *d;
1285b611c801SAlexander Leidinger 	struct pcm_channel *c;
1286b611c801SAlexander Leidinger 	int i, j, ncards;
1287b611c801SAlexander Leidinger 
1288b611c801SAlexander Leidinger 	ncards = 0;
1289b611c801SAlexander Leidinger 
1290b611c801SAlexander Leidinger 	strlcpy(si->product, si_product, sizeof(si->product));
1291b611c801SAlexander Leidinger 	strlcpy(si->version, si_version, sizeof(si->version));
1292b611c801SAlexander Leidinger 	si->versionnum = SOUND_VERSION;
1293c412d503SAlexander Motin 	strlcpy(si->license, si_license, sizeof(si->license));
1294b611c801SAlexander Leidinger 
1295b611c801SAlexander Leidinger 	/*
1296b611c801SAlexander Leidinger 	 * Iterate over PCM devices and their channels, gathering up data
1297b611c801SAlexander Leidinger 	 * for the numaudios, ncards, and openedaudio fields.
1298b611c801SAlexander Leidinger 	 */
1299b611c801SAlexander Leidinger 	si->numaudios = 0;
1300b611c801SAlexander Leidinger 	bzero((void *)&si->openedaudio, sizeof(si->openedaudio));
1301b611c801SAlexander Leidinger 
1302b611c801SAlexander Leidinger 	j = 0;
1303b611c801SAlexander Leidinger 
13049c271f79SAriff Abdullah 	for (i = 0; pcm_devclass != NULL &&
13059c271f79SAriff Abdullah 	    i < devclass_get_maxunit(pcm_devclass); i++) {
1306b611c801SAlexander Leidinger 		d = devclass_get_softc(pcm_devclass, i);
1307e4e61333SAriff Abdullah 		if (!PCM_REGISTERED(d))
1308b611c801SAlexander Leidinger 			continue;
1309b611c801SAlexander Leidinger 
1310e4e61333SAriff Abdullah 		/* XXX Need Giant magic entry ??? */
1311e4e61333SAriff Abdullah 
1312b611c801SAlexander Leidinger 		/* See note in function's docblock */
131390da2b28SAriff Abdullah 		PCM_UNLOCKASSERT(d);
131490da2b28SAriff Abdullah 		PCM_LOCK(d);
1315b611c801SAlexander Leidinger 
1316b611c801SAlexander Leidinger 		si->numaudios += d->devcount;
1317b611c801SAlexander Leidinger 		++ncards;
1318b611c801SAlexander Leidinger 
1319bba4862cSAriff Abdullah 		CHN_FOREACH(c, d, channels.pcm) {
132090da2b28SAriff Abdullah 			CHN_UNLOCKASSERT(c);
1321b611c801SAlexander Leidinger 			CHN_LOCK(c);
1322b611c801SAlexander Leidinger 			if (c->flags & CHN_F_BUSY)
1323b611c801SAlexander Leidinger 				si->openedaudio[j / intnbits] |=
1324b611c801SAlexander Leidinger 				    (1 << (j % intnbits));
1325b611c801SAlexander Leidinger 			CHN_UNLOCK(c);
1326b611c801SAlexander Leidinger 			j++;
1327b611c801SAlexander Leidinger 		}
1328b611c801SAlexander Leidinger 
132990da2b28SAriff Abdullah 		PCM_UNLOCK(d);
1330b611c801SAlexander Leidinger 	}
1331c412d503SAlexander Motin 	si->numaudioengines = si->numaudios;
1332b611c801SAlexander Leidinger 
1333b611c801SAlexander Leidinger 	si->numsynths = 0;	/* OSSv4 docs:  this field is obsolete */
1334b611c801SAlexander Leidinger 	/**
1335b611c801SAlexander Leidinger 	 * @todo	Collect num{midis,timers}.
1336b611c801SAlexander Leidinger 	 *
1337b611c801SAlexander Leidinger 	 * Need access to sound/midi/midi.c::midistat_lock in order
1338b611c801SAlexander Leidinger 	 * to safely touch midi_devices and get a head count of, well,
1339b611c801SAlexander Leidinger 	 * MIDI devices.  midistat_lock is a global static (i.e., local to
1340b611c801SAlexander Leidinger 	 * midi.c), but midi_devices is a regular global; should the mutex
1341b611c801SAlexander Leidinger 	 * be publicized, or is there another way to get this information?
1342b611c801SAlexander Leidinger 	 *
1343b611c801SAlexander Leidinger 	 * NB:	MIDI/sequencer stuff is currently on hold.
1344b611c801SAlexander Leidinger 	 */
1345b611c801SAlexander Leidinger 	si->nummidis = 0;
1346b611c801SAlexander Leidinger 	si->numtimers = 0;
1347b611c801SAlexander Leidinger 	si->nummixers = mixer_count;
1348b611c801SAlexander Leidinger 	si->numcards = ncards;
1349b611c801SAlexander Leidinger 		/* OSSv4 docs:	Intended only for test apps; API doesn't
1350b611c801SAlexander Leidinger 		   really have much of a concept of cards.  Shouldn't be
1351b611c801SAlexander Leidinger 		   used by applications. */
1352b611c801SAlexander Leidinger 
1353b611c801SAlexander Leidinger 	/**
1354b611c801SAlexander Leidinger 	 * @todo	Fill in "busy devices" fields.
1355b611c801SAlexander Leidinger 	 *
1356b611c801SAlexander Leidinger 	 *  si->openedmidi = " MIDI devices
1357b611c801SAlexander Leidinger 	 */
1358b611c801SAlexander Leidinger 	bzero((void *)&si->openedmidi, sizeof(si->openedmidi));
1359b611c801SAlexander Leidinger 
1360b611c801SAlexander Leidinger 	/*
1361b611c801SAlexander Leidinger 	 * Si->filler is a reserved array, but according to docs each
1362b611c801SAlexander Leidinger 	 * element should be set to -1.
1363b611c801SAlexander Leidinger 	 */
1364b611c801SAlexander Leidinger 	for (i = 0; i < sizeof(si->filler)/sizeof(si->filler[0]); i++)
1365b611c801SAlexander Leidinger 		si->filler[i] = -1;
1366b611c801SAlexander Leidinger }
1367b611c801SAlexander Leidinger 
136852f6e09eSAlexander Motin int
136952f6e09eSAlexander Motin sound_oss_card_info(oss_card_info *si)
137052f6e09eSAlexander Motin {
137152f6e09eSAlexander Motin 	struct snddev_info *d;
137252f6e09eSAlexander Motin 	int i, ncards;
137352f6e09eSAlexander Motin 
137452f6e09eSAlexander Motin 	ncards = 0;
137552f6e09eSAlexander Motin 
137652f6e09eSAlexander Motin 	for (i = 0; pcm_devclass != NULL &&
137752f6e09eSAlexander Motin 	    i < devclass_get_maxunit(pcm_devclass); i++) {
137852f6e09eSAlexander Motin 		d = devclass_get_softc(pcm_devclass, i);
137952f6e09eSAlexander Motin 		if (!PCM_REGISTERED(d))
138052f6e09eSAlexander Motin 			continue;
138152f6e09eSAlexander Motin 
138252f6e09eSAlexander Motin 		if (ncards++ != si->card)
138352f6e09eSAlexander Motin 			continue;
138452f6e09eSAlexander Motin 
138590da2b28SAriff Abdullah 		PCM_UNLOCKASSERT(d);
138690da2b28SAriff Abdullah 		PCM_LOCK(d);
138752f6e09eSAlexander Motin 
138852f6e09eSAlexander Motin 		strlcpy(si->shortname, device_get_nameunit(d->dev),
138952f6e09eSAlexander Motin 		    sizeof(si->shortname));
139052f6e09eSAlexander Motin 		strlcpy(si->longname, device_get_desc(d->dev),
139152f6e09eSAlexander Motin 		    sizeof(si->longname));
139252f6e09eSAlexander Motin 		strlcpy(si->hw_info, d->status, sizeof(si->hw_info));
139352f6e09eSAlexander Motin 		si->intr_count = si->ack_count = 0;
139490da2b28SAriff Abdullah 
139590da2b28SAriff Abdullah 		PCM_UNLOCK(d);
139690da2b28SAriff Abdullah 
139752f6e09eSAlexander Motin 		return (0);
139852f6e09eSAlexander Motin 	}
139952f6e09eSAlexander Motin 	return (ENXIO);
140052f6e09eSAlexander Motin }
140152f6e09eSAlexander Motin 
1402b611c801SAlexander Leidinger /************************************************************************/
1403b611c801SAlexander Leidinger 
14040739ea1dSSeigo Tanimura static int
14050739ea1dSSeigo Tanimura sound_modevent(module_t mod, int type, void *data)
14060739ea1dSSeigo Tanimura {
1407b611c801SAlexander Leidinger 	int ret;
14087233ababSAlexander Leidinger #if 0
14090739ea1dSSeigo Tanimura 	return (midi_modevent(mod, type, data));
14107233ababSAlexander Leidinger #else
1411b611c801SAlexander Leidinger 	ret = 0;
1412b611c801SAlexander Leidinger 
1413b611c801SAlexander Leidinger 	switch(type) {
1414b611c801SAlexander Leidinger 		case MOD_LOAD:
1415b611c801SAlexander Leidinger 			pcmsg_unrhdr = new_unrhdr(1, INT_MAX, NULL);
1416b611c801SAlexander Leidinger 			break;
1417b611c801SAlexander Leidinger 		case MOD_UNLOAD:
1418b611c801SAlexander Leidinger 			if (pcmsg_unrhdr != NULL) {
1419b611c801SAlexander Leidinger 				delete_unrhdr(pcmsg_unrhdr);
1420b611c801SAlexander Leidinger 				pcmsg_unrhdr = NULL;
1421b611c801SAlexander Leidinger 			}
1422b611c801SAlexander Leidinger 			break;
14235525b53dSAlexander Motin 		case MOD_SHUTDOWN:
14245525b53dSAlexander Motin 			break;
1425b611c801SAlexander Leidinger 		default:
142690da2b28SAriff Abdullah 			ret = ENOTSUP;
1427b611c801SAlexander Leidinger 	}
1428b611c801SAlexander Leidinger 
1429b611c801SAlexander Leidinger 	return ret;
14307233ababSAlexander Leidinger #endif
14310739ea1dSSeigo Tanimura }
14320739ea1dSSeigo Tanimura 
14330739ea1dSSeigo Tanimura DEV_MODULE(sound, sound_modevent, NULL);
14340739ea1dSSeigo Tanimura MODULE_VERSION(sound, SOUND_MODVER);
1435