xref: /freebsd/sys/dev/sound/pcm/sound.c (revision 9e5787d2284e187abb5b654d924394a65772e004)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5  * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
6  * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
7  * Copyright (c) 1997 Luigi Rizzo
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifdef HAVE_KERNEL_OPTION_HEADERS
33 #include "opt_snd.h"
34 #endif
35 
36 #include <dev/sound/pcm/sound.h>
37 #include <dev/sound/pcm/ac97.h>
38 #include <dev/sound/pcm/vchan.h>
39 #include <dev/sound/pcm/dsp.h>
40 #include <dev/sound/pcm/sndstat.h>
41 #include <dev/sound/version.h>
42 #include <sys/limits.h>
43 #include <sys/sysctl.h>
44 
45 #include "feeder_if.h"
46 
47 SND_DECLARE_FILE("$FreeBSD$");
48 
49 devclass_t pcm_devclass;
50 
51 int pcm_veto_load = 1;
52 
53 int snd_unit = -1;
54 
55 static int snd_unit_auto = -1;
56 SYSCTL_INT(_hw_snd, OID_AUTO, default_auto, CTLFLAG_RWTUN,
57     &snd_unit_auto, 0, "assign default unit to a newly attached device");
58 
59 int snd_maxautovchans = 16;
60 
61 SYSCTL_NODE(_hw, OID_AUTO, snd, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
62     "Sound driver");
63 
64 static void pcm_sysinit(device_t);
65 
66 /*
67  * XXX I've had enough with people not telling proper version/arch
68  *     while reporting problems, not after 387397913213th questions/requests.
69  */
70 static char snd_driver_version[] =
71     __XSTRING(SND_DRV_VERSION)"/"MACHINE_ARCH;
72 SYSCTL_STRING(_hw_snd, OID_AUTO, version, CTLFLAG_RD, &snd_driver_version,
73     0, "driver version/arch");
74 
75 /**
76  * @brief Unit number allocator for syncgroup IDs
77  */
78 struct unrhdr *pcmsg_unrhdr = NULL;
79 
80 static int
81 sndstat_prepare_pcm(SNDSTAT_PREPARE_PCM_ARGS)
82 {
83 	SNDSTAT_PREPARE_PCM_BEGIN();
84 	SNDSTAT_PREPARE_PCM_END();
85 }
86 
87 void *
88 snd_mtxcreate(const char *desc, const char *type)
89 {
90 	struct mtx *m;
91 
92 	m = malloc(sizeof(*m), M_DEVBUF, M_WAITOK | M_ZERO);
93 	mtx_init(m, desc, type, MTX_DEF);
94 	return m;
95 }
96 
97 void
98 snd_mtxfree(void *m)
99 {
100 	struct mtx *mtx = m;
101 
102 	mtx_destroy(mtx);
103 	free(mtx, M_DEVBUF);
104 }
105 
106 void
107 snd_mtxassert(void *m)
108 {
109 #ifdef INVARIANTS
110 	struct mtx *mtx = m;
111 
112 	mtx_assert(mtx, MA_OWNED);
113 #endif
114 }
115 
116 int
117 snd_setup_intr(device_t dev, struct resource *res, int flags, driver_intr_t hand, void *param, void **cookiep)
118 {
119 	struct snddev_info *d;
120 
121 	flags &= INTR_MPSAFE;
122 	flags |= INTR_TYPE_AV;
123 	d = device_get_softc(dev);
124 	if (d != NULL && (flags & INTR_MPSAFE))
125 		d->flags |= SD_F_MPSAFE;
126 
127 	return bus_setup_intr(dev, res, flags, NULL, hand, param, cookiep);
128 }
129 
130 static void
131 pcm_clonereset(struct snddev_info *d)
132 {
133 	int cmax;
134 
135 	PCM_BUSYASSERT(d);
136 
137 	cmax = d->playcount + d->reccount - 1;
138 	if (d->pvchancount > 0)
139 		cmax += max(d->pvchancount, snd_maxautovchans) - 1;
140 	if (d->rvchancount > 0)
141 		cmax += max(d->rvchancount, snd_maxautovchans) - 1;
142 	if (cmax > PCMMAXCLONE)
143 		cmax = PCMMAXCLONE;
144 	(void)snd_clone_gc(d->clones);
145 	(void)snd_clone_setmaxunit(d->clones, cmax);
146 }
147 
148 int
149 pcm_setvchans(struct snddev_info *d, int direction, int newcnt, int num)
150 {
151 	struct pcm_channel *c, *ch, *nch;
152 	struct pcmchan_caps *caps;
153 	int i, err, vcnt;
154 
155 	PCM_BUSYASSERT(d);
156 
157 	if ((direction == PCMDIR_PLAY && d->playcount < 1) ||
158 	    (direction == PCMDIR_REC && d->reccount < 1))
159 		return (ENODEV);
160 
161 	if (!(d->flags & SD_F_AUTOVCHAN))
162 		return (EINVAL);
163 
164 	if (newcnt < 0 || newcnt > SND_MAXVCHANS)
165 		return (E2BIG);
166 
167 	if (direction == PCMDIR_PLAY)
168 		vcnt = d->pvchancount;
169 	else if (direction == PCMDIR_REC)
170 		vcnt = d->rvchancount;
171 	else
172 		return (EINVAL);
173 
174 	if (newcnt > vcnt) {
175 		KASSERT(num == -1 ||
176 		    (num >= 0 && num < SND_MAXVCHANS && (newcnt - 1) == vcnt),
177 		    ("bogus vchan_create() request num=%d newcnt=%d vcnt=%d",
178 		    num, newcnt, vcnt));
179 		/* add new vchans - find a parent channel first */
180 		ch = NULL;
181 		CHN_FOREACH(c, d, channels.pcm) {
182 			CHN_LOCK(c);
183 			if (c->direction == direction &&
184 			    ((c->flags & CHN_F_HAS_VCHAN) || (vcnt == 0 &&
185 			    c->refcount < 1 &&
186 			    !(c->flags & (CHN_F_BUSY | CHN_F_VIRTUAL))))) {
187 				/*
188 				 * Reuse hw channel with vchans already
189 				 * created.
190 				 */
191 				if (c->flags & CHN_F_HAS_VCHAN) {
192 					ch = c;
193 					break;
194 				}
195 				/*
196 				 * No vchans ever created, look for
197 				 * channels with supported formats.
198 				 */
199 				caps = chn_getcaps(c);
200 				if (caps == NULL) {
201 					CHN_UNLOCK(c);
202 					continue;
203 				}
204 				for (i = 0; caps->fmtlist[i] != 0; i++) {
205 					if (caps->fmtlist[i] & AFMT_CONVERTIBLE)
206 						break;
207 				}
208 				if (caps->fmtlist[i] != 0) {
209 					ch = c;
210 				    	break;
211 				}
212 			}
213 			CHN_UNLOCK(c);
214 		}
215 		if (ch == NULL)
216 			return (EBUSY);
217 		ch->flags |= CHN_F_BUSY;
218 		err = 0;
219 		while (err == 0 && newcnt > vcnt) {
220 			err = vchan_create(ch, num);
221 			if (err == 0)
222 				vcnt++;
223 			else if (err == E2BIG && newcnt > vcnt)
224 				device_printf(d->dev,
225 				    "%s: err=%d Maximum channel reached.\n",
226 				    __func__, err);
227 		}
228 		if (vcnt == 0)
229 			ch->flags &= ~CHN_F_BUSY;
230 		CHN_UNLOCK(ch);
231 		if (err != 0)
232 			return (err);
233 		else
234 			pcm_clonereset(d);
235 	} else if (newcnt < vcnt) {
236 		KASSERT(num == -1,
237 		    ("bogus vchan_destroy() request num=%d", num));
238 		CHN_FOREACH(c, d, channels.pcm) {
239 			CHN_LOCK(c);
240 			if (c->direction != direction ||
241 			    CHN_EMPTY(c, children) ||
242 			    !(c->flags & CHN_F_HAS_VCHAN)) {
243 				CHN_UNLOCK(c);
244 				continue;
245 			}
246 			CHN_FOREACH_SAFE(ch, c, nch, children) {
247 				CHN_LOCK(ch);
248 				if (vcnt == 1 && c->refcount > 0) {
249 					CHN_UNLOCK(ch);
250 					break;
251 				}
252 				if (!(ch->flags & CHN_F_BUSY) &&
253 				    ch->refcount < 1) {
254 					err = vchan_destroy(ch);
255 					if (err == 0)
256 						vcnt--;
257 				} else
258 					CHN_UNLOCK(ch);
259 				if (vcnt == newcnt)
260 					break;
261 			}
262 			CHN_UNLOCK(c);
263 			break;
264 		}
265 		pcm_clonereset(d);
266 	}
267 
268 	return (0);
269 }
270 
271 /* return error status and a locked channel */
272 int
273 pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction,
274     pid_t pid, char *comm, int devunit)
275 {
276 	struct pcm_channel *c;
277 	int err, vchancount, vchan_num;
278 
279 	KASSERT(d != NULL && ch != NULL && (devunit == -1 ||
280 	    !(devunit & ~(SND_U_MASK | SND_D_MASK | SND_C_MASK))) &&
281 	    (direction == PCMDIR_PLAY || direction == PCMDIR_REC),
282 	    ("%s(): invalid d=%p ch=%p direction=%d pid=%d devunit=%d",
283 	    __func__, d, ch, direction, pid, devunit));
284 	PCM_BUSYASSERT(d);
285 
286 	/* Double check again. */
287 	if (devunit != -1) {
288 		switch (snd_unit2d(devunit)) {
289 		case SND_DEV_DSPHW_PLAY:
290 		case SND_DEV_DSPHW_VPLAY:
291 			if (direction != PCMDIR_PLAY)
292 				return (ENOTSUP);
293 			break;
294 		case SND_DEV_DSPHW_REC:
295 		case SND_DEV_DSPHW_VREC:
296 			if (direction != PCMDIR_REC)
297 				return (ENOTSUP);
298 			break;
299 		default:
300 			if (!(direction == PCMDIR_PLAY ||
301 			    direction == PCMDIR_REC))
302 				return (ENOTSUP);
303 			break;
304 		}
305 	}
306 
307 	*ch = NULL;
308 	vchan_num = 0;
309 	vchancount = (direction == PCMDIR_PLAY) ? d->pvchancount :
310 	    d->rvchancount;
311 
312 retry_chnalloc:
313 	err = ENOTSUP;
314 	/* scan for a free channel */
315 	CHN_FOREACH(c, d, channels.pcm) {
316 		CHN_LOCK(c);
317 		if (devunit == -1 && c->direction == direction &&
318 		    (c->flags & CHN_F_VIRTUAL)) {
319 			if (vchancount < snd_maxautovchans &&
320 			    vchan_num < CHN_CHAN(c)) {
321 			    	CHN_UNLOCK(c);
322 				goto vchan_alloc;
323 			}
324 			vchan_num++;
325 		}
326 		if (c->direction == direction && !(c->flags & CHN_F_BUSY) &&
327 		    (devunit == -1 || devunit == -2 || c->unit == devunit)) {
328 			c->flags |= CHN_F_BUSY;
329 			c->pid = pid;
330 			strlcpy(c->comm, (comm != NULL) ? comm :
331 			    CHN_COMM_UNKNOWN, sizeof(c->comm));
332 			*ch = c;
333 			return (0);
334 		} else if (c->unit == devunit) {
335 			if (c->direction != direction)
336 				err = ENOTSUP;
337 			else if (c->flags & CHN_F_BUSY)
338 				err = EBUSY;
339 			else
340 				err = EINVAL;
341 			CHN_UNLOCK(c);
342 			return (err);
343 		} else if ((devunit == -1 || devunit == -2) &&
344 		    c->direction == direction && (c->flags & CHN_F_BUSY))
345 			err = EBUSY;
346 		CHN_UNLOCK(c);
347 	}
348 
349 	if (devunit == -2)
350 		return (err);
351 
352 vchan_alloc:
353 	/* no channel available */
354 	if (devunit == -1 || snd_unit2d(devunit) == SND_DEV_DSPHW_VPLAY ||
355 	    snd_unit2d(devunit) == SND_DEV_DSPHW_VREC) {
356 		if (!(vchancount > 0 && vchancount < snd_maxautovchans) &&
357 		    (devunit == -1 || snd_unit2c(devunit) < snd_maxautovchans))
358 			return (err);
359 		err = pcm_setvchans(d, direction, vchancount + 1,
360 		    (devunit == -1) ? -1 : snd_unit2c(devunit));
361 		if (err == 0) {
362 			if (devunit == -1)
363 				devunit = -2;
364 			goto retry_chnalloc;
365 		}
366 	}
367 
368 	return (err);
369 }
370 
371 /* release a locked channel and unlock it */
372 int
373 pcm_chnrelease(struct pcm_channel *c)
374 {
375 	PCM_BUSYASSERT(c->parentsnddev);
376 	CHN_LOCKASSERT(c);
377 
378 	c->flags &= ~CHN_F_BUSY;
379 	c->pid = -1;
380 	strlcpy(c->comm, CHN_COMM_UNUSED, sizeof(c->comm));
381 	CHN_UNLOCK(c);
382 
383 	return (0);
384 }
385 
386 int
387 pcm_chnref(struct pcm_channel *c, int ref)
388 {
389 	PCM_BUSYASSERT(c->parentsnddev);
390 	CHN_LOCKASSERT(c);
391 
392 	c->refcount += ref;
393 
394 	return (c->refcount);
395 }
396 
397 int
398 pcm_inprog(struct snddev_info *d, int delta)
399 {
400 	PCM_LOCKASSERT(d);
401 
402 	d->inprog += delta;
403 
404 	return (d->inprog);
405 }
406 
407 static void
408 pcm_setmaxautovchans(struct snddev_info *d, int num)
409 {
410 	PCM_BUSYASSERT(d);
411 
412 	if (num < 0)
413 		return;
414 
415 	if (num >= 0 && d->pvchancount > num)
416 		(void)pcm_setvchans(d, PCMDIR_PLAY, num, -1);
417 	else if (num > 0 && d->pvchancount == 0)
418 		(void)pcm_setvchans(d, PCMDIR_PLAY, 1, -1);
419 
420 	if (num >= 0 && d->rvchancount > num)
421 		(void)pcm_setvchans(d, PCMDIR_REC, num, -1);
422 	else if (num > 0 && d->rvchancount == 0)
423 		(void)pcm_setvchans(d, PCMDIR_REC, 1, -1);
424 
425 	pcm_clonereset(d);
426 }
427 
428 static int
429 sysctl_hw_snd_default_unit(SYSCTL_HANDLER_ARGS)
430 {
431 	struct snddev_info *d;
432 	int error, unit;
433 
434 	unit = snd_unit;
435 	error = sysctl_handle_int(oidp, &unit, 0, req);
436 	if (error == 0 && req->newptr != NULL) {
437 		d = devclass_get_softc(pcm_devclass, unit);
438 		if (!PCM_REGISTERED(d) || CHN_EMPTY(d, channels.pcm))
439 			return EINVAL;
440 		snd_unit = unit;
441 		snd_unit_auto = 0;
442 	}
443 	return (error);
444 }
445 /* XXX: do we need a way to let the user change the default unit? */
446 SYSCTL_PROC(_hw_snd, OID_AUTO, default_unit,
447     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_ANYBODY | CTLFLAG_NEEDGIANT, 0,
448     sizeof(int), sysctl_hw_snd_default_unit, "I",
449     "default sound device");
450 
451 static int
452 sysctl_hw_snd_maxautovchans(SYSCTL_HANDLER_ARGS)
453 {
454 	struct snddev_info *d;
455 	int i, v, error;
456 
457 	v = snd_maxautovchans;
458 	error = sysctl_handle_int(oidp, &v, 0, req);
459 	if (error == 0 && req->newptr != NULL) {
460 		if (v < 0)
461 			v = 0;
462 		if (v > SND_MAXVCHANS)
463 			v = SND_MAXVCHANS;
464 		snd_maxautovchans = v;
465 		for (i = 0; pcm_devclass != NULL &&
466 		    i < devclass_get_maxunit(pcm_devclass); i++) {
467 			d = devclass_get_softc(pcm_devclass, i);
468 			if (!PCM_REGISTERED(d))
469 				continue;
470 			PCM_ACQUIRE_QUICK(d);
471 			pcm_setmaxautovchans(d, v);
472 			PCM_RELEASE_QUICK(d);
473 		}
474 	}
475 	return (error);
476 }
477 SYSCTL_PROC(_hw_snd, OID_AUTO, maxautovchans,
478     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
479     sysctl_hw_snd_maxautovchans, "I",
480     "maximum virtual channel");
481 
482 struct pcm_channel *
483 pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo)
484 {
485 	struct pcm_channel *ch;
486 	int direction, err, rpnum, *pnum, max;
487 	int udc, device, chan;
488 	char *dirs, *devname, buf[CHN_NAMELEN];
489 
490 	PCM_BUSYASSERT(d);
491 	PCM_LOCKASSERT(d);
492 	KASSERT(num >= -1, ("invalid num=%d", num));
493 
494 
495 	switch (dir) {
496 	case PCMDIR_PLAY:
497 		dirs = "play";
498 		direction = PCMDIR_PLAY;
499 		pnum = &d->playcount;
500 		device = SND_DEV_DSPHW_PLAY;
501 		max = SND_MAXHWCHAN;
502 		break;
503 	case PCMDIR_PLAY_VIRTUAL:
504 		dirs = "virtual";
505 		direction = PCMDIR_PLAY;
506 		pnum = &d->pvchancount;
507 		device = SND_DEV_DSPHW_VPLAY;
508 		max = SND_MAXVCHANS;
509 		break;
510 	case PCMDIR_REC:
511 		dirs = "record";
512 		direction = PCMDIR_REC;
513 		pnum = &d->reccount;
514 		device = SND_DEV_DSPHW_REC;
515 		max = SND_MAXHWCHAN;
516 		break;
517 	case PCMDIR_REC_VIRTUAL:
518 		dirs = "virtual";
519 		direction = PCMDIR_REC;
520 		pnum = &d->rvchancount;
521 		device = SND_DEV_DSPHW_VREC;
522 		max = SND_MAXVCHANS;
523 		break;
524 	default:
525 		return (NULL);
526 	}
527 
528 	chan = (num == -1) ? 0 : num;
529 
530 	if (*pnum >= max || chan >= max)
531 		return (NULL);
532 
533 	rpnum = 0;
534 
535 	CHN_FOREACH(ch, d, channels.pcm) {
536 		if (CHN_DEV(ch) != device)
537 			continue;
538 		if (chan == CHN_CHAN(ch)) {
539 			if (num != -1) {
540 				device_printf(d->dev,
541 				    "channel num=%d allocated!\n", chan);
542 				return (NULL);
543 			}
544 			chan++;
545 			if (chan >= max) {
546 				device_printf(d->dev,
547 				    "chan=%d > %d\n", chan, max);
548 				return (NULL);
549 			}
550 		}
551 		rpnum++;
552 	}
553 
554 	if (*pnum != rpnum) {
555 		device_printf(d->dev,
556 		    "%s(): WARNING: pnum screwed : dirs=%s pnum=%d rpnum=%d\n",
557 		    __func__, dirs, *pnum, rpnum);
558 		return (NULL);
559 	}
560 
561 	udc = snd_mkunit(device_get_unit(d->dev), device, chan);
562 	devname = dsp_unit2name(buf, sizeof(buf), udc);
563 
564 	if (devname == NULL) {
565 		device_printf(d->dev,
566 		    "Failed to query device name udc=0x%08x\n", udc);
567 		return (NULL);
568 	}
569 
570 	PCM_UNLOCK(d);
571 	ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
572 	ch->methods = kobj_create(cls, M_DEVBUF, M_WAITOK | M_ZERO);
573 	ch->unit = udc;
574 	ch->pid = -1;
575 	strlcpy(ch->comm, CHN_COMM_UNUSED, sizeof(ch->comm));
576 	ch->parentsnddev = d;
577 	ch->parentchannel = parent;
578 	ch->dev = d->dev;
579 	ch->trigger = PCMTRIG_STOP;
580 	snprintf(ch->name, sizeof(ch->name), "%s:%s:%s",
581 	    device_get_nameunit(ch->dev), dirs, devname);
582 
583 	err = chn_init(ch, devinfo, dir, direction);
584 	PCM_LOCK(d);
585 	if (err) {
586 		device_printf(d->dev, "chn_init(%s) failed: err = %d\n",
587 		    ch->name, err);
588 		kobj_delete(ch->methods, M_DEVBUF);
589 		free(ch, M_DEVBUF);
590 		return (NULL);
591 	}
592 
593 	return (ch);
594 }
595 
596 int
597 pcm_chn_destroy(struct pcm_channel *ch)
598 {
599 	struct snddev_info *d;
600 	int err;
601 
602 	d = ch->parentsnddev;
603 	PCM_BUSYASSERT(d);
604 
605 	err = chn_kill(ch);
606 	if (err) {
607 		device_printf(ch->dev, "chn_kill(%s) failed, err = %d\n",
608 		    ch->name, err);
609 		return (err);
610 	}
611 
612 	kobj_delete(ch->methods, M_DEVBUF);
613 	free(ch, M_DEVBUF);
614 
615 	return (0);
616 }
617 
618 int
619 pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch)
620 {
621 	PCM_BUSYASSERT(d);
622 	PCM_LOCKASSERT(d);
623 	KASSERT(ch != NULL && (ch->direction == PCMDIR_PLAY ||
624 	    ch->direction == PCMDIR_REC), ("Invalid pcm channel"));
625 
626 	CHN_INSERT_SORT_ASCEND(d, ch, channels.pcm);
627 
628 	switch (CHN_DEV(ch)) {
629 	case SND_DEV_DSPHW_PLAY:
630 		d->playcount++;
631 		break;
632 	case SND_DEV_DSPHW_VPLAY:
633 		d->pvchancount++;
634 		break;
635 	case SND_DEV_DSPHW_REC:
636 		d->reccount++;
637 		break;
638 	case SND_DEV_DSPHW_VREC:
639 		d->rvchancount++;
640 		break;
641 	default:
642 		break;
643 	}
644 
645 	d->devcount++;
646 
647 	return (0);
648 }
649 
650 int
651 pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch)
652 {
653 	struct pcm_channel *tmp;
654 
655 	PCM_BUSYASSERT(d);
656 	PCM_LOCKASSERT(d);
657 
658 	tmp = NULL;
659 
660 	CHN_FOREACH(tmp, d, channels.pcm) {
661 		if (tmp == ch)
662 			break;
663 	}
664 
665 	if (tmp != ch)
666 		return (EINVAL);
667 
668 	CHN_REMOVE(d, ch, channels.pcm);
669 
670 	switch (CHN_DEV(ch)) {
671 	case SND_DEV_DSPHW_PLAY:
672 		d->playcount--;
673 		break;
674 	case SND_DEV_DSPHW_VPLAY:
675 		d->pvchancount--;
676 		break;
677 	case SND_DEV_DSPHW_REC:
678 		d->reccount--;
679 		break;
680 	case SND_DEV_DSPHW_VREC:
681 		d->rvchancount--;
682 		break;
683 	default:
684 		break;
685 	}
686 
687 	d->devcount--;
688 
689 	return (0);
690 }
691 
692 int
693 pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
694 {
695 	struct snddev_info *d = device_get_softc(dev);
696 	struct pcm_channel *ch;
697 	int err;
698 
699 	PCM_BUSYASSERT(d);
700 
701 	PCM_LOCK(d);
702 	ch = pcm_chn_create(d, NULL, cls, dir, -1, devinfo);
703 	if (!ch) {
704 		device_printf(d->dev, "pcm_chn_create(%s, %d, %p) failed\n",
705 		    cls->name, dir, devinfo);
706 		PCM_UNLOCK(d);
707 		return (ENODEV);
708 	}
709 
710 	err = pcm_chn_add(d, ch);
711 	PCM_UNLOCK(d);
712 	if (err) {
713 		device_printf(d->dev, "pcm_chn_add(%s) failed, err=%d\n",
714 		    ch->name, err);
715 		pcm_chn_destroy(ch);
716 	}
717 
718 	return (err);
719 }
720 
721 static int
722 pcm_killchan(device_t dev)
723 {
724 	struct snddev_info *d = device_get_softc(dev);
725 	struct pcm_channel *ch;
726 	int error;
727 
728 	PCM_BUSYASSERT(d);
729 
730 	ch = CHN_FIRST(d, channels.pcm);
731 
732 	PCM_LOCK(d);
733 	error = pcm_chn_remove(d, ch);
734 	PCM_UNLOCK(d);
735 	if (error)
736 		return (error);
737 	return (pcm_chn_destroy(ch));
738 }
739 
740 static int
741 pcm_best_unit(int old)
742 {
743 	struct snddev_info *d;
744 	int i, best, bestprio, prio;
745 
746 	best = -1;
747 	bestprio = -100;
748 	for (i = 0; pcm_devclass != NULL &&
749 	    i < devclass_get_maxunit(pcm_devclass); i++) {
750 		d = devclass_get_softc(pcm_devclass, i);
751 		if (!PCM_REGISTERED(d))
752 			continue;
753 		prio = 0;
754 		if (d->playcount == 0)
755 			prio -= 10;
756 		if (d->reccount == 0)
757 			prio -= 2;
758 		if (prio > bestprio || (prio == bestprio && i == old)) {
759 			best = i;
760 			bestprio = prio;
761 		}
762 	}
763 	return (best);
764 }
765 
766 int
767 pcm_setstatus(device_t dev, char *str)
768 {
769 	struct snddev_info *d = device_get_softc(dev);
770 
771 	/* should only be called once */
772 	if (d->flags & SD_F_REGISTERED)
773 		return (EINVAL);
774 
775 	PCM_BUSYASSERT(d);
776 
777 	if (d->playcount == 0 || d->reccount == 0)
778 		d->flags |= SD_F_SIMPLEX;
779 
780 	if (d->playcount > 0 || d->reccount > 0)
781 		d->flags |= SD_F_AUTOVCHAN;
782 
783 	pcm_setmaxautovchans(d, snd_maxautovchans);
784 
785 	strlcpy(d->status, str, SND_STATUSLEN);
786 
787 	PCM_LOCK(d);
788 
789 	/* Last stage, enable cloning. */
790 	if (d->clones != NULL)
791 		(void)snd_clone_enable(d->clones);
792 
793 	/* Done, we're ready.. */
794 	d->flags |= SD_F_REGISTERED;
795 
796 	PCM_RELEASE(d);
797 
798 	PCM_UNLOCK(d);
799 
800 	/*
801 	 * Create all sysctls once SD_F_REGISTERED is set else
802 	 * tunable sysctls won't work:
803 	 */
804 	pcm_sysinit(dev);
805 
806 	if (snd_unit_auto < 0)
807 		snd_unit_auto = (snd_unit < 0) ? 1 : 0;
808 	if (snd_unit < 0 || snd_unit_auto > 1)
809 		snd_unit = device_get_unit(dev);
810 	else if (snd_unit_auto == 1)
811 		snd_unit = pcm_best_unit(snd_unit);
812 
813 	return (0);
814 }
815 
816 uint32_t
817 pcm_getflags(device_t dev)
818 {
819 	struct snddev_info *d = device_get_softc(dev);
820 
821 	return d->flags;
822 }
823 
824 void
825 pcm_setflags(device_t dev, uint32_t val)
826 {
827 	struct snddev_info *d = device_get_softc(dev);
828 
829 	d->flags = val;
830 }
831 
832 void *
833 pcm_getdevinfo(device_t dev)
834 {
835 	struct snddev_info *d = device_get_softc(dev);
836 
837 	return d->devinfo;
838 }
839 
840 unsigned int
841 pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz)
842 {
843 	struct snddev_info *d = device_get_softc(dev);
844 	int sz, x;
845 
846 	sz = 0;
847 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "buffersize", &sz) == 0) {
848 		x = sz;
849 		RANGE(sz, minbufsz, maxbufsz);
850 		if (x != sz)
851 			device_printf(dev, "'buffersize=%d' hint is out of range (%d-%d), using %d\n", x, minbufsz, maxbufsz, sz);
852 		x = minbufsz;
853 		while (x < sz)
854 			x <<= 1;
855 		if (x > sz)
856 			x >>= 1;
857 		if (x != sz) {
858 			device_printf(dev, "'buffersize=%d' hint is not a power of 2, using %d\n", sz, x);
859 			sz = x;
860 		}
861 	} else {
862 		sz = deflt;
863 	}
864 
865 	d->bufsz = sz;
866 
867 	return sz;
868 }
869 
870 static int
871 sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)
872 {
873 	struct snddev_info *d;
874 	int err, val;
875 
876 	d = oidp->oid_arg1;
877 	if (!PCM_REGISTERED(d))
878 		return (ENODEV);
879 
880 	PCM_LOCK(d);
881 	PCM_WAIT(d);
882 	val = (d->flags & SD_F_BITPERFECT) ? 1 : 0;
883 	PCM_ACQUIRE(d);
884 	PCM_UNLOCK(d);
885 
886 	err = sysctl_handle_int(oidp, &val, 0, req);
887 
888 	if (err == 0 && req->newptr != NULL) {
889 		if (!(val == 0 || val == 1)) {
890 			PCM_RELEASE_QUICK(d);
891 			return (EINVAL);
892 		}
893 
894 		PCM_LOCK(d);
895 
896 		d->flags &= ~SD_F_BITPERFECT;
897 		d->flags |= (val != 0) ? SD_F_BITPERFECT : 0;
898 
899 		PCM_RELEASE(d);
900 		PCM_UNLOCK(d);
901 	} else
902 		PCM_RELEASE_QUICK(d);
903 
904 	return (err);
905 }
906 
907 #ifdef SND_DEBUG
908 static int
909 sysctl_dev_pcm_clone_flags(SYSCTL_HANDLER_ARGS)
910 {
911 	struct snddev_info *d;
912 	uint32_t flags;
913 	int err;
914 
915 	d = oidp->oid_arg1;
916 	if (!PCM_REGISTERED(d) || d->clones == NULL)
917 		return (ENODEV);
918 
919 	PCM_ACQUIRE_QUICK(d);
920 
921 	flags = snd_clone_getflags(d->clones);
922 	err = sysctl_handle_int(oidp, &flags, 0, req);
923 
924 	if (err == 0 && req->newptr != NULL) {
925 		if (flags & ~SND_CLONE_MASK)
926 			err = EINVAL;
927 		else
928 			(void)snd_clone_setflags(d->clones, flags);
929 	}
930 
931 	PCM_RELEASE_QUICK(d);
932 
933 	return (err);
934 }
935 
936 static int
937 sysctl_dev_pcm_clone_deadline(SYSCTL_HANDLER_ARGS)
938 {
939 	struct snddev_info *d;
940 	int err, deadline;
941 
942 	d = oidp->oid_arg1;
943 	if (!PCM_REGISTERED(d) || d->clones == NULL)
944 		return (ENODEV);
945 
946 	PCM_ACQUIRE_QUICK(d);
947 
948 	deadline = snd_clone_getdeadline(d->clones);
949 	err = sysctl_handle_int(oidp, &deadline, 0, req);
950 
951 	if (err == 0 && req->newptr != NULL) {
952 		if (deadline < 0)
953 			err = EINVAL;
954 		else
955 			(void)snd_clone_setdeadline(d->clones, deadline);
956 	}
957 
958 	PCM_RELEASE_QUICK(d);
959 
960 	return (err);
961 }
962 
963 static int
964 sysctl_dev_pcm_clone_gc(SYSCTL_HANDLER_ARGS)
965 {
966 	struct snddev_info *d;
967 	int err, val;
968 
969 	d = oidp->oid_arg1;
970 	if (!PCM_REGISTERED(d) || d->clones == NULL)
971 		return (ENODEV);
972 
973 	val = 0;
974 	err = sysctl_handle_int(oidp, &val, 0, req);
975 
976 	if (err == 0 && req->newptr != NULL && val != 0) {
977 		PCM_ACQUIRE_QUICK(d);
978 		val = snd_clone_gc(d->clones);
979 		PCM_RELEASE_QUICK(d);
980 		if (bootverbose != 0 || snd_verbose > 3)
981 			device_printf(d->dev, "clone gc: pruned=%d\n", val);
982 	}
983 
984 	return (err);
985 }
986 
987 static int
988 sysctl_hw_snd_clone_gc(SYSCTL_HANDLER_ARGS)
989 {
990 	struct snddev_info *d;
991 	int i, err, val;
992 
993 	val = 0;
994 	err = sysctl_handle_int(oidp, &val, 0, req);
995 
996 	if (err == 0 && req->newptr != NULL && val != 0) {
997 		for (i = 0; pcm_devclass != NULL &&
998 		    i < devclass_get_maxunit(pcm_devclass); i++) {
999 			d = devclass_get_softc(pcm_devclass, i);
1000 			if (!PCM_REGISTERED(d) || d->clones == NULL)
1001 				continue;
1002 			PCM_ACQUIRE_QUICK(d);
1003 			val = snd_clone_gc(d->clones);
1004 			PCM_RELEASE_QUICK(d);
1005 			if (bootverbose != 0 || snd_verbose > 3)
1006 				device_printf(d->dev, "clone gc: pruned=%d\n",
1007 				    val);
1008 		}
1009 	}
1010 
1011 	return (err);
1012 }
1013 SYSCTL_PROC(_hw_snd, OID_AUTO, clone_gc,
1014     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, 0, sizeof(int),
1015     sysctl_hw_snd_clone_gc, "I",
1016     "global clone garbage collector");
1017 #endif
1018 
1019 static void
1020 pcm_sysinit(device_t dev)
1021 {
1022   	struct snddev_info *d = device_get_softc(dev);
1023 
1024   	/* XXX: an user should be able to set this with a control tool, the
1025 	   sysadmin then needs min+max sysctls for this */
1026 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
1027 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1028             OID_AUTO, "buffersize", CTLFLAG_RD, &d->bufsz, 0, "allocated buffer size");
1029 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1030 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1031 	    "bitperfect", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, d,
1032 	    sizeof(d), sysctl_dev_pcm_bitperfect, "I",
1033 	    "bit-perfect playback/recording (0=disable, 1=enable)");
1034 #ifdef SND_DEBUG
1035 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1036 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1037 	    "clone_flags", CTLTYPE_UINT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
1038 	    d, sizeof(d), sysctl_dev_pcm_clone_flags, "IU",
1039 	    "clone flags");
1040 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1041 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1042 	    "clone_deadline", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
1043 	    d, sizeof(d), sysctl_dev_pcm_clone_deadline, "I",
1044 	    "clone expiration deadline (ms)");
1045 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1046 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1047 	    "clone_gc",
1048 	    CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, d, sizeof(d),
1049 	    sysctl_dev_pcm_clone_gc, "I", "clone garbage collector");
1050 #endif
1051 	if (d->flags & SD_F_AUTOVCHAN)
1052 		vchan_initsys(dev);
1053 	if (d->flags & SD_F_EQ)
1054 		feeder_eq_initsys(dev);
1055 }
1056 
1057 int
1058 pcm_register(device_t dev, void *devinfo, int numplay, int numrec)
1059 {
1060 	struct snddev_info *d;
1061 	int i;
1062 
1063 	if (pcm_veto_load) {
1064 		device_printf(dev, "disabled due to an error while initialising: %d\n", pcm_veto_load);
1065 
1066 		return EINVAL;
1067 	}
1068 
1069 	if (device_get_unit(dev) > PCMMAXUNIT) {
1070 		device_printf(dev, "PCMMAXUNIT reached : unit=%d > %d\n",
1071 		    device_get_unit(dev), PCMMAXUNIT);
1072 		device_printf(dev,
1073 		    "Use 'hw.snd.maxunit' tunable to raise the limit.\n");
1074 		return ENODEV;
1075 	}
1076 
1077 	d = device_get_softc(dev);
1078 	d->dev = dev;
1079 	d->lock = snd_mtxcreate(device_get_nameunit(dev), "sound cdev");
1080 	cv_init(&d->cv, device_get_nameunit(dev));
1081 	PCM_ACQUIRE_QUICK(d);
1082 	dsp_cdevinfo_init(d);
1083 #if 0
1084 	/*
1085 	 * d->flags should be cleared by the allocator of the softc.
1086 	 * We cannot clear this field here because several devices set
1087 	 * this flag before calling pcm_register().
1088 	 */
1089 	d->flags = 0;
1090 #endif
1091 	i = 0;
1092 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1093 	    "vpc", &i) != 0 || i != 0)
1094 		d->flags |= SD_F_VPC;
1095 
1096 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1097 	    "bitperfect", &i) == 0 && i != 0)
1098 		d->flags |= SD_F_BITPERFECT;
1099 
1100 	d->devinfo = devinfo;
1101 	d->devcount = 0;
1102 	d->reccount = 0;
1103 	d->playcount = 0;
1104 	d->pvchancount = 0;
1105 	d->rvchancount = 0;
1106 	d->pvchanrate = 0;
1107 	d->pvchanformat = 0;
1108 	d->rvchanrate = 0;
1109 	d->rvchanformat = 0;
1110 	d->inprog = 0;
1111 
1112 	/*
1113 	 * Create clone manager, disabled by default. Cloning will be
1114 	 * enabled during final stage of driver initialization through
1115 	 * pcm_setstatus().
1116 	 */
1117 	d->clones = snd_clone_create(SND_U_MASK | SND_D_MASK, PCMMAXCLONE,
1118 	    SND_CLONE_DEADLINE_DEFAULT, SND_CLONE_WAITOK |
1119 	    SND_CLONE_GC_ENABLE | SND_CLONE_GC_UNREF |
1120 	    SND_CLONE_GC_LASTREF | SND_CLONE_GC_EXPIRED);
1121 
1122 	CHN_INIT(d, channels.pcm);
1123 	CHN_INIT(d, channels.pcm.busy);
1124 	CHN_INIT(d, channels.pcm.opened);
1125 
1126 	/* XXX This is incorrect, but lets play along for now. */
1127 	if ((numplay == 0 || numrec == 0) && numplay != numrec)
1128 		d->flags |= SD_F_SIMPLEX;
1129 
1130 	sysctl_ctx_init(&d->play_sysctl_ctx);
1131 	d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx,
1132 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "play",
1133 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "playback channels node");
1134 	sysctl_ctx_init(&d->rec_sysctl_ctx);
1135 	d->rec_sysctl_tree = SYSCTL_ADD_NODE(&d->rec_sysctl_ctx,
1136 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "rec",
1137 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "record channels node");
1138 
1139 	if (numplay > 0 || numrec > 0)
1140 		d->flags |= SD_F_AUTOVCHAN;
1141 
1142 	sndstat_register(dev, d->status, sndstat_prepare_pcm);
1143 
1144 	return 0;
1145 }
1146 
1147 int
1148 pcm_unregister(device_t dev)
1149 {
1150 	struct snddev_info *d;
1151 	struct pcm_channel *ch;
1152 	struct thread *td;
1153 
1154 	td = curthread;
1155 	d = device_get_softc(dev);
1156 
1157 	if (!PCM_ALIVE(d)) {
1158 		device_printf(dev, "unregister: device not configured\n");
1159 		return (0);
1160 	}
1161 
1162 	PCM_LOCK(d);
1163 	PCM_WAIT(d);
1164 
1165 	d->flags |= SD_F_DETACHING;
1166 
1167 	if (d->inprog != 0) {
1168 		device_printf(dev, "unregister: operation in progress\n");
1169 		PCM_UNLOCK(d);
1170 		return (EBUSY);
1171 	}
1172 
1173 	PCM_ACQUIRE(d);
1174 	PCM_UNLOCK(d);
1175 
1176 	CHN_FOREACH(ch, d, channels.pcm) {
1177 		CHN_LOCK(ch);
1178 		if (ch->refcount > 0) {
1179 			device_printf(dev,
1180 			    "unregister: channel %s busy (pid %d)\n",
1181 			    ch->name, ch->pid);
1182 			CHN_UNLOCK(ch);
1183 			PCM_RELEASE_QUICK(d);
1184 			return (EBUSY);
1185 		}
1186 		CHN_UNLOCK(ch);
1187 	}
1188 
1189 	if (d->clones != NULL) {
1190 		if (snd_clone_busy(d->clones) != 0) {
1191 			device_printf(dev, "unregister: clone busy\n");
1192 			PCM_RELEASE_QUICK(d);
1193 			return (EBUSY);
1194 		} else {
1195 			PCM_LOCK(d);
1196 			(void)snd_clone_disable(d->clones);
1197 			PCM_UNLOCK(d);
1198 		}
1199 	}
1200 
1201 	if (mixer_uninit(dev) == EBUSY) {
1202 		device_printf(dev, "unregister: mixer busy\n");
1203 		PCM_LOCK(d);
1204 		if (d->clones != NULL)
1205 			(void)snd_clone_enable(d->clones);
1206 		PCM_RELEASE(d);
1207 		PCM_UNLOCK(d);
1208 		return (EBUSY);
1209 	}
1210 
1211 	/* remove /dev/sndstat entry first */
1212 	sndstat_unregister(dev);
1213 
1214 	PCM_LOCK(d);
1215 	d->flags |= SD_F_DYING;
1216 	d->flags &= ~SD_F_REGISTERED;
1217 	PCM_UNLOCK(d);
1218 
1219 	/*
1220 	 * No lock being held, so this thing can be flushed without
1221 	 * stucking into devdrn oblivion.
1222 	 */
1223 	if (d->clones != NULL) {
1224 		snd_clone_destroy(d->clones);
1225 		d->clones = NULL;
1226 	}
1227 
1228 	if (d->play_sysctl_tree != NULL) {
1229 		sysctl_ctx_free(&d->play_sysctl_ctx);
1230 		d->play_sysctl_tree = NULL;
1231 	}
1232 	if (d->rec_sysctl_tree != NULL) {
1233 		sysctl_ctx_free(&d->rec_sysctl_ctx);
1234 		d->rec_sysctl_tree = NULL;
1235 	}
1236 
1237 	while (!CHN_EMPTY(d, channels.pcm))
1238 		pcm_killchan(dev);
1239 
1240 	dsp_cdevinfo_flush(d);
1241 
1242 	PCM_LOCK(d);
1243 	PCM_RELEASE(d);
1244 	cv_destroy(&d->cv);
1245 	PCM_UNLOCK(d);
1246 	snd_mtxfree(d->lock);
1247 
1248 	if (snd_unit == device_get_unit(dev)) {
1249 		snd_unit = pcm_best_unit(-1);
1250 		if (snd_unit_auto == 0)
1251 			snd_unit_auto = 1;
1252 	}
1253 
1254 	return (0);
1255 }
1256 
1257 /************************************************************************/
1258 
1259 /**
1260  * @brief	Handle OSSv4 SNDCTL_SYSINFO ioctl.
1261  *
1262  * @param si	Pointer to oss_sysinfo struct where information about the
1263  * 		sound subsystem will be written/copied.
1264  *
1265  * This routine returns information about the sound system, such as the
1266  * current OSS version, number of audio, MIDI, and mixer drivers, etc.
1267  * Also includes a bitmask showing which of the above types of devices
1268  * are open (busy).
1269  *
1270  * @note
1271  * Calling threads must not hold any snddev_info or pcm_channel locks.
1272  *
1273  * @author	Ryan Beasley <ryanb@FreeBSD.org>
1274  */
1275 void
1276 sound_oss_sysinfo(oss_sysinfo *si)
1277 {
1278 	static char si_product[] = "FreeBSD native OSS ABI";
1279 	static char si_version[] = __XSTRING(__FreeBSD_version);
1280 	static char si_license[] = "BSD";
1281 	static int intnbits = sizeof(int) * 8;	/* Better suited as macro?
1282 						   Must pester a C guru. */
1283 
1284 	struct snddev_info *d;
1285 	struct pcm_channel *c;
1286 	int i, j, ncards;
1287 
1288 	ncards = 0;
1289 
1290 	strlcpy(si->product, si_product, sizeof(si->product));
1291 	strlcpy(si->version, si_version, sizeof(si->version));
1292 	si->versionnum = SOUND_VERSION;
1293 	strlcpy(si->license, si_license, sizeof(si->license));
1294 
1295 	/*
1296 	 * Iterate over PCM devices and their channels, gathering up data
1297 	 * for the numaudios, ncards, and openedaudio fields.
1298 	 */
1299 	si->numaudios = 0;
1300 	bzero((void *)&si->openedaudio, sizeof(si->openedaudio));
1301 
1302 	j = 0;
1303 
1304 	for (i = 0; pcm_devclass != NULL &&
1305 	    i < devclass_get_maxunit(pcm_devclass); i++) {
1306 		d = devclass_get_softc(pcm_devclass, i);
1307 		if (!PCM_REGISTERED(d))
1308 			continue;
1309 
1310 		/* XXX Need Giant magic entry ??? */
1311 
1312 		/* See note in function's docblock */
1313 		PCM_UNLOCKASSERT(d);
1314 		PCM_LOCK(d);
1315 
1316 		si->numaudios += d->devcount;
1317 		++ncards;
1318 
1319 		CHN_FOREACH(c, d, channels.pcm) {
1320 			CHN_UNLOCKASSERT(c);
1321 			CHN_LOCK(c);
1322 			if (c->flags & CHN_F_BUSY)
1323 				si->openedaudio[j / intnbits] |=
1324 				    (1 << (j % intnbits));
1325 			CHN_UNLOCK(c);
1326 			j++;
1327 		}
1328 
1329 		PCM_UNLOCK(d);
1330 	}
1331 	si->numaudioengines = si->numaudios;
1332 
1333 	si->numsynths = 0;	/* OSSv4 docs:  this field is obsolete */
1334 	/**
1335 	 * @todo	Collect num{midis,timers}.
1336 	 *
1337 	 * Need access to sound/midi/midi.c::midistat_lock in order
1338 	 * to safely touch midi_devices and get a head count of, well,
1339 	 * MIDI devices.  midistat_lock is a global static (i.e., local to
1340 	 * midi.c), but midi_devices is a regular global; should the mutex
1341 	 * be publicized, or is there another way to get this information?
1342 	 *
1343 	 * NB:	MIDI/sequencer stuff is currently on hold.
1344 	 */
1345 	si->nummidis = 0;
1346 	si->numtimers = 0;
1347 	si->nummixers = mixer_count;
1348 	si->numcards = ncards;
1349 		/* OSSv4 docs:	Intended only for test apps; API doesn't
1350 		   really have much of a concept of cards.  Shouldn't be
1351 		   used by applications. */
1352 
1353 	/**
1354 	 * @todo	Fill in "busy devices" fields.
1355 	 *
1356 	 *  si->openedmidi = " MIDI devices
1357 	 */
1358 	bzero((void *)&si->openedmidi, sizeof(si->openedmidi));
1359 
1360 	/*
1361 	 * Si->filler is a reserved array, but according to docs each
1362 	 * element should be set to -1.
1363 	 */
1364 	for (i = 0; i < sizeof(si->filler)/sizeof(si->filler[0]); i++)
1365 		si->filler[i] = -1;
1366 }
1367 
1368 int
1369 sound_oss_card_info(oss_card_info *si)
1370 {
1371 	struct snddev_info *d;
1372 	int i, ncards;
1373 
1374 	ncards = 0;
1375 
1376 	for (i = 0; pcm_devclass != NULL &&
1377 	    i < devclass_get_maxunit(pcm_devclass); i++) {
1378 		d = devclass_get_softc(pcm_devclass, i);
1379 		if (!PCM_REGISTERED(d))
1380 			continue;
1381 
1382 		if (ncards++ != si->card)
1383 			continue;
1384 
1385 		PCM_UNLOCKASSERT(d);
1386 		PCM_LOCK(d);
1387 
1388 		strlcpy(si->shortname, device_get_nameunit(d->dev),
1389 		    sizeof(si->shortname));
1390 		strlcpy(si->longname, device_get_desc(d->dev),
1391 		    sizeof(si->longname));
1392 		strlcpy(si->hw_info, d->status, sizeof(si->hw_info));
1393 		si->intr_count = si->ack_count = 0;
1394 
1395 		PCM_UNLOCK(d);
1396 
1397 		return (0);
1398 	}
1399 	return (ENXIO);
1400 }
1401 
1402 /************************************************************************/
1403 
1404 static int
1405 sound_modevent(module_t mod, int type, void *data)
1406 {
1407 	int ret;
1408 #if 0
1409 	return (midi_modevent(mod, type, data));
1410 #else
1411 	ret = 0;
1412 
1413 	switch(type) {
1414 		case MOD_LOAD:
1415 			pcmsg_unrhdr = new_unrhdr(1, INT_MAX, NULL);
1416 			break;
1417 		case MOD_UNLOAD:
1418 			if (pcmsg_unrhdr != NULL) {
1419 				delete_unrhdr(pcmsg_unrhdr);
1420 				pcmsg_unrhdr = NULL;
1421 			}
1422 			break;
1423 		case MOD_SHUTDOWN:
1424 			break;
1425 		default:
1426 			ret = ENOTSUP;
1427 	}
1428 
1429 	return ret;
1430 #endif
1431 }
1432 
1433 DEV_MODULE(sound, sound_modevent, NULL);
1434 MODULE_VERSION(sound, SOUND_MODVER);
1435