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