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