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