xref: /freebsd/sys/dev/sound/pcm/sound.c (revision bc96366c864c07ef352edb92017357917c75b36c)
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 = -1;
55 SYSCTL_INT(_hw_snd, OID_AUTO, default_auto, CTLFLAG_RWTUN,
56     &snd_unit_auto, 0, "assign default unit to a newly attached device");
57 
58 int snd_maxautovchans = 16;
59 /* XXX: a tunable implies that we may need more than one sound channel before
60    the system can change a sysctl (/etc/sysctl.conf), do we really need
61    this? */
62 TUNABLE_INT("hw.snd.maxautovchans", &snd_maxautovchans);
63 
64 SYSCTL_NODE(_hw, OID_AUTO, snd, CTLFLAG_RD, 0, "Sound driver");
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_RW | CTLFLAG_ANYBODY,
448 	    0, 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, CTLTYPE_INT | CTLFLAG_RW,
478             0, sizeof(int), sysctl_hw_snd_maxautovchans, "I", "maximum virtual channel");
479 
480 struct pcm_channel *
481 pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo)
482 {
483 	struct pcm_channel *ch;
484 	int direction, err, rpnum, *pnum, max;
485 	int udc, device, chan;
486 	char *dirs, *devname, buf[CHN_NAMELEN];
487 
488 	PCM_BUSYASSERT(d);
489 	PCM_LOCKASSERT(d);
490 	KASSERT(num >= -1, ("invalid num=%d", num));
491 
492 
493 	switch (dir) {
494 	case PCMDIR_PLAY:
495 		dirs = "play";
496 		direction = PCMDIR_PLAY;
497 		pnum = &d->playcount;
498 		device = SND_DEV_DSPHW_PLAY;
499 		max = SND_MAXHWCHAN;
500 		break;
501 	case PCMDIR_PLAY_VIRTUAL:
502 		dirs = "virtual";
503 		direction = PCMDIR_PLAY;
504 		pnum = &d->pvchancount;
505 		device = SND_DEV_DSPHW_VPLAY;
506 		max = SND_MAXVCHANS;
507 		break;
508 	case PCMDIR_REC:
509 		dirs = "record";
510 		direction = PCMDIR_REC;
511 		pnum = &d->reccount;
512 		device = SND_DEV_DSPHW_REC;
513 		max = SND_MAXHWCHAN;
514 		break;
515 	case PCMDIR_REC_VIRTUAL:
516 		dirs = "virtual";
517 		direction = PCMDIR_REC;
518 		pnum = &d->rvchancount;
519 		device = SND_DEV_DSPHW_VREC;
520 		max = SND_MAXVCHANS;
521 		break;
522 	default:
523 		return (NULL);
524 	}
525 
526 	chan = (num == -1) ? 0 : num;
527 
528 	if (*pnum >= max || chan >= max)
529 		return (NULL);
530 
531 	rpnum = 0;
532 
533 	CHN_FOREACH(ch, d, channels.pcm) {
534 		if (CHN_DEV(ch) != device)
535 			continue;
536 		if (chan == CHN_CHAN(ch)) {
537 			if (num != -1) {
538 				device_printf(d->dev,
539 				    "channel num=%d allocated!\n", chan);
540 				return (NULL);
541 			}
542 			chan++;
543 			if (chan >= max) {
544 				device_printf(d->dev,
545 				    "chan=%d > %d\n", chan, max);
546 				return (NULL);
547 			}
548 		}
549 		rpnum++;
550 	}
551 
552 	if (*pnum != rpnum) {
553 		device_printf(d->dev,
554 		    "%s(): WARNING: pnum screwed : dirs=%s pnum=%d rpnum=%d\n",
555 		    __func__, dirs, *pnum, rpnum);
556 		return (NULL);
557 	}
558 
559 	udc = snd_mkunit(device_get_unit(d->dev), device, chan);
560 	devname = dsp_unit2name(buf, sizeof(buf), udc);
561 
562 	if (devname == NULL) {
563 		device_printf(d->dev,
564 		    "Failed to query device name udc=0x%08x\n", udc);
565 		return (NULL);
566 	}
567 
568 	PCM_UNLOCK(d);
569 	ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
570 	ch->methods = kobj_create(cls, M_DEVBUF, M_WAITOK | M_ZERO);
571 	ch->unit = udc;
572 	ch->pid = -1;
573 	strlcpy(ch->comm, CHN_COMM_UNUSED, sizeof(ch->comm));
574 	ch->parentsnddev = d;
575 	ch->parentchannel = parent;
576 	ch->dev = d->dev;
577 	ch->trigger = PCMTRIG_STOP;
578 	snprintf(ch->name, sizeof(ch->name), "%s:%s:%s",
579 	    device_get_nameunit(ch->dev), dirs, devname);
580 
581 	err = chn_init(ch, devinfo, dir, direction);
582 	PCM_LOCK(d);
583 	if (err) {
584 		device_printf(d->dev, "chn_init(%s) failed: err = %d\n",
585 		    ch->name, err);
586 		kobj_delete(ch->methods, M_DEVBUF);
587 		free(ch, M_DEVBUF);
588 		return (NULL);
589 	}
590 
591 	return (ch);
592 }
593 
594 int
595 pcm_chn_destroy(struct pcm_channel *ch)
596 {
597 	struct snddev_info *d;
598 	int err;
599 
600 	d = ch->parentsnddev;
601 	PCM_BUSYASSERT(d);
602 
603 	err = chn_kill(ch);
604 	if (err) {
605 		device_printf(ch->dev, "chn_kill(%s) failed, err = %d\n",
606 		    ch->name, err);
607 		return (err);
608 	}
609 
610 	kobj_delete(ch->methods, M_DEVBUF);
611 	free(ch, M_DEVBUF);
612 
613 	return (0);
614 }
615 
616 int
617 pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch)
618 {
619 	PCM_BUSYASSERT(d);
620 	PCM_LOCKASSERT(d);
621 	KASSERT(ch != NULL && (ch->direction == PCMDIR_PLAY ||
622 	    ch->direction == PCMDIR_REC), ("Invalid pcm channel"));
623 
624 	CHN_INSERT_SORT_ASCEND(d, ch, channels.pcm);
625 
626 	switch (CHN_DEV(ch)) {
627 	case SND_DEV_DSPHW_PLAY:
628 		d->playcount++;
629 		break;
630 	case SND_DEV_DSPHW_VPLAY:
631 		d->pvchancount++;
632 		break;
633 	case SND_DEV_DSPHW_REC:
634 		d->reccount++;
635 		break;
636 	case SND_DEV_DSPHW_VREC:
637 		d->rvchancount++;
638 		break;
639 	default:
640 		break;
641 	}
642 
643 	d->devcount++;
644 
645 	return (0);
646 }
647 
648 int
649 pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch)
650 {
651 	struct pcm_channel *tmp;
652 
653 	PCM_BUSYASSERT(d);
654 	PCM_LOCKASSERT(d);
655 
656 	tmp = NULL;
657 
658 	CHN_FOREACH(tmp, d, channels.pcm) {
659 		if (tmp == ch)
660 			break;
661 	}
662 
663 	if (tmp != ch)
664 		return (EINVAL);
665 
666 	CHN_REMOVE(d, ch, channels.pcm);
667 
668 	switch (CHN_DEV(ch)) {
669 	case SND_DEV_DSPHW_PLAY:
670 		d->playcount--;
671 		break;
672 	case SND_DEV_DSPHW_VPLAY:
673 		d->pvchancount--;
674 		break;
675 	case SND_DEV_DSPHW_REC:
676 		d->reccount--;
677 		break;
678 	case SND_DEV_DSPHW_VREC:
679 		d->rvchancount--;
680 		break;
681 	default:
682 		break;
683 	}
684 
685 	d->devcount--;
686 
687 	return (0);
688 }
689 
690 int
691 pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
692 {
693 	struct snddev_info *d = device_get_softc(dev);
694 	struct pcm_channel *ch;
695 	int err;
696 
697 	PCM_BUSYASSERT(d);
698 
699 	PCM_LOCK(d);
700 	ch = pcm_chn_create(d, NULL, cls, dir, -1, devinfo);
701 	if (!ch) {
702 		device_printf(d->dev, "pcm_chn_create(%s, %d, %p) failed\n",
703 		    cls->name, dir, devinfo);
704 		PCM_UNLOCK(d);
705 		return (ENODEV);
706 	}
707 
708 	err = pcm_chn_add(d, ch);
709 	PCM_UNLOCK(d);
710 	if (err) {
711 		device_printf(d->dev, "pcm_chn_add(%s) failed, err=%d\n",
712 		    ch->name, err);
713 		pcm_chn_destroy(ch);
714 	}
715 
716 	return (err);
717 }
718 
719 static int
720 pcm_killchan(device_t dev)
721 {
722 	struct snddev_info *d = device_get_softc(dev);
723 	struct pcm_channel *ch;
724 	int error;
725 
726 	PCM_BUSYASSERT(d);
727 
728 	ch = CHN_FIRST(d, channels.pcm);
729 
730 	PCM_LOCK(d);
731 	error = pcm_chn_remove(d, ch);
732 	PCM_UNLOCK(d);
733 	if (error)
734 		return (error);
735 	return (pcm_chn_destroy(ch));
736 }
737 
738 static int
739 pcm_best_unit(int old)
740 {
741 	struct snddev_info *d;
742 	int i, best, bestprio, prio;
743 
744 	best = -1;
745 	bestprio = -100;
746 	for (i = 0; pcm_devclass != NULL &&
747 	    i < devclass_get_maxunit(pcm_devclass); i++) {
748 		d = devclass_get_softc(pcm_devclass, i);
749 		if (!PCM_REGISTERED(d))
750 			continue;
751 		prio = 0;
752 		if (d->playcount == 0)
753 			prio -= 10;
754 		if (d->reccount == 0)
755 			prio -= 2;
756 		if (prio > bestprio || (prio == bestprio && i == old)) {
757 			best = i;
758 			bestprio = prio;
759 		}
760 	}
761 	return (best);
762 }
763 
764 int
765 pcm_setstatus(device_t dev, char *str)
766 {
767 	struct snddev_info *d = device_get_softc(dev);
768 
769 	PCM_BUSYASSERT(d);
770 
771 	if (d->playcount == 0 || d->reccount == 0)
772 		d->flags |= SD_F_SIMPLEX;
773 
774 	if ((d->playcount > 0 || d->reccount > 0) &&
775 	    !(d->flags & SD_F_AUTOVCHAN)) {
776 		d->flags |= SD_F_AUTOVCHAN;
777 		vchan_initsys(dev);
778 	}
779 
780 	pcm_setmaxautovchans(d, snd_maxautovchans);
781 
782 	strlcpy(d->status, str, SND_STATUSLEN);
783 
784 	PCM_LOCK(d);
785 
786 	/* Last stage, enable cloning. */
787 	if (d->clones != NULL)
788 		(void)snd_clone_enable(d->clones);
789 
790 	/* Done, we're ready.. */
791 	d->flags |= SD_F_REGISTERED;
792 
793 	PCM_RELEASE(d);
794 
795 	PCM_UNLOCK(d);
796 
797 	if (snd_unit_auto < 0)
798 		snd_unit_auto = (snd_unit < 0) ? 1 : 0;
799 	if (snd_unit < 0 || snd_unit_auto > 1)
800 		snd_unit = device_get_unit(dev);
801 	else if (snd_unit_auto == 1)
802 		snd_unit = pcm_best_unit(snd_unit);
803 
804 	return (0);
805 }
806 
807 uint32_t
808 pcm_getflags(device_t dev)
809 {
810 	struct snddev_info *d = device_get_softc(dev);
811 
812 	return d->flags;
813 }
814 
815 void
816 pcm_setflags(device_t dev, uint32_t val)
817 {
818 	struct snddev_info *d = device_get_softc(dev);
819 
820 	d->flags = val;
821 }
822 
823 void *
824 pcm_getdevinfo(device_t dev)
825 {
826 	struct snddev_info *d = device_get_softc(dev);
827 
828 	return d->devinfo;
829 }
830 
831 unsigned int
832 pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz)
833 {
834 	struct snddev_info *d = device_get_softc(dev);
835 	int sz, x;
836 
837 	sz = 0;
838 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "buffersize", &sz) == 0) {
839 		x = sz;
840 		RANGE(sz, minbufsz, maxbufsz);
841 		if (x != sz)
842 			device_printf(dev, "'buffersize=%d' hint is out of range (%d-%d), using %d\n", x, minbufsz, maxbufsz, sz);
843 		x = minbufsz;
844 		while (x < sz)
845 			x <<= 1;
846 		if (x > sz)
847 			x >>= 1;
848 		if (x != sz) {
849 			device_printf(dev, "'buffersize=%d' hint is not a power of 2, using %d\n", sz, x);
850 			sz = x;
851 		}
852 	} else {
853 		sz = deflt;
854 	}
855 
856 	d->bufsz = sz;
857 
858 	return sz;
859 }
860 
861 static int
862 sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)
863 {
864 	struct snddev_info *d;
865 	int err, val;
866 
867 	d = oidp->oid_arg1;
868 	if (!PCM_REGISTERED(d))
869 		return (ENODEV);
870 
871 	PCM_LOCK(d);
872 	PCM_WAIT(d);
873 	val = (d->flags & SD_F_BITPERFECT) ? 1 : 0;
874 	PCM_ACQUIRE(d);
875 	PCM_UNLOCK(d);
876 
877 	err = sysctl_handle_int(oidp, &val, 0, req);
878 
879 	if (err == 0 && req->newptr != NULL) {
880 		if (!(val == 0 || val == 1)) {
881 			PCM_RELEASE_QUICK(d);
882 			return (EINVAL);
883 		}
884 
885 		PCM_LOCK(d);
886 
887 		d->flags &= ~SD_F_BITPERFECT;
888 		d->flags |= (val != 0) ? SD_F_BITPERFECT : 0;
889 
890 		PCM_RELEASE(d);
891 		PCM_UNLOCK(d);
892 	} else
893 		PCM_RELEASE_QUICK(d);
894 
895 	return (err);
896 }
897 
898 #ifdef SND_DEBUG
899 static int
900 sysctl_dev_pcm_clone_flags(SYSCTL_HANDLER_ARGS)
901 {
902 	struct snddev_info *d;
903 	uint32_t flags;
904 	int err;
905 
906 	d = oidp->oid_arg1;
907 	if (!PCM_REGISTERED(d) || d->clones == NULL)
908 		return (ENODEV);
909 
910 	PCM_ACQUIRE_QUICK(d);
911 
912 	flags = snd_clone_getflags(d->clones);
913 	err = sysctl_handle_int(oidp, &flags, 0, req);
914 
915 	if (err == 0 && req->newptr != NULL) {
916 		if (flags & ~SND_CLONE_MASK)
917 			err = EINVAL;
918 		else
919 			(void)snd_clone_setflags(d->clones, flags);
920 	}
921 
922 	PCM_RELEASE_QUICK(d);
923 
924 	return (err);
925 }
926 
927 static int
928 sysctl_dev_pcm_clone_deadline(SYSCTL_HANDLER_ARGS)
929 {
930 	struct snddev_info *d;
931 	int err, deadline;
932 
933 	d = oidp->oid_arg1;
934 	if (!PCM_REGISTERED(d) || d->clones == NULL)
935 		return (ENODEV);
936 
937 	PCM_ACQUIRE_QUICK(d);
938 
939 	deadline = snd_clone_getdeadline(d->clones);
940 	err = sysctl_handle_int(oidp, &deadline, 0, req);
941 
942 	if (err == 0 && req->newptr != NULL) {
943 		if (deadline < 0)
944 			err = EINVAL;
945 		else
946 			(void)snd_clone_setdeadline(d->clones, deadline);
947 	}
948 
949 	PCM_RELEASE_QUICK(d);
950 
951 	return (err);
952 }
953 
954 static int
955 sysctl_dev_pcm_clone_gc(SYSCTL_HANDLER_ARGS)
956 {
957 	struct snddev_info *d;
958 	int err, val;
959 
960 	d = oidp->oid_arg1;
961 	if (!PCM_REGISTERED(d) || d->clones == NULL)
962 		return (ENODEV);
963 
964 	val = 0;
965 	err = sysctl_handle_int(oidp, &val, 0, req);
966 
967 	if (err == 0 && req->newptr != NULL && val != 0) {
968 		PCM_ACQUIRE_QUICK(d);
969 		val = snd_clone_gc(d->clones);
970 		PCM_RELEASE_QUICK(d);
971 		if (bootverbose != 0 || snd_verbose > 3)
972 			device_printf(d->dev, "clone gc: pruned=%d\n", val);
973 	}
974 
975 	return (err);
976 }
977 
978 static int
979 sysctl_hw_snd_clone_gc(SYSCTL_HANDLER_ARGS)
980 {
981 	struct snddev_info *d;
982 	int i, err, val;
983 
984 	val = 0;
985 	err = sysctl_handle_int(oidp, &val, 0, req);
986 
987 	if (err == 0 && req->newptr != NULL && val != 0) {
988 		for (i = 0; pcm_devclass != NULL &&
989 		    i < devclass_get_maxunit(pcm_devclass); i++) {
990 			d = devclass_get_softc(pcm_devclass, i);
991 			if (!PCM_REGISTERED(d) || d->clones == NULL)
992 				continue;
993 			PCM_ACQUIRE_QUICK(d);
994 			val = snd_clone_gc(d->clones);
995 			PCM_RELEASE_QUICK(d);
996 			if (bootverbose != 0 || snd_verbose > 3)
997 				device_printf(d->dev, "clone gc: pruned=%d\n",
998 				    val);
999 		}
1000 	}
1001 
1002 	return (err);
1003 }
1004 SYSCTL_PROC(_hw_snd, OID_AUTO, clone_gc, CTLTYPE_INT | CTLFLAG_RW,
1005     0, sizeof(int), sysctl_hw_snd_clone_gc, "I",
1006     "global clone garbage collector");
1007 #endif
1008 
1009 int
1010 pcm_register(device_t dev, void *devinfo, int numplay, int numrec)
1011 {
1012 	struct snddev_info *d;
1013 	int i;
1014 
1015 	if (pcm_veto_load) {
1016 		device_printf(dev, "disabled due to an error while initialising: %d\n", pcm_veto_load);
1017 
1018 		return EINVAL;
1019 	}
1020 
1021 	if (device_get_unit(dev) > PCMMAXUNIT) {
1022 		device_printf(dev, "PCMMAXUNIT reached : unit=%d > %d\n",
1023 		    device_get_unit(dev), PCMMAXUNIT);
1024 		device_printf(dev,
1025 		    "Use 'hw.snd.maxunit' tunable to raise the limit.\n");
1026 		return ENODEV;
1027 	}
1028 
1029 	d = device_get_softc(dev);
1030 	d->dev = dev;
1031 	d->lock = snd_mtxcreate(device_get_nameunit(dev), "sound cdev");
1032 	cv_init(&d->cv, device_get_nameunit(dev));
1033 	PCM_ACQUIRE_QUICK(d);
1034 	dsp_cdevinfo_init(d);
1035 #if 0
1036 	/*
1037 	 * d->flags should be cleared by the allocator of the softc.
1038 	 * We cannot clear this field here because several devices set
1039 	 * this flag before calling pcm_register().
1040 	 */
1041 	d->flags = 0;
1042 #endif
1043 	i = 0;
1044 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1045 	    "vpc", &i) != 0 || i != 0)
1046 		d->flags |= SD_F_VPC;
1047 
1048 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
1049 	    "bitperfect", &i) == 0 && i != 0)
1050 		d->flags |= SD_F_BITPERFECT;
1051 
1052 	d->devinfo = devinfo;
1053 	d->devcount = 0;
1054 	d->reccount = 0;
1055 	d->playcount = 0;
1056 	d->pvchancount = 0;
1057 	d->rvchancount = 0;
1058 	d->pvchanrate = 0;
1059 	d->pvchanformat = 0;
1060 	d->rvchanrate = 0;
1061 	d->rvchanformat = 0;
1062 	d->inprog = 0;
1063 
1064 	/*
1065 	 * Create clone manager, disabled by default. Cloning will be
1066 	 * enabled during final stage of driver initialization through
1067 	 * pcm_setstatus().
1068 	 */
1069 	d->clones = snd_clone_create(SND_U_MASK | SND_D_MASK, PCMMAXCLONE,
1070 	    SND_CLONE_DEADLINE_DEFAULT, SND_CLONE_WAITOK |
1071 	    SND_CLONE_GC_ENABLE | SND_CLONE_GC_UNREF |
1072 	    SND_CLONE_GC_LASTREF | SND_CLONE_GC_EXPIRED);
1073 
1074 	CHN_INIT(d, channels.pcm);
1075 	CHN_INIT(d, channels.pcm.busy);
1076 	CHN_INIT(d, channels.pcm.opened);
1077 
1078 	/* XXX This is incorrect, but lets play along for now. */
1079 	if ((numplay == 0 || numrec == 0) && numplay != numrec)
1080 		d->flags |= SD_F_SIMPLEX;
1081 
1082 	sysctl_ctx_init(&d->play_sysctl_ctx);
1083 	d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx,
1084 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "play",
1085 	    CTLFLAG_RD, 0, "playback channels node");
1086 	sysctl_ctx_init(&d->rec_sysctl_ctx);
1087 	d->rec_sysctl_tree = SYSCTL_ADD_NODE(&d->rec_sysctl_ctx,
1088 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "rec",
1089 	    CTLFLAG_RD, 0, "record channels node");
1090 	/* XXX: an user should be able to set this with a control tool, the
1091 	   sysadmin then needs min+max sysctls for this */
1092 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
1093 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
1094             OID_AUTO, "buffersize", CTLFLAG_RD, &d->bufsz, 0, "allocated buffer size");
1095 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1096 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1097 	    "bitperfect", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
1098 	    sysctl_dev_pcm_bitperfect, "I",
1099 	    "bit-perfect playback/recording (0=disable, 1=enable)");
1100 #ifdef SND_DEBUG
1101 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1102 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1103 	    "clone_flags", CTLTYPE_UINT | CTLFLAG_RW, d, sizeof(d),
1104 	    sysctl_dev_pcm_clone_flags, "IU",
1105 	    "clone flags");
1106 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1107 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1108 	    "clone_deadline", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
1109 	    sysctl_dev_pcm_clone_deadline, "I",
1110 	    "clone expiration deadline (ms)");
1111 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
1112 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
1113 	    "clone_gc", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
1114 	    sysctl_dev_pcm_clone_gc, "I",
1115 	    "clone garbage collector");
1116 #endif
1117 
1118 	if (numplay > 0 || numrec > 0) {
1119 		d->flags |= SD_F_AUTOVCHAN;
1120 		vchan_initsys(dev);
1121 	}
1122 
1123 	if (d->flags & SD_F_EQ)
1124 		feeder_eq_initsys(dev);
1125 
1126 	sndstat_register(dev, d->status, sndstat_prepare_pcm);
1127 
1128 	return 0;
1129 }
1130 
1131 int
1132 pcm_unregister(device_t dev)
1133 {
1134 	struct snddev_info *d;
1135 	struct pcm_channel *ch;
1136 	struct thread *td;
1137 
1138 	td = curthread;
1139 	d = device_get_softc(dev);
1140 
1141 	if (!PCM_ALIVE(d)) {
1142 		device_printf(dev, "unregister: device not configured\n");
1143 		return (0);
1144 	}
1145 
1146 	if (sndstat_acquire(td) != 0) {
1147 		device_printf(dev, "unregister: sndstat busy\n");
1148 		return (EBUSY);
1149 	}
1150 
1151 	PCM_LOCK(d);
1152 	PCM_WAIT(d);
1153 
1154 	if (d->inprog != 0) {
1155 		device_printf(dev, "unregister: operation in progress\n");
1156 		PCM_UNLOCK(d);
1157 		sndstat_release(td);
1158 		return (EBUSY);
1159 	}
1160 
1161 	PCM_ACQUIRE(d);
1162 	PCM_UNLOCK(d);
1163 
1164 	CHN_FOREACH(ch, d, channels.pcm) {
1165 		CHN_LOCK(ch);
1166 		if (ch->refcount > 0) {
1167 			device_printf(dev,
1168 			    "unregister: channel %s busy (pid %d)\n",
1169 			    ch->name, ch->pid);
1170 			CHN_UNLOCK(ch);
1171 			PCM_RELEASE_QUICK(d);
1172 			sndstat_release(td);
1173 			return (EBUSY);
1174 		}
1175 		CHN_UNLOCK(ch);
1176 	}
1177 
1178 	if (d->clones != NULL) {
1179 		if (snd_clone_busy(d->clones) != 0) {
1180 			device_printf(dev, "unregister: clone busy\n");
1181 			PCM_RELEASE_QUICK(d);
1182 			sndstat_release(td);
1183 			return (EBUSY);
1184 		} else {
1185 			PCM_LOCK(d);
1186 			(void)snd_clone_disable(d->clones);
1187 			PCM_UNLOCK(d);
1188 		}
1189 	}
1190 
1191 	if (mixer_uninit(dev) == EBUSY) {
1192 		device_printf(dev, "unregister: mixer busy\n");
1193 		PCM_LOCK(d);
1194 		if (d->clones != NULL)
1195 			(void)snd_clone_enable(d->clones);
1196 		PCM_RELEASE(d);
1197 		PCM_UNLOCK(d);
1198 		sndstat_release(td);
1199 		return (EBUSY);
1200 	}
1201 
1202 	PCM_LOCK(d);
1203 	d->flags |= SD_F_DYING;
1204 	d->flags &= ~SD_F_REGISTERED;
1205 	PCM_UNLOCK(d);
1206 
1207 	/*
1208 	 * No lock being held, so this thing can be flushed without
1209 	 * stucking into devdrn oblivion.
1210 	 */
1211 	if (d->clones != NULL) {
1212 		snd_clone_destroy(d->clones);
1213 		d->clones = NULL;
1214 	}
1215 
1216 	if (d->play_sysctl_tree != NULL) {
1217 		sysctl_ctx_free(&d->play_sysctl_ctx);
1218 		d->play_sysctl_tree = NULL;
1219 	}
1220 	if (d->rec_sysctl_tree != NULL) {
1221 		sysctl_ctx_free(&d->rec_sysctl_ctx);
1222 		d->rec_sysctl_tree = NULL;
1223 	}
1224 
1225 	while (!CHN_EMPTY(d, channels.pcm))
1226 		pcm_killchan(dev);
1227 
1228 	dsp_cdevinfo_flush(d);
1229 
1230 	PCM_LOCK(d);
1231 	PCM_RELEASE(d);
1232 	cv_destroy(&d->cv);
1233 	PCM_UNLOCK(d);
1234 	snd_mtxfree(d->lock);
1235 	sndstat_unregister(dev);
1236 	sndstat_release(td);
1237 
1238 	if (snd_unit == device_get_unit(dev)) {
1239 		snd_unit = pcm_best_unit(-1);
1240 		if (snd_unit_auto == 0)
1241 			snd_unit_auto = 1;
1242 	}
1243 
1244 	return (0);
1245 }
1246 
1247 /************************************************************************/
1248 
1249 /**
1250  * @brief	Handle OSSv4 SNDCTL_SYSINFO ioctl.
1251  *
1252  * @param si	Pointer to oss_sysinfo struct where information about the
1253  * 		sound subsystem will be written/copied.
1254  *
1255  * This routine returns information about the sound system, such as the
1256  * current OSS version, number of audio, MIDI, and mixer drivers, etc.
1257  * Also includes a bitmask showing which of the above types of devices
1258  * are open (busy).
1259  *
1260  * @note
1261  * Calling threads must not hold any snddev_info or pcm_channel locks.
1262  *
1263  * @author	Ryan Beasley <ryanb@FreeBSD.org>
1264  */
1265 void
1266 sound_oss_sysinfo(oss_sysinfo *si)
1267 {
1268 	static char si_product[] = "FreeBSD native OSS ABI";
1269 	static char si_version[] = __XSTRING(__FreeBSD_version);
1270 	static char si_license[] = "BSD";
1271 	static int intnbits = sizeof(int) * 8;	/* Better suited as macro?
1272 						   Must pester a C guru. */
1273 
1274 	struct snddev_info *d;
1275 	struct pcm_channel *c;
1276 	int i, j, ncards;
1277 
1278 	ncards = 0;
1279 
1280 	strlcpy(si->product, si_product, sizeof(si->product));
1281 	strlcpy(si->version, si_version, sizeof(si->version));
1282 	si->versionnum = SOUND_VERSION;
1283 	strlcpy(si->license, si_license, sizeof(si->license));
1284 
1285 	/*
1286 	 * Iterate over PCM devices and their channels, gathering up data
1287 	 * for the numaudios, ncards, and openedaudio fields.
1288 	 */
1289 	si->numaudios = 0;
1290 	bzero((void *)&si->openedaudio, sizeof(si->openedaudio));
1291 
1292 	j = 0;
1293 
1294 	for (i = 0; pcm_devclass != NULL &&
1295 	    i < devclass_get_maxunit(pcm_devclass); i++) {
1296 		d = devclass_get_softc(pcm_devclass, i);
1297 		if (!PCM_REGISTERED(d))
1298 			continue;
1299 
1300 		/* XXX Need Giant magic entry ??? */
1301 
1302 		/* See note in function's docblock */
1303 		PCM_UNLOCKASSERT(d);
1304 		PCM_LOCK(d);
1305 
1306 		si->numaudios += d->devcount;
1307 		++ncards;
1308 
1309 		CHN_FOREACH(c, d, channels.pcm) {
1310 			CHN_UNLOCKASSERT(c);
1311 			CHN_LOCK(c);
1312 			if (c->flags & CHN_F_BUSY)
1313 				si->openedaudio[j / intnbits] |=
1314 				    (1 << (j % intnbits));
1315 			CHN_UNLOCK(c);
1316 			j++;
1317 		}
1318 
1319 		PCM_UNLOCK(d);
1320 	}
1321 	si->numaudioengines = si->numaudios;
1322 
1323 	si->numsynths = 0;	/* OSSv4 docs:  this field is obsolete */
1324 	/**
1325 	 * @todo	Collect num{midis,timers}.
1326 	 *
1327 	 * Need access to sound/midi/midi.c::midistat_lock in order
1328 	 * to safely touch midi_devices and get a head count of, well,
1329 	 * MIDI devices.  midistat_lock is a global static (i.e., local to
1330 	 * midi.c), but midi_devices is a regular global; should the mutex
1331 	 * be publicized, or is there another way to get this information?
1332 	 *
1333 	 * NB:	MIDI/sequencer stuff is currently on hold.
1334 	 */
1335 	si->nummidis = 0;
1336 	si->numtimers = 0;
1337 	si->nummixers = mixer_count;
1338 	si->numcards = ncards;
1339 		/* OSSv4 docs:	Intended only for test apps; API doesn't
1340 		   really have much of a concept of cards.  Shouldn't be
1341 		   used by applications. */
1342 
1343 	/**
1344 	 * @todo	Fill in "busy devices" fields.
1345 	 *
1346 	 *  si->openedmidi = " MIDI devices
1347 	 */
1348 	bzero((void *)&si->openedmidi, sizeof(si->openedmidi));
1349 
1350 	/*
1351 	 * Si->filler is a reserved array, but according to docs each
1352 	 * element should be set to -1.
1353 	 */
1354 	for (i = 0; i < sizeof(si->filler)/sizeof(si->filler[0]); i++)
1355 		si->filler[i] = -1;
1356 }
1357 
1358 int
1359 sound_oss_card_info(oss_card_info *si)
1360 {
1361 	struct snddev_info *d;
1362 	int i, ncards;
1363 
1364 	ncards = 0;
1365 
1366 	for (i = 0; pcm_devclass != NULL &&
1367 	    i < devclass_get_maxunit(pcm_devclass); i++) {
1368 		d = devclass_get_softc(pcm_devclass, i);
1369 		if (!PCM_REGISTERED(d))
1370 			continue;
1371 
1372 		if (ncards++ != si->card)
1373 			continue;
1374 
1375 		PCM_UNLOCKASSERT(d);
1376 		PCM_LOCK(d);
1377 
1378 		strlcpy(si->shortname, device_get_nameunit(d->dev),
1379 		    sizeof(si->shortname));
1380 		strlcpy(si->longname, device_get_desc(d->dev),
1381 		    sizeof(si->longname));
1382 		strlcpy(si->hw_info, d->status, sizeof(si->hw_info));
1383 		si->intr_count = si->ack_count = 0;
1384 
1385 		PCM_UNLOCK(d);
1386 
1387 		return (0);
1388 	}
1389 	return (ENXIO);
1390 }
1391 
1392 /************************************************************************/
1393 
1394 static int
1395 sound_modevent(module_t mod, int type, void *data)
1396 {
1397 	int ret;
1398 #if 0
1399 	return (midi_modevent(mod, type, data));
1400 #else
1401 	ret = 0;
1402 
1403 	switch(type) {
1404 		case MOD_LOAD:
1405 			pcmsg_unrhdr = new_unrhdr(1, INT_MAX, NULL);
1406 			break;
1407 		case MOD_UNLOAD:
1408 			ret = sndstat_acquire(curthread);
1409 			if (ret != 0)
1410 				break;
1411 			if (pcmsg_unrhdr != NULL) {
1412 				delete_unrhdr(pcmsg_unrhdr);
1413 				pcmsg_unrhdr = NULL;
1414 			}
1415 			break;
1416 		case MOD_SHUTDOWN:
1417 			break;
1418 		default:
1419 			ret = ENOTSUP;
1420 	}
1421 
1422 	return ret;
1423 #endif
1424 }
1425 
1426 DEV_MODULE(sound, sound_modevent, NULL);
1427 MODULE_VERSION(sound, SOUND_MODVER);
1428