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