xref: /freebsd/sys/dev/sound/pcm/sound.c (revision b3ea087c05d8c75978a302cbb3fa92ce1afa3e49)
1098ca2bdSWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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.
94f854658SChristos Margiolis  * Copyright (c) 2024 The FreeBSD Foundation
104f854658SChristos Margiolis  *
114f854658SChristos Margiolis  * Portions of this software were developed by Christos Margiolis
124f854658SChristos Margiolis  * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
13987e5972SCameron Grant  *
14987e5972SCameron Grant  * Redistribution and use in source and binary forms, with or without
15987e5972SCameron Grant  * modification, are permitted provided that the following conditions
16987e5972SCameron Grant  * are met:
17987e5972SCameron Grant  * 1. Redistributions of source code must retain the above copyright
18987e5972SCameron Grant  *    notice, this list of conditions and the following disclaimer.
19987e5972SCameron Grant  * 2. Redistributions in binary form must reproduce the above copyright
20987e5972SCameron Grant  *    notice, this list of conditions and the following disclaimer in the
21987e5972SCameron Grant  *    documentation and/or other materials provided with the distribution.
22987e5972SCameron Grant  *
23987e5972SCameron Grant  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24987e5972SCameron Grant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25987e5972SCameron Grant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26987e5972SCameron Grant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27987e5972SCameron Grant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28987e5972SCameron Grant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29987e5972SCameron Grant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30987e5972SCameron Grant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31987e5972SCameron Grant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32987e5972SCameron Grant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33987e5972SCameron Grant  * SUCH DAMAGE.
34987e5972SCameron Grant  */
35987e5972SCameron Grant 
3690da2b28SAriff Abdullah #ifdef HAVE_KERNEL_OPTION_HEADERS
3790da2b28SAriff Abdullah #include "opt_snd.h"
3890da2b28SAriff Abdullah #endif
3990da2b28SAriff Abdullah 
40ef9308b1SCameron Grant #include <dev/sound/pcm/sound.h>
41a580b31aSAriff Abdullah #include <dev/sound/pcm/ac97.h>
42285648f9SCameron Grant #include <dev/sound/pcm/vchan.h>
435ee30e27SMathew Kanner #include <dev/sound/pcm/dsp.h>
44bba4862cSAriff Abdullah #include <dev/sound/version.h>
45b611c801SAlexander Leidinger #include <sys/limits.h>
467c438dbeSCameron Grant #include <sys/sysctl.h>
47285648f9SCameron Grant 
4867b1dce3SCameron Grant #include "feeder_if.h"
4967b1dce3SCameron Grant 
50d95502a8SCameron Grant devclass_t pcm_devclass;
5182db23e2SCameron Grant 
52b8a36395SCameron Grant int pcm_veto_load = 1;
53b8a36395SCameron Grant 
54f3685841SAriff Abdullah int snd_unit = -1;
55cbe7d6a3SCameron Grant 
56cbebc90dSAlexander Motin static int snd_unit_auto = -1;
57af3b2549SHans Petter Selasky SYSCTL_INT(_hw_snd, OID_AUTO, default_auto, CTLFLAG_RWTUN,
58838d3589SAriff Abdullah     &snd_unit_auto, 0, "assign default unit to a newly attached device");
59ad8612b9SAriff Abdullah 
60bba4862cSAriff Abdullah int snd_maxautovchans = 16;
61987e5972SCameron Grant 
627029da5cSPawel Biernacki SYSCTL_NODE(_hw, OID_AUTO, snd, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
637029da5cSPawel Biernacki     "Sound driver");
6482db23e2SCameron Grant 
6532a0e5d5SHans Petter Selasky static void pcm_sysinit(device_t);
6632a0e5d5SHans Petter Selasky 
67bba4862cSAriff Abdullah /*
68bba4862cSAriff Abdullah  * XXX I've had enough with people not telling proper version/arch
69bba4862cSAriff Abdullah  *     while reporting problems, not after 387397913213th questions/requests.
70bba4862cSAriff Abdullah  */
71a9bdb5d3SAndriy Gapon static char snd_driver_version[] =
72bba4862cSAriff Abdullah     __XSTRING(SND_DRV_VERSION)"/"MACHINE_ARCH;
73bba4862cSAriff Abdullah SYSCTL_STRING(_hw_snd, OID_AUTO, version, CTLFLAG_RD, &snd_driver_version,
7490da2b28SAriff Abdullah     0, "driver version/arch");
75bba4862cSAriff Abdullah 
76b611c801SAlexander Leidinger /**
77b611c801SAlexander Leidinger  * @brief Unit number allocator for syncgroup IDs
78b611c801SAlexander Leidinger  */
79b611c801SAlexander Leidinger struct unrhdr *pcmsg_unrhdr = NULL;
80b611c801SAlexander Leidinger 
8137209180SCameron Grant void *
822c69ba87SJohn Baldwin snd_mtxcreate(const char *desc, const char *type)
8337209180SCameron Grant {
8437209180SCameron Grant 	struct mtx *m;
8537209180SCameron Grant 
86a163d034SWarner Losh 	m = malloc(sizeof(*m), M_DEVBUF, M_WAITOK | M_ZERO);
8712e524a2SDon Lewis 	mtx_init(m, desc, type, MTX_DEF);
8812e524a2SDon Lewis 	return m;
8912e524a2SDon Lewis }
9012e524a2SDon Lewis 
9137209180SCameron Grant void
9237209180SCameron Grant snd_mtxfree(void *m)
9337209180SCameron Grant {
9437209180SCameron Grant 	struct mtx *mtx = m;
9537209180SCameron Grant 
9637209180SCameron Grant 	mtx_destroy(mtx);
9737209180SCameron Grant 	free(mtx, M_DEVBUF);
9837209180SCameron Grant }
9937209180SCameron Grant 
10037209180SCameron Grant void
10137209180SCameron Grant snd_mtxassert(void *m)
10237209180SCameron Grant {
103f00f162aSCameron Grant #ifdef INVARIANTS
10437209180SCameron Grant 	struct mtx *mtx = m;
10537209180SCameron Grant 
10637209180SCameron Grant 	mtx_assert(mtx, MA_OWNED);
10737209180SCameron Grant #endif
10837209180SCameron Grant }
10937209180SCameron Grant 
11037209180SCameron Grant int
11137209180SCameron Grant snd_setup_intr(device_t dev, struct resource *res, int flags, driver_intr_t hand, void *param, void **cookiep)
11237209180SCameron Grant {
113e4e61333SAriff Abdullah 	struct snddev_info *d;
11490da2b28SAriff Abdullah 
11537209180SCameron Grant 	flags &= INTR_MPSAFE;
11646700f12SPeter Wemm 	flags |= INTR_TYPE_AV;
117e4e61333SAriff Abdullah 	d = device_get_softc(dev);
118e4e61333SAriff Abdullah 	if (d != NULL && (flags & INTR_MPSAFE))
119e4e61333SAriff Abdullah 		d->flags |= SD_F_MPSAFE;
120e4e61333SAriff Abdullah 
121775fcb6eSBaptiste Daroussin 	return bus_setup_intr(dev, res, flags, NULL, hand, param, cookiep);
12237209180SCameron Grant }
12337209180SCameron Grant 
12490da2b28SAriff Abdullah int
125bba4862cSAriff Abdullah pcm_setvchans(struct snddev_info *d, int direction, int newcnt, int num)
126bba4862cSAriff Abdullah {
127bba4862cSAriff Abdullah 	struct pcm_channel *c, *ch, *nch;
12890da2b28SAriff Abdullah 	struct pcmchan_caps *caps;
12990da2b28SAriff Abdullah 	int i, err, vcnt;
130bba4862cSAriff Abdullah 
131e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
132a1d444e1SAriff Abdullah 
133bba4862cSAriff Abdullah 	if ((direction == PCMDIR_PLAY && d->playcount < 1) ||
134e4e61333SAriff Abdullah 	    (direction == PCMDIR_REC && d->reccount < 1))
135e4e61333SAriff Abdullah 		return (ENODEV);
136a580b31aSAriff Abdullah 
137e4e61333SAriff Abdullah 	if (!(d->flags & SD_F_AUTOVCHAN))
138e4e61333SAriff Abdullah 		return (EINVAL);
139a1d444e1SAriff Abdullah 
140e4e61333SAriff Abdullah 	if (newcnt < 0 || newcnt > SND_MAXVCHANS)
141e4e61333SAriff Abdullah 		return (E2BIG);
142a1d444e1SAriff Abdullah 
143bba4862cSAriff Abdullah 	if (direction == PCMDIR_PLAY)
144bba4862cSAriff Abdullah 		vcnt = d->pvchancount;
145bba4862cSAriff Abdullah 	else if (direction == PCMDIR_REC)
146bba4862cSAriff Abdullah 		vcnt = d->rvchancount;
147e4e61333SAriff Abdullah 	else
148e4e61333SAriff Abdullah 		return (EINVAL);
149a1d444e1SAriff Abdullah 
150a1d444e1SAriff Abdullah 	if (newcnt > vcnt) {
151bba4862cSAriff Abdullah 		KASSERT(num == -1 ||
152bba4862cSAriff Abdullah 		    (num >= 0 && num < SND_MAXVCHANS && (newcnt - 1) == vcnt),
153bba4862cSAriff Abdullah 		    ("bogus vchan_create() request num=%d newcnt=%d vcnt=%d",
154bba4862cSAriff Abdullah 		    num, newcnt, vcnt));
155a1d444e1SAriff Abdullah 		/* add new vchans - find a parent channel first */
156e4e61333SAriff Abdullah 		ch = NULL;
157bba4862cSAriff Abdullah 		CHN_FOREACH(c, d, channels.pcm) {
158a1d444e1SAriff Abdullah 			CHN_LOCK(c);
159bba4862cSAriff Abdullah 			if (c->direction == direction &&
160bba4862cSAriff Abdullah 			    ((c->flags & CHN_F_HAS_VCHAN) || (vcnt == 0 &&
16190da2b28SAriff Abdullah 			    c->refcount < 1 &&
162e4e61333SAriff Abdullah 			    !(c->flags & (CHN_F_BUSY | CHN_F_VIRTUAL))))) {
16390da2b28SAriff Abdullah 				/*
16490da2b28SAriff Abdullah 				 * Reuse hw channel with vchans already
16590da2b28SAriff Abdullah 				 * created.
16690da2b28SAriff Abdullah 				 */
16790da2b28SAriff Abdullah 				if (c->flags & CHN_F_HAS_VCHAN) {
168e4e61333SAriff Abdullah 					ch = c;
169e4e61333SAriff Abdullah 					break;
170e4e61333SAriff Abdullah 				}
17190da2b28SAriff Abdullah 				/*
17290da2b28SAriff Abdullah 				 * No vchans ever created, look for
17390da2b28SAriff Abdullah 				 * channels with supported formats.
17490da2b28SAriff Abdullah 				 */
17590da2b28SAriff Abdullah 				caps = chn_getcaps(c);
17690da2b28SAriff Abdullah 				if (caps == NULL) {
17790da2b28SAriff Abdullah 					CHN_UNLOCK(c);
17890da2b28SAriff Abdullah 					continue;
17990da2b28SAriff Abdullah 				}
18090da2b28SAriff Abdullah 				for (i = 0; caps->fmtlist[i] != 0; i++) {
18190da2b28SAriff Abdullah 					if (caps->fmtlist[i] & AFMT_CONVERTIBLE)
18290da2b28SAriff Abdullah 						break;
18390da2b28SAriff Abdullah 				}
18490da2b28SAriff Abdullah 				if (caps->fmtlist[i] != 0) {
18590da2b28SAriff Abdullah 					ch = c;
18690da2b28SAriff Abdullah 				    	break;
18790da2b28SAriff Abdullah 				}
18890da2b28SAriff Abdullah 			}
189a1d444e1SAriff Abdullah 			CHN_UNLOCK(c);
190a1d444e1SAriff Abdullah 		}
191e4e61333SAriff Abdullah 		if (ch == NULL)
192e4e61333SAriff Abdullah 			return (EBUSY);
193e4e61333SAriff Abdullah 		ch->flags |= CHN_F_BUSY;
194e4e61333SAriff Abdullah 		err = 0;
195a1d444e1SAriff Abdullah 		while (err == 0 && newcnt > vcnt) {
196e4e61333SAriff Abdullah 			err = vchan_create(ch, num);
197bba4862cSAriff Abdullah 			if (err == 0)
198a1d444e1SAriff Abdullah 				vcnt++;
199bba4862cSAriff Abdullah 			else if (err == E2BIG && newcnt > vcnt)
200bba4862cSAriff Abdullah 				device_printf(d->dev,
201bba4862cSAriff Abdullah 				    "%s: err=%d Maximum channel reached.\n",
202bba4862cSAriff Abdullah 				    __func__, err);
203a1d444e1SAriff Abdullah 		}
204a1d444e1SAriff Abdullah 		if (vcnt == 0)
205e4e61333SAriff Abdullah 			ch->flags &= ~CHN_F_BUSY;
206e4e61333SAriff Abdullah 		CHN_UNLOCK(ch);
207e4e61333SAriff Abdullah 		if (err != 0)
208e4e61333SAriff Abdullah 			return (err);
209a1d444e1SAriff Abdullah 	} else if (newcnt < vcnt) {
210bba4862cSAriff Abdullah 		KASSERT(num == -1,
211bba4862cSAriff Abdullah 		    ("bogus vchan_destroy() request num=%d", num));
212bba4862cSAriff Abdullah 		CHN_FOREACH(c, d, channels.pcm) {
213a1d444e1SAriff Abdullah 			CHN_LOCK(c);
214bba4862cSAriff Abdullah 			if (c->direction != direction ||
215bba4862cSAriff Abdullah 			    CHN_EMPTY(c, children) ||
216bba4862cSAriff Abdullah 			    !(c->flags & CHN_F_HAS_VCHAN)) {
217a1d444e1SAriff Abdullah 				CHN_UNLOCK(c);
218bba4862cSAriff Abdullah 				continue;
219a1d444e1SAriff Abdullah 			}
220bba4862cSAriff Abdullah 			CHN_FOREACH_SAFE(ch, c, nch, children) {
221bba4862cSAriff Abdullah 				CHN_LOCK(ch);
22290da2b28SAriff Abdullah 				if (vcnt == 1 && c->refcount > 0) {
223bba4862cSAriff Abdullah 					CHN_UNLOCK(ch);
22490da2b28SAriff Abdullah 					break;
22590da2b28SAriff Abdullah 				}
22690da2b28SAriff Abdullah 				if (!(ch->flags & CHN_F_BUSY) &&
22790da2b28SAriff Abdullah 				    ch->refcount < 1) {
228bba4862cSAriff Abdullah 					err = vchan_destroy(ch);
229a1d444e1SAriff Abdullah 					if (err == 0)
230a1d444e1SAriff Abdullah 						vcnt--;
231bba4862cSAriff Abdullah 				} else
232bba4862cSAriff Abdullah 					CHN_UNLOCK(ch);
233e4e61333SAriff Abdullah 				if (vcnt == newcnt)
234bba4862cSAriff Abdullah 					break;
235a1d444e1SAriff Abdullah 			}
236bba4862cSAriff Abdullah 			CHN_UNLOCK(c);
237bba4862cSAriff Abdullah 			break;
238bba4862cSAriff Abdullah 		}
239bba4862cSAriff Abdullah 	}
240a1d444e1SAriff Abdullah 
241e4e61333SAriff Abdullah 	return (0);
242a1d444e1SAriff Abdullah }
243a1d444e1SAriff Abdullah 
2443fdb3676SAriff Abdullah /* return error status and a locked channel */
2453fdb3676SAriff Abdullah int
2463fdb3676SAriff Abdullah pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction,
24725723d66SChristos Margiolis     pid_t pid, char *comm)
248285648f9SCameron Grant {
249285648f9SCameron Grant 	struct pcm_channel *c;
25090da2b28SAriff Abdullah 	int err, vchancount, vchan_num;
25125723d66SChristos Margiolis 	bool retry;
252bba4862cSAriff Abdullah 
25325723d66SChristos Margiolis 	KASSERT(d != NULL && ch != NULL &&
254bba4862cSAriff Abdullah 	    (direction == PCMDIR_PLAY || direction == PCMDIR_REC),
25525723d66SChristos Margiolis 	    ("%s(): invalid d=%p ch=%p direction=%d pid=%d",
25625723d66SChristos Margiolis 	    __func__, d, ch, direction, pid));
257e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
258bba4862cSAriff Abdullah 
25990da2b28SAriff Abdullah 	*ch = NULL;
26090da2b28SAriff Abdullah 	vchan_num = 0;
26190da2b28SAriff Abdullah 	vchancount = (direction == PCMDIR_PLAY) ? d->pvchancount :
26290da2b28SAriff Abdullah 	    d->rvchancount;
26390da2b28SAriff Abdullah 
26425723d66SChristos Margiolis 	retry = false;
2653fdb3676SAriff Abdullah retry_chnalloc:
26690da2b28SAriff Abdullah 	err = ENOTSUP;
267f637a36cSCameron Grant 	/* scan for a free channel */
268bba4862cSAriff Abdullah 	CHN_FOREACH(c, d, channels.pcm) {
26949c5e6e2SCameron Grant 		CHN_LOCK(c);
27025723d66SChristos Margiolis 		if (c->direction == direction && (c->flags & CHN_F_VIRTUAL)) {
27190da2b28SAriff Abdullah 			if (vchancount < snd_maxautovchans &&
27225723d66SChristos Margiolis 			    vchan_num < c->unit) {
27390da2b28SAriff Abdullah 			    	CHN_UNLOCK(c);
27490da2b28SAriff Abdullah 				goto vchan_alloc;
27590da2b28SAriff Abdullah 			}
27690da2b28SAriff Abdullah 			vchan_num++;
27790da2b28SAriff Abdullah 		}
27825723d66SChristos Margiolis 		if (c->direction == direction && !(c->flags & CHN_F_BUSY)) {
279285648f9SCameron Grant 			c->flags |= CHN_F_BUSY;
280b8f0d9e0SCameron Grant 			c->pid = pid;
28190da2b28SAriff Abdullah 			strlcpy(c->comm, (comm != NULL) ? comm :
28290da2b28SAriff Abdullah 			    CHN_COMM_UNKNOWN, sizeof(c->comm));
2833fdb3676SAriff Abdullah 			*ch = c;
284bba4862cSAriff Abdullah 			return (0);
28525723d66SChristos Margiolis 		} else if (c->direction == direction && (c->flags & CHN_F_BUSY))
286a1d444e1SAriff Abdullah 			err = EBUSY;
28749c5e6e2SCameron Grant 		CHN_UNLOCK(c);
288285648f9SCameron Grant 	}
289f637a36cSCameron Grant 
29025723d66SChristos Margiolis 	/*
29125723d66SChristos Margiolis 	 * We came from retry_chnalloc and still didn't find a free channel.
29225723d66SChristos Margiolis 	 */
29325723d66SChristos Margiolis 	if (retry)
294e4e61333SAriff Abdullah 		return (err);
295e4e61333SAriff Abdullah 
29690da2b28SAriff Abdullah vchan_alloc:
297f637a36cSCameron Grant 	/* no channel available */
29825723d66SChristos Margiolis 	if (!(vchancount > 0 && vchancount < snd_maxautovchans))
299bba4862cSAriff Abdullah 		return (err);
30025723d66SChristos Margiolis 	err = pcm_setvchans(d, direction, vchancount + 1, -1);
301a1d444e1SAriff Abdullah 	if (err == 0) {
30225723d66SChristos Margiolis 		retry = true;
3033fdb3676SAriff Abdullah 		goto retry_chnalloc;
304f637a36cSCameron Grant 	}
305f637a36cSCameron Grant 
306bba4862cSAriff Abdullah 	return (err);
307285648f9SCameron Grant }
308285648f9SCameron Grant 
309b8f0d9e0SCameron Grant /* release a locked channel and unlock it */
310285648f9SCameron Grant int
311b8f0d9e0SCameron Grant pcm_chnrelease(struct pcm_channel *c)
312285648f9SCameron Grant {
313e4e61333SAriff Abdullah 	PCM_BUSYASSERT(c->parentsnddev);
314b8f0d9e0SCameron Grant 	CHN_LOCKASSERT(c);
315e4e61333SAriff Abdullah 
316285648f9SCameron Grant 	c->flags &= ~CHN_F_BUSY;
317b8f0d9e0SCameron Grant 	c->pid = -1;
31890da2b28SAriff Abdullah 	strlcpy(c->comm, CHN_COMM_UNUSED, sizeof(c->comm));
31949c5e6e2SCameron Grant 	CHN_UNLOCK(c);
320e4e61333SAriff Abdullah 
321e4e61333SAriff Abdullah 	return (0);
322285648f9SCameron Grant }
323285648f9SCameron Grant 
324285648f9SCameron Grant int
325285648f9SCameron Grant pcm_chnref(struct pcm_channel *c, int ref)
326285648f9SCameron Grant {
327e4e61333SAriff Abdullah 	PCM_BUSYASSERT(c->parentsnddev);
328b8f0d9e0SCameron Grant 	CHN_LOCKASSERT(c);
329e4e61333SAriff Abdullah 
330285648f9SCameron Grant 	c->refcount += ref;
331e4e61333SAriff Abdullah 
332e4e61333SAriff Abdullah 	return (c->refcount);
333285648f9SCameron Grant }
334285648f9SCameron Grant 
33567b1dce3SCameron Grant static void
33667b1dce3SCameron Grant pcm_setmaxautovchans(struct snddev_info *d, int num)
33767b1dce3SCameron Grant {
338e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
339e4e61333SAriff Abdullah 
340bba4862cSAriff Abdullah 	if (num < 0)
341bba4862cSAriff Abdullah 		return;
342bba4862cSAriff Abdullah 
343bba4862cSAriff Abdullah 	if (num >= 0 && d->pvchancount > num)
344bba4862cSAriff Abdullah 		(void)pcm_setvchans(d, PCMDIR_PLAY, num, -1);
345bba4862cSAriff Abdullah 	else if (num > 0 && d->pvchancount == 0)
346bba4862cSAriff Abdullah 		(void)pcm_setvchans(d, PCMDIR_PLAY, 1, -1);
347bba4862cSAriff Abdullah 
348bba4862cSAriff Abdullah 	if (num >= 0 && d->rvchancount > num)
349bba4862cSAriff Abdullah 		(void)pcm_setvchans(d, PCMDIR_REC, num, -1);
350bba4862cSAriff Abdullah 	else if (num > 0 && d->rvchancount == 0)
351bba4862cSAriff Abdullah 		(void)pcm_setvchans(d, PCMDIR_REC, 1, -1);
35267b1dce3SCameron Grant }
35367b1dce3SCameron Grant 
35433dbf14aSCameron Grant static int
355851a904aSAlexander Leidinger sysctl_hw_snd_default_unit(SYSCTL_HANDLER_ARGS)
35633dbf14aSCameron Grant {
357b8f0d9e0SCameron Grant 	struct snddev_info *d;
35833dbf14aSCameron Grant 	int error, unit;
35933dbf14aSCameron Grant 
36033dbf14aSCameron Grant 	unit = snd_unit;
361041b706bSDavid Malone 	error = sysctl_handle_int(oidp, &unit, 0, req);
36233dbf14aSCameron Grant 	if (error == 0 && req->newptr != NULL) {
363b8f0d9e0SCameron Grant 		d = devclass_get_softc(pcm_devclass, unit);
364e4e61333SAriff Abdullah 		if (!PCM_REGISTERED(d) || CHN_EMPTY(d, channels.pcm))
365b8f0d9e0SCameron Grant 			return EINVAL;
36633dbf14aSCameron Grant 		snd_unit = unit;
367cbebc90dSAlexander Motin 		snd_unit_auto = 0;
36833dbf14aSCameron Grant 	}
36933dbf14aSCameron Grant 	return (error);
37033dbf14aSCameron Grant }
371851a904aSAlexander Leidinger /* XXX: do we need a way to let the user change the default unit? */
372b7d6c6b5SEitan Adler SYSCTL_PROC(_hw_snd, OID_AUTO, default_unit,
3737029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT, 0,
3747029da5cSPawel Biernacki     sizeof(int), sysctl_hw_snd_default_unit, "I",
375b7d6c6b5SEitan Adler     "default sound device");
376987e5972SCameron Grant 
377cd9766c5SCameron Grant static int
37867b1dce3SCameron Grant sysctl_hw_snd_maxautovchans(SYSCTL_HANDLER_ARGS)
379cd9766c5SCameron Grant {
38067b1dce3SCameron Grant 	struct snddev_info *d;
38167b1dce3SCameron Grant 	int i, v, error;
382cd9766c5SCameron Grant 
38367b1dce3SCameron Grant 	v = snd_maxautovchans;
384041b706bSDavid Malone 	error = sysctl_handle_int(oidp, &v, 0, req);
385cd9766c5SCameron Grant 	if (error == 0 && req->newptr != NULL) {
386bba4862cSAriff Abdullah 		if (v < 0)
387bba4862cSAriff Abdullah 			v = 0;
388bba4862cSAriff Abdullah 		if (v > SND_MAXVCHANS)
389bba4862cSAriff Abdullah 			v = SND_MAXVCHANS;
390bba4862cSAriff Abdullah 		snd_maxautovchans = v;
3919c271f79SAriff Abdullah 		for (i = 0; pcm_devclass != NULL &&
3929c271f79SAriff Abdullah 		    i < devclass_get_maxunit(pcm_devclass); i++) {
39367b1dce3SCameron Grant 			d = devclass_get_softc(pcm_devclass, i);
394e4e61333SAriff Abdullah 			if (!PCM_REGISTERED(d))
39567b1dce3SCameron Grant 				continue;
396e4e61333SAriff Abdullah 			PCM_ACQUIRE_QUICK(d);
39767b1dce3SCameron Grant 			pcm_setmaxautovchans(d, v);
398e4e61333SAriff Abdullah 			PCM_RELEASE_QUICK(d);
39967b1dce3SCameron Grant 		}
40067b1dce3SCameron Grant 	}
401cd9766c5SCameron Grant 	return (error);
402cd9766c5SCameron Grant }
4037029da5cSPawel Biernacki SYSCTL_PROC(_hw_snd, OID_AUTO, maxautovchans,
4047029da5cSPawel Biernacki     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
4057029da5cSPawel Biernacki     sysctl_hw_snd_maxautovchans, "I",
4067029da5cSPawel Biernacki     "maximum virtual channel");
407f637a36cSCameron Grant 
408285648f9SCameron Grant struct pcm_channel *
409bba4862cSAriff Abdullah pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo)
410987e5972SCameron Grant {
411bba4862cSAriff Abdullah 	struct pcm_channel *ch;
412bba4862cSAriff Abdullah 	int direction, err, rpnum, *pnum, max;
41325723d66SChristos Margiolis 	int type, unit;
414bba4862cSAriff Abdullah 	char *dirs, *devname, buf[CHN_NAMELEN];
415bba4862cSAriff Abdullah 
416e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
41790da2b28SAriff Abdullah 	PCM_LOCKASSERT(d);
418bba4862cSAriff Abdullah 	KASSERT(num >= -1, ("invalid num=%d", num));
419bba4862cSAriff Abdullah 
420285648f9SCameron Grant 	switch (dir) {
421285648f9SCameron Grant 	case PCMDIR_PLAY:
422285648f9SCameron Grant 		dirs = "play";
423a3193a9cSDon Lewis 		direction = PCMDIR_PLAY;
424a67fe5c1SCameron Grant 		pnum = &d->playcount;
42525723d66SChristos Margiolis 		type = SND_DEV_DSPHW_PLAY;
426bba4862cSAriff Abdullah 		max = SND_MAXHWCHAN;
427285648f9SCameron Grant 		break;
428bba4862cSAriff Abdullah 	case PCMDIR_PLAY_VIRTUAL:
429d0032e6aSChristos Margiolis 		dirs = "virtual_play";
430bba4862cSAriff Abdullah 		direction = PCMDIR_PLAY;
431bba4862cSAriff Abdullah 		pnum = &d->pvchancount;
43225723d66SChristos Margiolis 		type = SND_DEV_DSPHW_VPLAY;
433bba4862cSAriff Abdullah 		max = SND_MAXVCHANS;
434bba4862cSAriff Abdullah 		break;
435285648f9SCameron Grant 	case PCMDIR_REC:
436285648f9SCameron Grant 		dirs = "record";
437a3193a9cSDon Lewis 		direction = PCMDIR_REC;
438a67fe5c1SCameron Grant 		pnum = &d->reccount;
43925723d66SChristos Margiolis 		type = SND_DEV_DSPHW_REC;
440bba4862cSAriff Abdullah 		max = SND_MAXHWCHAN;
441285648f9SCameron Grant 		break;
442bba4862cSAriff Abdullah 	case PCMDIR_REC_VIRTUAL:
443d0032e6aSChristos Margiolis 		dirs = "virtual_record";
444bba4862cSAriff Abdullah 		direction = PCMDIR_REC;
445bba4862cSAriff Abdullah 		pnum = &d->rvchancount;
44625723d66SChristos Margiolis 		type = SND_DEV_DSPHW_VREC;
447bba4862cSAriff Abdullah 		max = SND_MAXVCHANS;
448285648f9SCameron Grant 		break;
449285648f9SCameron Grant 	default:
450e4e61333SAriff Abdullah 		return (NULL);
4519c326820SCameron Grant 	}
452285648f9SCameron Grant 
45325723d66SChristos Margiolis 	unit = (num == -1) ? 0 : num;
454285648f9SCameron Grant 
45525723d66SChristos Margiolis 	if (*pnum >= max || unit >= max)
456e4e61333SAriff Abdullah 		return (NULL);
457bba4862cSAriff Abdullah 
4583fdb3676SAriff Abdullah 	rpnum = 0;
459bba4862cSAriff Abdullah 
460bba4862cSAriff Abdullah 	CHN_FOREACH(ch, d, channels.pcm) {
46125723d66SChristos Margiolis 		if (ch->type != type)
4623fdb3676SAriff Abdullah 			continue;
46325723d66SChristos Margiolis 		if (unit == ch->unit && num != -1) {
4643fdb3676SAriff Abdullah 			device_printf(d->dev,
46525723d66SChristos Margiolis 			    "channel num=%d allocated!\n", unit);
466e4e61333SAriff Abdullah 			return (NULL);
467bba4862cSAriff Abdullah 		}
46825723d66SChristos Margiolis 		unit++;
46925723d66SChristos Margiolis 		if (unit >= max) {
470bba4862cSAriff Abdullah 			device_printf(d->dev,
47125723d66SChristos Margiolis 			    "chan=%d > %d\n", unit, max);
472e4e61333SAriff Abdullah 			return (NULL);
473bba4862cSAriff Abdullah 		}
4743fdb3676SAriff Abdullah 		rpnum++;
4753fdb3676SAriff Abdullah 	}
476bba4862cSAriff Abdullah 
4773fdb3676SAriff Abdullah 	if (*pnum != rpnum) {
4783fdb3676SAriff Abdullah 		device_printf(d->dev,
479bba4862cSAriff Abdullah 		    "%s(): WARNING: pnum screwed : dirs=%s pnum=%d rpnum=%d\n",
4803fdb3676SAriff Abdullah 		    __func__, dirs, *pnum, rpnum);
481e4e61333SAriff Abdullah 		return (NULL);
4823fdb3676SAriff Abdullah 	}
483a67fe5c1SCameron Grant 
48490da2b28SAriff Abdullah 	PCM_UNLOCK(d);
485bba4862cSAriff Abdullah 	ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
486bba4862cSAriff Abdullah 	ch->methods = kobj_create(cls, M_DEVBUF, M_WAITOK | M_ZERO);
48725723d66SChristos Margiolis 	ch->type = type;
48825723d66SChristos Margiolis 	ch->unit = unit;
489285648f9SCameron Grant 	ch->pid = -1;
49090da2b28SAriff Abdullah 	strlcpy(ch->comm, CHN_COMM_UNUSED, sizeof(ch->comm));
491285648f9SCameron Grant 	ch->parentsnddev = d;
492285648f9SCameron Grant 	ch->parentchannel = parent;
493436c9b65SScott Long 	ch->dev = d->dev;
494bba4862cSAriff Abdullah 	ch->trigger = PCMTRIG_STOP;
49525723d66SChristos Margiolis 	devname = dsp_unit2name(buf, sizeof(buf), ch);
49625723d66SChristos Margiolis 	if (devname == NULL) {
49725723d66SChristos Margiolis 		device_printf(d->dev, "Failed to query device name");
49825723d66SChristos Margiolis 		kobj_delete(ch->methods, M_DEVBUF);
49925723d66SChristos Margiolis 		free(ch, M_DEVBUF);
50025723d66SChristos Margiolis 		return (NULL);
50125723d66SChristos Margiolis 	}
502bba4862cSAriff Abdullah 	snprintf(ch->name, sizeof(ch->name), "%s:%s:%s",
503bba4862cSAriff Abdullah 	    device_get_nameunit(ch->dev), dirs, devname);
504285648f9SCameron Grant 
505a3193a9cSDon Lewis 	err = chn_init(ch, devinfo, dir, direction);
50690da2b28SAriff Abdullah 	PCM_LOCK(d);
5070f55ac6cSCameron Grant 	if (err) {
508bba4862cSAriff Abdullah 		device_printf(d->dev, "chn_init(%s) failed: err = %d\n",
509bba4862cSAriff Abdullah 		    ch->name, err);
510285648f9SCameron Grant 		kobj_delete(ch->methods, M_DEVBUF);
511285648f9SCameron Grant 		free(ch, M_DEVBUF);
512e4e61333SAriff Abdullah 		return (NULL);
513bbb5bf3dSCameron Grant 	}
514285648f9SCameron Grant 
515e4e61333SAriff Abdullah 	return (ch);
516285648f9SCameron Grant }
517285648f9SCameron Grant 
518285648f9SCameron Grant int
5195ee30e27SMathew Kanner pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch)
520285648f9SCameron Grant {
521e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
52290da2b28SAriff Abdullah 	PCM_LOCKASSERT(d);
523bba4862cSAriff Abdullah 	KASSERT(ch != NULL && (ch->direction == PCMDIR_PLAY ||
524bba4862cSAriff Abdullah 	    ch->direction == PCMDIR_REC), ("Invalid pcm channel"));
525285648f9SCameron Grant 
52690da2b28SAriff Abdullah 	CHN_INSERT_SORT_ASCEND(d, ch, channels.pcm);
5273fdb3676SAriff Abdullah 
52825723d66SChristos Margiolis 	switch (ch->type) {
529e4e61333SAriff Abdullah 	case SND_DEV_DSPHW_PLAY:
530e4e61333SAriff Abdullah 		d->playcount++;
531e4e61333SAriff Abdullah 		break;
532e4e61333SAriff Abdullah 	case SND_DEV_DSPHW_VPLAY:
533e4e61333SAriff Abdullah 		d->pvchancount++;
534e4e61333SAriff Abdullah 		break;
535e4e61333SAriff Abdullah 	case SND_DEV_DSPHW_REC:
536e4e61333SAriff Abdullah 		d->reccount++;
537e4e61333SAriff Abdullah 		break;
538e4e61333SAriff Abdullah 	case SND_DEV_DSPHW_VREC:
539e4e61333SAriff Abdullah 		d->rvchancount++;
540e4e61333SAriff Abdullah 		break;
541e4e61333SAriff Abdullah 	default:
542e4e61333SAriff Abdullah 		break;
543e4e61333SAriff Abdullah 	}
544b8f0d9e0SCameron Grant 
545e4e61333SAriff Abdullah 	return (0);
54633dbf14aSCameron Grant }
54733dbf14aSCameron Grant 
548285648f9SCameron Grant int
5495ee30e27SMathew Kanner pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch)
55033dbf14aSCameron Grant {
551bba4862cSAriff Abdullah 	struct pcm_channel *tmp;
55233dbf14aSCameron Grant 
553e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
55490da2b28SAriff Abdullah 	PCM_LOCKASSERT(d);
555e4e61333SAriff Abdullah 
556bba4862cSAriff Abdullah 	tmp = NULL;
557a527dbc7SCameron Grant 
558bba4862cSAriff Abdullah 	CHN_FOREACH(tmp, d, channels.pcm) {
559bba4862cSAriff Abdullah 		if (tmp == ch)
560bba4862cSAriff Abdullah 			break;
56133dbf14aSCameron Grant 	}
562bba4862cSAriff Abdullah 
563bba4862cSAriff Abdullah 	if (tmp != ch)
564e4e61333SAriff Abdullah 		return (EINVAL);
56567beb5a5SCameron Grant 
566bba4862cSAriff Abdullah 	CHN_REMOVE(d, ch, channels.pcm);
567e4e61333SAriff Abdullah 
56825723d66SChristos Margiolis 	switch (ch->type) {
569bba4862cSAriff Abdullah 	case SND_DEV_DSPHW_PLAY:
57067beb5a5SCameron Grant 		d->playcount--;
571bba4862cSAriff Abdullah 		break;
572bba4862cSAriff Abdullah 	case SND_DEV_DSPHW_VPLAY:
573bba4862cSAriff Abdullah 		d->pvchancount--;
574bba4862cSAriff Abdullah 		break;
575bba4862cSAriff Abdullah 	case SND_DEV_DSPHW_REC:
576bba4862cSAriff Abdullah 		d->reccount--;
577bba4862cSAriff Abdullah 		break;
578bba4862cSAriff Abdullah 	case SND_DEV_DSPHW_VREC:
579bba4862cSAriff Abdullah 		d->rvchancount--;
580bba4862cSAriff Abdullah 		break;
581bba4862cSAriff Abdullah 	default:
582bba4862cSAriff Abdullah 		break;
583bba4862cSAriff Abdullah 	}
584285648f9SCameron Grant 
585e4e61333SAriff Abdullah 	return (0);
586987e5972SCameron Grant }
587987e5972SCameron Grant 
588987e5972SCameron Grant int
589285648f9SCameron Grant pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
590285648f9SCameron Grant {
591285648f9SCameron Grant 	struct snddev_info *d = device_get_softc(dev);
59267b1dce3SCameron Grant 	struct pcm_channel *ch;
59367b1dce3SCameron Grant 	int err;
594285648f9SCameron Grant 
595e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
596e4e61333SAriff Abdullah 
59790da2b28SAriff Abdullah 	PCM_LOCK(d);
598bba4862cSAriff Abdullah 	ch = pcm_chn_create(d, NULL, cls, dir, -1, devinfo);
599285648f9SCameron Grant 	if (!ch) {
600e4e61333SAriff Abdullah 		device_printf(d->dev, "pcm_chn_create(%s, %d, %p) failed\n",
601e4e61333SAriff Abdullah 		    cls->name, dir, devinfo);
60290da2b28SAriff Abdullah 		PCM_UNLOCK(d);
603e4e61333SAriff Abdullah 		return (ENODEV);
604285648f9SCameron Grant 	}
605cd9766c5SCameron Grant 
6065ee30e27SMathew Kanner 	err = pcm_chn_add(d, ch);
60790da2b28SAriff Abdullah 	PCM_UNLOCK(d);
608285648f9SCameron Grant 	if (err) {
609e4e61333SAriff Abdullah 		device_printf(d->dev, "pcm_chn_add(%s) failed, err=%d\n",
610e4e61333SAriff Abdullah 		    ch->name, err);
611*b3ea087cSChristos Margiolis 		chn_kill(ch);
612cd9766c5SCameron Grant 	}
613cd9766c5SCameron Grant 
614e4e61333SAriff Abdullah 	return (err);
615285648f9SCameron Grant }
616285648f9SCameron Grant 
61703614fcbSChristos Margiolis static void
61803614fcbSChristos Margiolis pcm_killchans(struct snddev_info *d)
619285648f9SCameron Grant {
620e33bee07SOlivier Houchard 	struct pcm_channel *ch;
621e4e61333SAriff Abdullah 	int error;
62203614fcbSChristos Margiolis 	bool found;
623e4e61333SAriff Abdullah 
624e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
62503614fcbSChristos Margiolis 	do {
62603614fcbSChristos Margiolis 		found = false;
62703614fcbSChristos Margiolis 		CHN_FOREACH(ch, d, channels.pcm) {
62803614fcbSChristos Margiolis 			CHN_LOCK(ch);
62903614fcbSChristos Margiolis 			/*
63003614fcbSChristos Margiolis 			 * Make sure no channel has went to sleep in the
63103614fcbSChristos Margiolis 			 * meantime.
63203614fcbSChristos Margiolis 			 */
63303614fcbSChristos Margiolis 			chn_shutdown(ch);
63403614fcbSChristos Margiolis 			/*
63503614fcbSChristos Margiolis 			 * We have to give a thread sleeping in chn_sleep() a
63603614fcbSChristos Margiolis 			 * chance to observe that the channel is dead.
63703614fcbSChristos Margiolis 			 */
63803614fcbSChristos Margiolis 			if ((ch->flags & CHN_F_SLEEPING) == 0) {
63903614fcbSChristos Margiolis 				found = true;
64003614fcbSChristos Margiolis 				CHN_UNLOCK(ch);
64103614fcbSChristos Margiolis 				break;
64203614fcbSChristos Margiolis 			}
64303614fcbSChristos Margiolis 			CHN_UNLOCK(ch);
64403614fcbSChristos Margiolis 		}
645285648f9SCameron Grant 
64603614fcbSChristos Margiolis 		/*
64703614fcbSChristos Margiolis 		 * All channels are still sleeping. Sleep for a bit and try
64803614fcbSChristos Margiolis 		 * again to see if any of them is awake now.
64903614fcbSChristos Margiolis 		 */
65003614fcbSChristos Margiolis 		if (!found) {
65103614fcbSChristos Margiolis 			pause_sbt("pcmkillchans", SBT_1MS * 5, 0, 0);
65203614fcbSChristos Margiolis 			continue;
65303614fcbSChristos Margiolis 		}
654285648f9SCameron Grant 
65590da2b28SAriff Abdullah 		PCM_LOCK(d);
656bba4862cSAriff Abdullah 		error = pcm_chn_remove(d, ch);
65790da2b28SAriff Abdullah 		PCM_UNLOCK(d);
65803614fcbSChristos Margiolis 		if (error == 0)
659*b3ea087cSChristos Margiolis 			chn_kill(ch);
66003614fcbSChristos Margiolis 	} while (!CHN_EMPTY(d, channels.pcm));
661285648f9SCameron Grant }
662285648f9SCameron Grant 
663cbebc90dSAlexander Motin static int
664cbebc90dSAlexander Motin pcm_best_unit(int old)
665cbebc90dSAlexander Motin {
666cbebc90dSAlexander Motin 	struct snddev_info *d;
667cbebc90dSAlexander Motin 	int i, best, bestprio, prio;
668cbebc90dSAlexander Motin 
669cbebc90dSAlexander Motin 	best = -1;
670cbebc90dSAlexander Motin 	bestprio = -100;
671cbebc90dSAlexander Motin 	for (i = 0; pcm_devclass != NULL &&
672cbebc90dSAlexander Motin 	    i < devclass_get_maxunit(pcm_devclass); i++) {
673cbebc90dSAlexander Motin 		d = devclass_get_softc(pcm_devclass, i);
674cbebc90dSAlexander Motin 		if (!PCM_REGISTERED(d))
675cbebc90dSAlexander Motin 			continue;
676cbebc90dSAlexander Motin 		prio = 0;
677cbebc90dSAlexander Motin 		if (d->playcount == 0)
678cbebc90dSAlexander Motin 			prio -= 10;
679cbebc90dSAlexander Motin 		if (d->reccount == 0)
680cbebc90dSAlexander Motin 			prio -= 2;
681cbebc90dSAlexander Motin 		if (prio > bestprio || (prio == bestprio && i == old)) {
682cbebc90dSAlexander Motin 			best = i;
683cbebc90dSAlexander Motin 			bestprio = prio;
684cbebc90dSAlexander Motin 		}
685cbebc90dSAlexander Motin 	}
686cbebc90dSAlexander Motin 	return (best);
687cbebc90dSAlexander Motin }
688cbebc90dSAlexander Motin 
689285648f9SCameron Grant int
690987e5972SCameron Grant pcm_setstatus(device_t dev, char *str)
691987e5972SCameron Grant {
69266ef8af5SCameron Grant 	struct snddev_info *d = device_get_softc(dev);
69349c5e6e2SCameron Grant 
69432a0e5d5SHans Petter Selasky 	/* should only be called once */
69532a0e5d5SHans Petter Selasky 	if (d->flags & SD_F_REGISTERED)
69632a0e5d5SHans Petter Selasky 		return (EINVAL);
69732a0e5d5SHans Petter Selasky 
698e4e61333SAriff Abdullah 	PCM_BUSYASSERT(d);
699bba4862cSAriff Abdullah 
700e4e61333SAriff Abdullah 	if (d->playcount == 0 || d->reccount == 0)
701e4e61333SAriff Abdullah 		d->flags |= SD_F_SIMPLEX;
702e4e61333SAriff Abdullah 
70332a0e5d5SHans Petter Selasky 	if (d->playcount > 0 || d->reccount > 0)
704e4e61333SAriff Abdullah 		d->flags |= SD_F_AUTOVCHAN;
705e4e61333SAriff Abdullah 
706e4e61333SAriff Abdullah 	pcm_setmaxautovchans(d, snd_maxautovchans);
707bba4862cSAriff Abdullah 
708a580b31aSAriff Abdullah 	strlcpy(d->status, str, SND_STATUSLEN);
709bba4862cSAriff Abdullah 
71090da2b28SAriff Abdullah 	PCM_LOCK(d);
711e4e61333SAriff Abdullah 
712e4e61333SAriff Abdullah 	/* Done, we're ready.. */
713e4e61333SAriff Abdullah 	d->flags |= SD_F_REGISTERED;
714e4e61333SAriff Abdullah 
715e4e61333SAriff Abdullah 	PCM_RELEASE(d);
716bba4862cSAriff Abdullah 
71790da2b28SAriff Abdullah 	PCM_UNLOCK(d);
718bba4862cSAriff Abdullah 
71932a0e5d5SHans Petter Selasky 	/*
72032a0e5d5SHans Petter Selasky 	 * Create all sysctls once SD_F_REGISTERED is set else
72132a0e5d5SHans Petter Selasky 	 * tunable sysctls won't work:
72232a0e5d5SHans Petter Selasky 	 */
72332a0e5d5SHans Petter Selasky 	pcm_sysinit(dev);
72432a0e5d5SHans Petter Selasky 
725cbebc90dSAlexander Motin 	if (snd_unit_auto < 0)
726cbebc90dSAlexander Motin 		snd_unit_auto = (snd_unit < 0) ? 1 : 0;
727cbebc90dSAlexander Motin 	if (snd_unit < 0 || snd_unit_auto > 1)
728f3685841SAriff Abdullah 		snd_unit = device_get_unit(dev);
729cbebc90dSAlexander Motin 	else if (snd_unit_auto == 1)
730cbebc90dSAlexander Motin 		snd_unit = pcm_best_unit(snd_unit);
731f3685841SAriff Abdullah 
732e4e61333SAriff Abdullah 	return (0);
733987e5972SCameron Grant }
734987e5972SCameron Grant 
735a1d444e1SAriff Abdullah uint32_t
736987e5972SCameron Grant pcm_getflags(device_t dev)
737987e5972SCameron Grant {
73866ef8af5SCameron Grant 	struct snddev_info *d = device_get_softc(dev);
73949c5e6e2SCameron Grant 
740987e5972SCameron Grant 	return d->flags;
741987e5972SCameron Grant }
742987e5972SCameron Grant 
743987e5972SCameron Grant void
744a1d444e1SAriff Abdullah pcm_setflags(device_t dev, uint32_t val)
745987e5972SCameron Grant {
74666ef8af5SCameron Grant 	struct snddev_info *d = device_get_softc(dev);
747d95502a8SCameron Grant 
748987e5972SCameron Grant 	d->flags = val;
749987e5972SCameron Grant }
750987e5972SCameron Grant 
75139004e69SCameron Grant void *
75239004e69SCameron Grant pcm_getdevinfo(device_t dev)
75339004e69SCameron Grant {
75466ef8af5SCameron Grant 	struct snddev_info *d = device_get_softc(dev);
75549c5e6e2SCameron Grant 
75639004e69SCameron Grant 	return d->devinfo;
75739004e69SCameron Grant }
75839004e69SCameron Grant 
759a67fe5c1SCameron Grant unsigned int
760d55d96f6SAlexander Leidinger pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz)
761a67fe5c1SCameron Grant {
762a67fe5c1SCameron Grant 	struct snddev_info *d = device_get_softc(dev);
7634e60be34SCameron Grant 	int sz, x;
764a67fe5c1SCameron Grant 
765a67fe5c1SCameron Grant 	sz = 0;
7664e60be34SCameron Grant 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "buffersize", &sz) == 0) {
7674e60be34SCameron Grant 		x = sz;
768d55d96f6SAlexander Leidinger 		RANGE(sz, minbufsz, maxbufsz);
7694e60be34SCameron Grant 		if (x != sz)
770d55d96f6SAlexander Leidinger 			device_printf(dev, "'buffersize=%d' hint is out of range (%d-%d), using %d\n", x, minbufsz, maxbufsz, sz);
771d55d96f6SAlexander Leidinger 		x = minbufsz;
7724e60be34SCameron Grant 		while (x < sz)
7734e60be34SCameron Grant 			x <<= 1;
7744e60be34SCameron Grant 		if (x > sz)
7754e60be34SCameron Grant 			x >>= 1;
7764e60be34SCameron Grant 		if (x != sz) {
7774e60be34SCameron Grant 			device_printf(dev, "'buffersize=%d' hint is not a power of 2, using %d\n", sz, x);
7784e60be34SCameron Grant 			sz = x;
7794e60be34SCameron Grant 		}
7804e60be34SCameron Grant 	} else {
781a67fe5c1SCameron Grant 		sz = deflt;
7824e60be34SCameron Grant 	}
7834e60be34SCameron Grant 
784a67fe5c1SCameron Grant 	d->bufsz = sz;
785a67fe5c1SCameron Grant 
786a67fe5c1SCameron Grant 	return sz;
787a67fe5c1SCameron Grant }
788a67fe5c1SCameron Grant 
78990da2b28SAriff Abdullah static int
79090da2b28SAriff Abdullah sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)
79190da2b28SAriff Abdullah {
79290da2b28SAriff Abdullah 	struct snddev_info *d;
79390da2b28SAriff Abdullah 	int err, val;
79490da2b28SAriff Abdullah 
79590da2b28SAriff Abdullah 	d = oidp->oid_arg1;
79690da2b28SAriff Abdullah 	if (!PCM_REGISTERED(d))
79790da2b28SAriff Abdullah 		return (ENODEV);
79890da2b28SAriff Abdullah 
79990da2b28SAriff Abdullah 	PCM_LOCK(d);
80090da2b28SAriff Abdullah 	PCM_WAIT(d);
80190da2b28SAriff Abdullah 	val = (d->flags & SD_F_BITPERFECT) ? 1 : 0;
80290da2b28SAriff Abdullah 	PCM_ACQUIRE(d);
80390da2b28SAriff Abdullah 	PCM_UNLOCK(d);
80490da2b28SAriff Abdullah 
80590da2b28SAriff Abdullah 	err = sysctl_handle_int(oidp, &val, 0, req);
80690da2b28SAriff Abdullah 
80790da2b28SAriff Abdullah 	if (err == 0 && req->newptr != NULL) {
80890da2b28SAriff Abdullah 		if (!(val == 0 || val == 1)) {
80990da2b28SAriff Abdullah 			PCM_RELEASE_QUICK(d);
81090da2b28SAriff Abdullah 			return (EINVAL);
81190da2b28SAriff Abdullah 		}
81290da2b28SAriff Abdullah 
81390da2b28SAriff Abdullah 		PCM_LOCK(d);
81490da2b28SAriff Abdullah 
81590da2b28SAriff Abdullah 		d->flags &= ~SD_F_BITPERFECT;
81690da2b28SAriff Abdullah 		d->flags |= (val != 0) ? SD_F_BITPERFECT : 0;
81790da2b28SAriff Abdullah 
81890da2b28SAriff Abdullah 		PCM_RELEASE(d);
81990da2b28SAriff Abdullah 		PCM_UNLOCK(d);
82090da2b28SAriff Abdullah 	} else
82190da2b28SAriff Abdullah 		PCM_RELEASE_QUICK(d);
82290da2b28SAriff Abdullah 
82390da2b28SAriff Abdullah 	return (err);
82490da2b28SAriff Abdullah }
82590da2b28SAriff Abdullah 
826ed2196e5SHans Petter Selasky static u_int8_t
827ed2196e5SHans Petter Selasky pcm_mode_init(struct snddev_info *d)
828ed2196e5SHans Petter Selasky {
829ed2196e5SHans Petter Selasky 	u_int8_t mode = 0;
830ed2196e5SHans Petter Selasky 
831ed2196e5SHans Petter Selasky 	if (d->playcount > 0)
832ed2196e5SHans Petter Selasky 		mode |= PCM_MODE_PLAY;
833ed2196e5SHans Petter Selasky 	if (d->reccount > 0)
834ed2196e5SHans Petter Selasky 		mode |= PCM_MODE_REC;
835ed2196e5SHans Petter Selasky 	if (d->mixer_dev != NULL)
836ed2196e5SHans Petter Selasky 		mode |= PCM_MODE_MIXER;
837ed2196e5SHans Petter Selasky 
838ed2196e5SHans Petter Selasky 	return (mode);
839ed2196e5SHans Petter Selasky }
840ed2196e5SHans Petter Selasky 
84132a0e5d5SHans Petter Selasky static void
84232a0e5d5SHans Petter Selasky pcm_sysinit(device_t dev)
84332a0e5d5SHans Petter Selasky {
84432a0e5d5SHans Petter Selasky   	struct snddev_info *d = device_get_softc(dev);
845ed2196e5SHans Petter Selasky 	u_int8_t mode;
846ed2196e5SHans Petter Selasky 
847ed2196e5SHans Petter Selasky 	mode = pcm_mode_init(d);
84832a0e5d5SHans Petter Selasky 
849132fca63SHans Petter Selasky 	/* XXX: a user should be able to set this with a control tool, the
85032a0e5d5SHans Petter Selasky 	   sysadmin then needs min+max sysctls for this */
85132a0e5d5SHans Petter Selasky 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
85232a0e5d5SHans Petter Selasky 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
85332a0e5d5SHans Petter Selasky             OID_AUTO, "buffersize", CTLFLAG_RD, &d->bufsz, 0, "allocated buffer size");
85432a0e5d5SHans Petter Selasky 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
85532a0e5d5SHans Petter Selasky 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
8563b4c5433SAlexander Motin 	    "bitperfect", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, d,
8577029da5cSPawel Biernacki 	    sizeof(d), sysctl_dev_pcm_bitperfect, "I",
85832a0e5d5SHans Petter Selasky 	    "bit-perfect playback/recording (0=disable, 1=enable)");
859ed2196e5SHans Petter Selasky 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
860ed2196e5SHans Petter Selasky 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
861ed2196e5SHans Petter Selasky 	    OID_AUTO, "mode", CTLFLAG_RD, NULL, mode,
862ed2196e5SHans Petter Selasky 	    "mode (1=mixer, 2=play, 4=rec. The values are OR'ed if more than one"
863ed2196e5SHans Petter Selasky 	    "mode is supported)");
86432a0e5d5SHans Petter Selasky 	if (d->flags & SD_F_AUTOVCHAN)
86532a0e5d5SHans Petter Selasky 		vchan_initsys(dev);
86632a0e5d5SHans Petter Selasky 	if (d->flags & SD_F_EQ)
86732a0e5d5SHans Petter Selasky 		feeder_eq_initsys(dev);
86832a0e5d5SHans Petter Selasky }
86932a0e5d5SHans Petter Selasky 
870987e5972SCameron Grant int
871987e5972SCameron Grant pcm_register(device_t dev, void *devinfo, int numplay, int numrec)
872987e5972SCameron Grant {
873bba4862cSAriff Abdullah 	struct snddev_info *d;
87490da2b28SAriff Abdullah 	int i;
875987e5972SCameron Grant 
876b8a36395SCameron Grant 	if (pcm_veto_load) {
877b8a36395SCameron Grant 		device_printf(dev, "disabled due to an error while initialising: %d\n", pcm_veto_load);
878b8a36395SCameron Grant 
879b8a36395SCameron Grant 		return EINVAL;
880b8a36395SCameron Grant 	}
881b8a36395SCameron Grant 
882bba4862cSAriff Abdullah 	d = device_get_softc(dev);
883e4e61333SAriff Abdullah 	d->dev = dev;
8842c69ba87SJohn Baldwin 	d->lock = snd_mtxcreate(device_get_nameunit(dev), "sound cdev");
885e4e61333SAriff Abdullah 	cv_init(&d->cv, device_get_nameunit(dev));
886e4e61333SAriff Abdullah 	PCM_ACQUIRE_QUICK(d);
8877233ababSAlexander Leidinger #if 0
8887233ababSAlexander Leidinger 	/*
8897233ababSAlexander Leidinger 	 * d->flags should be cleared by the allocator of the softc.
8907233ababSAlexander Leidinger 	 * We cannot clear this field here because several devices set
8917233ababSAlexander Leidinger 	 * this flag before calling pcm_register().
8927233ababSAlexander Leidinger 	 */
893cd9766c5SCameron Grant 	d->flags = 0;
8947233ababSAlexander Leidinger #endif
89590da2b28SAriff Abdullah 	i = 0;
89690da2b28SAriff Abdullah 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
89790da2b28SAriff Abdullah 	    "vpc", &i) != 0 || i != 0)
89890da2b28SAriff Abdullah 		d->flags |= SD_F_VPC;
89990da2b28SAriff Abdullah 
90090da2b28SAriff Abdullah 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
90190da2b28SAriff Abdullah 	    "bitperfect", &i) == 0 && i != 0)
90290da2b28SAriff Abdullah 		d->flags |= SD_F_BITPERFECT;
90390da2b28SAriff Abdullah 
904987e5972SCameron Grant 	d->devinfo = devinfo;
905506a5308SCameron Grant 	d->reccount = 0;
906a67fe5c1SCameron Grant 	d->playcount = 0;
907bba4862cSAriff Abdullah 	d->pvchancount = 0;
908bba4862cSAriff Abdullah 	d->rvchancount = 0;
909bba4862cSAriff Abdullah 	d->pvchanrate = 0;
910bba4862cSAriff Abdullah 	d->pvchanformat = 0;
911bba4862cSAriff Abdullah 	d->rvchanrate = 0;
912bba4862cSAriff Abdullah 	d->rvchanformat = 0;
913833f7023SCameron Grant 
914bba4862cSAriff Abdullah 	CHN_INIT(d, channels.pcm);
915bba4862cSAriff Abdullah 	CHN_INIT(d, channels.pcm.busy);
91690da2b28SAriff Abdullah 	CHN_INIT(d, channels.pcm.opened);
91745550658SPoul-Henning Kamp 
918e4e61333SAriff Abdullah 	/* XXX This is incorrect, but lets play along for now. */
919a1d444e1SAriff Abdullah 	if ((numplay == 0 || numrec == 0) && numplay != numrec)
920285648f9SCameron Grant 		d->flags |= SD_F_SIMPLEX;
921d95502a8SCameron Grant 
922bba4862cSAriff Abdullah 	sysctl_ctx_init(&d->play_sysctl_ctx);
923bba4862cSAriff Abdullah 	d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx,
924bba4862cSAriff Abdullah 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "play",
9257029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "playback channels node");
926bba4862cSAriff Abdullah 	sysctl_ctx_init(&d->rec_sysctl_ctx);
927bba4862cSAriff Abdullah 	d->rec_sysctl_tree = SYSCTL_ADD_NODE(&d->rec_sysctl_ctx,
928bba4862cSAriff Abdullah 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "rec",
929132fca63SHans Petter Selasky 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "recording channels node");
930e4e61333SAriff Abdullah 
93132a0e5d5SHans Petter Selasky 	if (numplay > 0 || numrec > 0)
932cd9766c5SCameron Grant 		d->flags |= SD_F_AUTOVCHAN;
93390da2b28SAriff Abdullah 
9349da3b645SChristos Margiolis 	sndstat_register(dev, d->status);
935e4e61333SAriff Abdullah 
936e8c0d15aSChristos Margiolis 	return (dsp_make_dev(dev));
937987e5972SCameron Grant }
938987e5972SCameron Grant 
93933dbf14aSCameron Grant int
94033dbf14aSCameron Grant pcm_unregister(device_t dev)
9417c438dbeSCameron Grant {
942e4e61333SAriff Abdullah 	struct snddev_info *d;
943a67fe5c1SCameron Grant 	struct pcm_channel *ch;
9447c438dbeSCameron Grant 
945e4e61333SAriff Abdullah 	d = device_get_softc(dev);
946e4e61333SAriff Abdullah 
947e4e61333SAriff Abdullah 	if (!PCM_ALIVE(d)) {
948e4e61333SAriff Abdullah 		device_printf(dev, "unregister: device not configured\n");
949e4e61333SAriff Abdullah 		return (0);
950e4e61333SAriff Abdullah 	}
951bba4862cSAriff Abdullah 
95290da2b28SAriff Abdullah 	PCM_LOCK(d);
953e4e61333SAriff Abdullah 	PCM_WAIT(d);
954e4e61333SAriff Abdullah 
955cc1efc23SHans Petter Selasky 	d->flags |= SD_F_DETACHING;
956cc1efc23SHans Petter Selasky 
957e4e61333SAriff Abdullah 	PCM_ACQUIRE(d);
95890da2b28SAriff Abdullah 	PCM_UNLOCK(d);
959e4e61333SAriff Abdullah 
960e4e61333SAriff Abdullah 	CHN_FOREACH(ch, d, channels.pcm) {
961e4e61333SAriff Abdullah 		CHN_LOCK(ch);
96244e128feSChristos Margiolis 		/*
96303614fcbSChristos Margiolis 		 * Do not wait for the timeout in chn_read()/chn_write(). Wake
96403614fcbSChristos Margiolis 		 * up the sleeping thread and kill the channel.
96544e128feSChristos Margiolis 		 */
96603614fcbSChristos Margiolis 		chn_shutdown(ch);
96744e128feSChristos Margiolis 		chn_abort(ch);
968e4e61333SAriff Abdullah 		CHN_UNLOCK(ch);
969c9b53085SCameron Grant 	}
9705ee30e27SMathew Kanner 
9718461d581SHans Petter Selasky 	/* remove /dev/sndstat entry first */
9728461d581SHans Petter Selasky 	sndstat_unregister(dev);
9738461d581SHans Petter Selasky 
97490da2b28SAriff Abdullah 	PCM_LOCK(d);
975e4e61333SAriff Abdullah 	d->flags |= SD_F_DYING;
976e4e61333SAriff Abdullah 	d->flags &= ~SD_F_REGISTERED;
97790da2b28SAriff Abdullah 	PCM_UNLOCK(d);
978e4e61333SAriff Abdullah 
979bba4862cSAriff Abdullah 	if (d->play_sysctl_tree != NULL) {
980bba4862cSAriff Abdullah 		sysctl_ctx_free(&d->play_sysctl_ctx);
981bba4862cSAriff Abdullah 		d->play_sysctl_tree = NULL;
982bba4862cSAriff Abdullah 	}
983bba4862cSAriff Abdullah 	if (d->rec_sysctl_tree != NULL) {
984bba4862cSAriff Abdullah 		sysctl_ctx_free(&d->rec_sysctl_ctx);
985bba4862cSAriff Abdullah 		d->rec_sysctl_tree = NULL;
986a1d444e1SAriff Abdullah 	}
987bba4862cSAriff Abdullah 
988074d6fbeSChristos Margiolis 	dsp_destroy_dev(dev);
989074d6fbeSChristos Margiolis 	(void)mixer_uninit(dev);
990074d6fbeSChristos Margiolis 
99103614fcbSChristos Margiolis 	pcm_killchans(d);
9927c438dbeSCameron Grant 
99390da2b28SAriff Abdullah 	PCM_LOCK(d);
994e4e61333SAriff Abdullah 	PCM_RELEASE(d);
995e4e61333SAriff Abdullah 	cv_destroy(&d->cv);
99690da2b28SAriff Abdullah 	PCM_UNLOCK(d);
99749c5e6e2SCameron Grant 	snd_mtxfree(d->lock);
998bba4862cSAriff Abdullah 
999bba4862cSAriff Abdullah 	if (snd_unit == device_get_unit(dev)) {
1000cbebc90dSAlexander Motin 		snd_unit = pcm_best_unit(-1);
1001cbebc90dSAlexander Motin 		if (snd_unit_auto == 0)
1002cbebc90dSAlexander Motin 			snd_unit_auto = 1;
1003e4e61333SAriff Abdullah 	}
1004bba4862cSAriff Abdullah 
1005e4e61333SAriff Abdullah 	return (0);
100633dbf14aSCameron Grant }
10077c438dbeSCameron Grant 
100867b1dce3SCameron Grant /************************************************************************/
100967b1dce3SCameron Grant 
1010b611c801SAlexander Leidinger /**
1011b611c801SAlexander Leidinger  * @brief	Handle OSSv4 SNDCTL_SYSINFO ioctl.
1012b611c801SAlexander Leidinger  *
1013b611c801SAlexander Leidinger  * @param si	Pointer to oss_sysinfo struct where information about the
1014b611c801SAlexander Leidinger  * 		sound subsystem will be written/copied.
1015b611c801SAlexander Leidinger  *
1016b611c801SAlexander Leidinger  * This routine returns information about the sound system, such as the
1017b611c801SAlexander Leidinger  * current OSS version, number of audio, MIDI, and mixer drivers, etc.
1018b611c801SAlexander Leidinger  * Also includes a bitmask showing which of the above types of devices
1019b611c801SAlexander Leidinger  * are open (busy).
1020b611c801SAlexander Leidinger  *
1021b611c801SAlexander Leidinger  * @note
1022b611c801SAlexander Leidinger  * Calling threads must not hold any snddev_info or pcm_channel locks.
1023b611c801SAlexander Leidinger  *
1024b611c801SAlexander Leidinger  * @author	Ryan Beasley <ryanb@FreeBSD.org>
1025b611c801SAlexander Leidinger  */
1026b611c801SAlexander Leidinger void
1027b611c801SAlexander Leidinger sound_oss_sysinfo(oss_sysinfo *si)
1028b611c801SAlexander Leidinger {
1029b611c801SAlexander Leidinger 	static char si_product[] = "FreeBSD native OSS ABI";
1030b611c801SAlexander Leidinger 	static char si_version[] = __XSTRING(__FreeBSD_version);
1031c412d503SAlexander Motin 	static char si_license[] = "BSD";
1032b611c801SAlexander Leidinger 	static int intnbits = sizeof(int) * 8;	/* Better suited as macro?
1033b611c801SAlexander Leidinger 						   Must pester a C guru. */
1034b611c801SAlexander Leidinger 
1035b611c801SAlexander Leidinger 	struct snddev_info *d;
1036b611c801SAlexander Leidinger 	struct pcm_channel *c;
1037b611c801SAlexander Leidinger 	int i, j, ncards;
1038b611c801SAlexander Leidinger 
1039b611c801SAlexander Leidinger 	ncards = 0;
1040b611c801SAlexander Leidinger 
1041b611c801SAlexander Leidinger 	strlcpy(si->product, si_product, sizeof(si->product));
1042b611c801SAlexander Leidinger 	strlcpy(si->version, si_version, sizeof(si->version));
1043b611c801SAlexander Leidinger 	si->versionnum = SOUND_VERSION;
1044c412d503SAlexander Motin 	strlcpy(si->license, si_license, sizeof(si->license));
1045b611c801SAlexander Leidinger 
1046b611c801SAlexander Leidinger 	/*
1047b611c801SAlexander Leidinger 	 * Iterate over PCM devices and their channels, gathering up data
1048b611c801SAlexander Leidinger 	 * for the numaudios, ncards, and openedaudio fields.
1049b611c801SAlexander Leidinger 	 */
1050b611c801SAlexander Leidinger 	si->numaudios = 0;
1051b611c801SAlexander Leidinger 	bzero((void *)&si->openedaudio, sizeof(si->openedaudio));
1052b611c801SAlexander Leidinger 
1053b611c801SAlexander Leidinger 	j = 0;
1054b611c801SAlexander Leidinger 
10559c271f79SAriff Abdullah 	for (i = 0; pcm_devclass != NULL &&
10569c271f79SAriff Abdullah 	    i < devclass_get_maxunit(pcm_devclass); i++) {
1057b611c801SAlexander Leidinger 		d = devclass_get_softc(pcm_devclass, i);
1058e4e61333SAriff Abdullah 		if (!PCM_REGISTERED(d))
1059b611c801SAlexander Leidinger 			continue;
1060b611c801SAlexander Leidinger 
1061e4e61333SAriff Abdullah 		/* XXX Need Giant magic entry ??? */
1062e4e61333SAriff Abdullah 
1063b611c801SAlexander Leidinger 		/* See note in function's docblock */
106490da2b28SAriff Abdullah 		PCM_UNLOCKASSERT(d);
106590da2b28SAriff Abdullah 		PCM_LOCK(d);
1066b611c801SAlexander Leidinger 
10674d2be7beSChristos Margiolis 		si->numaudios += PCM_CHANCOUNT(d);
1068b611c801SAlexander Leidinger 		++ncards;
1069b611c801SAlexander Leidinger 
1070bba4862cSAriff Abdullah 		CHN_FOREACH(c, d, channels.pcm) {
107190da2b28SAriff Abdullah 			CHN_UNLOCKASSERT(c);
1072b611c801SAlexander Leidinger 			CHN_LOCK(c);
1073b611c801SAlexander Leidinger 			if (c->flags & CHN_F_BUSY)
1074b611c801SAlexander Leidinger 				si->openedaudio[j / intnbits] |=
1075b611c801SAlexander Leidinger 				    (1 << (j % intnbits));
1076b611c801SAlexander Leidinger 			CHN_UNLOCK(c);
1077b611c801SAlexander Leidinger 			j++;
1078b611c801SAlexander Leidinger 		}
1079b611c801SAlexander Leidinger 
108090da2b28SAriff Abdullah 		PCM_UNLOCK(d);
1081b611c801SAlexander Leidinger 	}
1082c412d503SAlexander Motin 	si->numaudioengines = si->numaudios;
1083b611c801SAlexander Leidinger 
1084b611c801SAlexander Leidinger 	si->numsynths = 0;	/* OSSv4 docs:  this field is obsolete */
1085b611c801SAlexander Leidinger 	/**
1086b611c801SAlexander Leidinger 	 * @todo	Collect num{midis,timers}.
1087b611c801SAlexander Leidinger 	 *
1088b611c801SAlexander Leidinger 	 * Need access to sound/midi/midi.c::midistat_lock in order
1089b611c801SAlexander Leidinger 	 * to safely touch midi_devices and get a head count of, well,
1090b611c801SAlexander Leidinger 	 * MIDI devices.  midistat_lock is a global static (i.e., local to
1091b611c801SAlexander Leidinger 	 * midi.c), but midi_devices is a regular global; should the mutex
1092b611c801SAlexander Leidinger 	 * be publicized, or is there another way to get this information?
1093b611c801SAlexander Leidinger 	 *
1094b611c801SAlexander Leidinger 	 * NB:	MIDI/sequencer stuff is currently on hold.
1095b611c801SAlexander Leidinger 	 */
1096b611c801SAlexander Leidinger 	si->nummidis = 0;
1097b611c801SAlexander Leidinger 	si->numtimers = 0;
1098b611c801SAlexander Leidinger 	si->nummixers = mixer_count;
1099b611c801SAlexander Leidinger 	si->numcards = ncards;
1100b611c801SAlexander Leidinger 		/* OSSv4 docs:	Intended only for test apps; API doesn't
1101b611c801SAlexander Leidinger 		   really have much of a concept of cards.  Shouldn't be
1102b611c801SAlexander Leidinger 		   used by applications. */
1103b611c801SAlexander Leidinger 
1104b611c801SAlexander Leidinger 	/**
1105b611c801SAlexander Leidinger 	 * @todo	Fill in "busy devices" fields.
1106b611c801SAlexander Leidinger 	 *
1107b611c801SAlexander Leidinger 	 *  si->openedmidi = " MIDI devices
1108b611c801SAlexander Leidinger 	 */
1109b611c801SAlexander Leidinger 	bzero((void *)&si->openedmidi, sizeof(si->openedmidi));
1110b611c801SAlexander Leidinger 
1111b611c801SAlexander Leidinger 	/*
1112b611c801SAlexander Leidinger 	 * Si->filler is a reserved array, but according to docs each
1113b611c801SAlexander Leidinger 	 * element should be set to -1.
1114b611c801SAlexander Leidinger 	 */
1115b611c801SAlexander Leidinger 	for (i = 0; i < sizeof(si->filler)/sizeof(si->filler[0]); i++)
1116b611c801SAlexander Leidinger 		si->filler[i] = -1;
1117b611c801SAlexander Leidinger }
1118b611c801SAlexander Leidinger 
111952f6e09eSAlexander Motin int
112052f6e09eSAlexander Motin sound_oss_card_info(oss_card_info *si)
112152f6e09eSAlexander Motin {
112252f6e09eSAlexander Motin 	struct snddev_info *d;
112352f6e09eSAlexander Motin 	int i, ncards;
112452f6e09eSAlexander Motin 
112552f6e09eSAlexander Motin 	ncards = 0;
112652f6e09eSAlexander Motin 
112752f6e09eSAlexander Motin 	for (i = 0; pcm_devclass != NULL &&
112852f6e09eSAlexander Motin 	    i < devclass_get_maxunit(pcm_devclass); i++) {
112952f6e09eSAlexander Motin 		d = devclass_get_softc(pcm_devclass, i);
113052f6e09eSAlexander Motin 		if (!PCM_REGISTERED(d))
113152f6e09eSAlexander Motin 			continue;
113252f6e09eSAlexander Motin 
113352f6e09eSAlexander Motin 		if (ncards++ != si->card)
113452f6e09eSAlexander Motin 			continue;
113552f6e09eSAlexander Motin 
113690da2b28SAriff Abdullah 		PCM_UNLOCKASSERT(d);
113790da2b28SAriff Abdullah 		PCM_LOCK(d);
113852f6e09eSAlexander Motin 
113952f6e09eSAlexander Motin 		strlcpy(si->shortname, device_get_nameunit(d->dev),
114052f6e09eSAlexander Motin 		    sizeof(si->shortname));
114152f6e09eSAlexander Motin 		strlcpy(si->longname, device_get_desc(d->dev),
114252f6e09eSAlexander Motin 		    sizeof(si->longname));
114352f6e09eSAlexander Motin 		strlcpy(si->hw_info, d->status, sizeof(si->hw_info));
114452f6e09eSAlexander Motin 		si->intr_count = si->ack_count = 0;
114590da2b28SAriff Abdullah 
114690da2b28SAriff Abdullah 		PCM_UNLOCK(d);
114790da2b28SAriff Abdullah 
114852f6e09eSAlexander Motin 		return (0);
114952f6e09eSAlexander Motin 	}
115052f6e09eSAlexander Motin 	return (ENXIO);
115152f6e09eSAlexander Motin }
115252f6e09eSAlexander Motin 
1153b611c801SAlexander Leidinger /************************************************************************/
1154b611c801SAlexander Leidinger 
11550739ea1dSSeigo Tanimura static int
11560739ea1dSSeigo Tanimura sound_modevent(module_t mod, int type, void *data)
11570739ea1dSSeigo Tanimura {
1158b611c801SAlexander Leidinger 	int ret;
1159b611c801SAlexander Leidinger 
11608109ec9dSJohn Baldwin 	ret = 0;
1161b611c801SAlexander Leidinger 	switch (type) {
1162b611c801SAlexander Leidinger 		case MOD_LOAD:
116313bebcd3SJohn Baldwin 			pcm_devclass = devclass_create("pcm");
1164b611c801SAlexander Leidinger 			pcmsg_unrhdr = new_unrhdr(1, INT_MAX, NULL);
1165b611c801SAlexander Leidinger 			break;
1166b611c801SAlexander Leidinger 		case MOD_UNLOAD:
1167b611c801SAlexander Leidinger 			if (pcmsg_unrhdr != NULL) {
1168b611c801SAlexander Leidinger 				delete_unrhdr(pcmsg_unrhdr);
1169b611c801SAlexander Leidinger 				pcmsg_unrhdr = NULL;
1170b611c801SAlexander Leidinger 			}
1171b611c801SAlexander Leidinger 			break;
11725525b53dSAlexander Motin 		case MOD_SHUTDOWN:
11735525b53dSAlexander Motin 			break;
1174b611c801SAlexander Leidinger 		default:
117590da2b28SAriff Abdullah 			ret = ENOTSUP;
1176b611c801SAlexander Leidinger 	}
1177b611c801SAlexander Leidinger 
1178b611c801SAlexander Leidinger 	return ret;
11790739ea1dSSeigo Tanimura }
11800739ea1dSSeigo Tanimura 
11810739ea1dSSeigo Tanimura DEV_MODULE(sound, sound_modevent, NULL);
11820739ea1dSSeigo Tanimura MODULE_VERSION(sound, SOUND_MODVER);
1183