xref: /freebsd/sys/dev/sound/pcm/sound.c (revision 5e9f2a6aefabec01c587a169fd5efe3a2720a8e5)
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  * Copyright (c) 2024-2025 The FreeBSD Foundation
10  *
11  * Portions of this software were developed by Christos Margiolis
12  * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifdef HAVE_KERNEL_OPTION_HEADERS
37 #include "opt_snd.h"
38 #endif
39 
40 #include <dev/sound/pcm/sound.h>
41 #include <dev/sound/pcm/ac97.h>
42 #include <dev/sound/pcm/vchan.h>
43 #include <dev/sound/pcm/dsp.h>
44 #include <dev/sound/sndstat.h>
45 #include <sys/limits.h>
46 #include <sys/sysctl.h>
47 
48 #include "feeder_if.h"
49 
50 devclass_t pcm_devclass;
51 
52 int snd_unit = -1;
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 SYSCTL_NODE(_hw, OID_AUTO, snd, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
59     "Sound driver");
60 
61 /**
62  * @brief Unit number allocator for syncgroup IDs
63  */
64 struct unrhdr *pcmsg_unrhdr = NULL;
65 
66 int
snd_setup_intr(device_t dev,struct resource * res,int flags,driver_intr_t hand,void * param,void ** cookiep)67 snd_setup_intr(device_t dev, struct resource *res, int flags, driver_intr_t hand, void *param, void **cookiep)
68 {
69 	struct snddev_info *d;
70 
71 	flags &= INTR_MPSAFE;
72 	flags |= INTR_TYPE_AV;
73 	d = device_get_softc(dev);
74 	if (d != NULL && (flags & INTR_MPSAFE))
75 		d->flags |= SD_F_MPSAFE;
76 
77 	return bus_setup_intr(dev, res, flags, NULL, hand, param, cookiep);
78 }
79 
80 static int
sysctl_hw_snd_default_unit(SYSCTL_HANDLER_ARGS)81 sysctl_hw_snd_default_unit(SYSCTL_HANDLER_ARGS)
82 {
83 	struct snddev_info *d;
84 	char buf[32];
85 	int error, unit;
86 
87 	unit = snd_unit;
88 	error = sysctl_handle_int(oidp, &unit, 0, req);
89 	if (error == 0 && req->newptr != NULL) {
90 		bus_topo_lock();
91 		d = devclass_get_softc(pcm_devclass, unit);
92 		if (!PCM_REGISTERED(d) || CHN_EMPTY(d, channels.pcm)) {
93 			bus_topo_unlock();
94 			return EINVAL;
95 		}
96 		snd_unit = unit;
97 		snd_unit_auto = 0;
98 		bus_topo_unlock();
99 
100 		snprintf(buf, sizeof(buf), "cdev=dsp%d", snd_unit);
101 		if (d->reccount > 0)
102 			devctl_notify("SND", "CONN", "IN", buf);
103 		if (d->playcount > 0)
104 			devctl_notify("SND", "CONN", "OUT", buf);
105 	}
106 	return (error);
107 }
108 /* XXX: do we need a way to let the user change the default unit? */
109 SYSCTL_PROC(_hw_snd, OID_AUTO, default_unit,
110     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 0,
111     sizeof(int), sysctl_hw_snd_default_unit, "I",
112     "default sound device");
113 
114 int
pcm_addchan(device_t dev,int dir,kobj_class_t cls,void * devinfo)115 pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
116 {
117 	struct snddev_info *d = device_get_softc(dev);
118 	struct pcm_channel *ch;
119 	int err = 0;
120 
121 	PCM_LOCK(d);
122 	PCM_WAIT(d);
123 	PCM_ACQUIRE(d);
124 	ch = chn_init(d, NULL, cls, dir, devinfo);
125 	if (!ch) {
126 		device_printf(d->dev, "chn_init(%s, %d, %p) failed\n",
127 		    cls->name, dir, devinfo);
128 		err = ENODEV;
129 	}
130 	PCM_RELEASE(d);
131 	PCM_UNLOCK(d);
132 
133 	return (err);
134 }
135 
136 static void
pcm_killchans(struct snddev_info * d)137 pcm_killchans(struct snddev_info *d)
138 {
139 	struct pcm_channel *ch;
140 	bool again;
141 
142 	PCM_BUSYASSERT(d);
143 	KASSERT(!PCM_REGISTERED(d), ("%s(): still registered\n", __func__));
144 
145 	for (;;) {
146 		again = false;
147 		/* Make sure all channels are stopped. */
148 		CHN_FOREACH(ch, d, channels.pcm) {
149 			CHN_LOCK(ch);
150 			if (ch->inprog == 0 && ch->sleeping == 0 &&
151 			    CHN_STOPPED(ch)) {
152 				CHN_UNLOCK(ch);
153 				continue;
154 			}
155 			chn_shutdown(ch);
156 			if (ch->direction == PCMDIR_PLAY)
157 				chn_flush(ch);
158 			else
159 				chn_abort(ch);
160 			CHN_UNLOCK(ch);
161 			again = true;
162 		}
163 		/*
164 		 * Some channels are still active. Sleep for a bit and try
165 		 * again.
166 		 */
167 		if (again)
168 			pause_sbt("pcmkillchans", mstosbt(5), 0, 0);
169 		else
170 			break;
171 	}
172 
173 	/* All channels are finally dead. */
174 	while (!CHN_EMPTY(d, channels.pcm)) {
175 		ch = CHN_FIRST(d, channels.pcm);
176 		chn_kill(ch);
177 	}
178 
179 	if (d->p_unr != NULL)
180 		delete_unrhdr(d->p_unr);
181 	if (d->vp_unr != NULL)
182 		delete_unrhdr(d->vp_unr);
183 	if (d->r_unr != NULL)
184 		delete_unrhdr(d->r_unr);
185 	if (d->vr_unr != NULL)
186 		delete_unrhdr(d->vr_unr);
187 }
188 
189 static int
pcm_best_unit(int old)190 pcm_best_unit(int old)
191 {
192 	struct snddev_info *d;
193 	int i, best, bestprio, prio;
194 
195 	best = -1;
196 	bestprio = -100;
197 	bus_topo_lock();
198 	for (i = 0; pcm_devclass != NULL &&
199 	    i < devclass_get_maxunit(pcm_devclass); i++) {
200 		d = devclass_get_softc(pcm_devclass, i);
201 		if (!PCM_REGISTERED(d))
202 			continue;
203 		prio = 0;
204 		if (d->playcount == 0)
205 			prio -= 10;
206 		if (d->reccount == 0)
207 			prio -= 2;
208 		if (prio > bestprio || (prio == bestprio && i == old)) {
209 			best = i;
210 			bestprio = prio;
211 		}
212 	}
213 	bus_topo_unlock();
214 
215 	return (best);
216 }
217 
218 uint32_t
pcm_getflags(device_t dev)219 pcm_getflags(device_t dev)
220 {
221 	struct snddev_info *d = device_get_softc(dev);
222 
223 	return d->flags;
224 }
225 
226 void
pcm_setflags(device_t dev,uint32_t val)227 pcm_setflags(device_t dev, uint32_t val)
228 {
229 	struct snddev_info *d = device_get_softc(dev);
230 
231 	d->flags = val;
232 }
233 
234 void *
pcm_getdevinfo(device_t dev)235 pcm_getdevinfo(device_t dev)
236 {
237 	struct snddev_info *d = device_get_softc(dev);
238 
239 	return d->devinfo;
240 }
241 
242 unsigned int
pcm_getbuffersize(device_t dev,unsigned int minbufsz,unsigned int deflt,unsigned int maxbufsz)243 pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz)
244 {
245 	int sz, x;
246 
247 	sz = 0;
248 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "buffersize", &sz) == 0) {
249 		x = sz;
250 		RANGE(sz, minbufsz, maxbufsz);
251 		if (x != sz)
252 			device_printf(dev, "'buffersize=%d' hint is out of range (%d-%d), using %d\n", x, minbufsz, maxbufsz, sz);
253 		x = minbufsz;
254 		while (x < sz)
255 			x <<= 1;
256 		if (x > sz)
257 			x >>= 1;
258 		if (x != sz) {
259 			device_printf(dev, "'buffersize=%d' hint is not a power of 2, using %d\n", sz, x);
260 			sz = x;
261 		}
262 	} else {
263 		sz = deflt;
264 	}
265 
266 	return sz;
267 }
268 
269 static int
sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)270 sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)
271 {
272 	struct snddev_info *d;
273 	int err, val;
274 
275 	d = oidp->oid_arg1;
276 	if (!PCM_REGISTERED(d))
277 		return (ENODEV);
278 
279 	PCM_LOCK(d);
280 	PCM_WAIT(d);
281 	val = (d->flags & SD_F_BITPERFECT) ? 1 : 0;
282 	PCM_ACQUIRE(d);
283 	PCM_UNLOCK(d);
284 
285 	err = sysctl_handle_int(oidp, &val, 0, req);
286 
287 	if (err == 0 && req->newptr != NULL) {
288 		if (!(val == 0 || val == 1)) {
289 			PCM_RELEASE_QUICK(d);
290 			return (EINVAL);
291 		}
292 
293 		PCM_LOCK(d);
294 
295 		d->flags &= ~SD_F_BITPERFECT;
296 		d->flags |= (val != 0) ? SD_F_BITPERFECT : 0;
297 
298 		PCM_RELEASE(d);
299 		PCM_UNLOCK(d);
300 	} else
301 		PCM_RELEASE_QUICK(d);
302 
303 	return (err);
304 }
305 
306 static int
sysctl_dev_pcm_mode(SYSCTL_HANDLER_ARGS)307 sysctl_dev_pcm_mode(SYSCTL_HANDLER_ARGS)
308 {
309 	struct snddev_info *d;
310 	int mode = 0;
311 
312 	d = oidp->oid_arg1;
313 	if (!PCM_REGISTERED(d))
314 		return (ENODEV);
315 
316 	PCM_LOCK(d);
317 	if (d->playcount > 0)
318 		mode |= PCM_MODE_PLAY;
319 	if (d->reccount > 0)
320 		mode |= PCM_MODE_REC;
321 	if (d->mixer_dev != NULL)
322 		mode |= PCM_MODE_MIXER;
323 	PCM_UNLOCK(d);
324 
325 	return (sysctl_handle_int(oidp, &mode, 0, req));
326 }
327 
328 /*
329  * Basic initialization so that drivers can use pcm_addchan() before
330  * pcm_register().
331  */
332 void
pcm_init(device_t dev,void * devinfo)333 pcm_init(device_t dev, void *devinfo)
334 {
335 	struct snddev_info *d;
336 	int i;
337 
338 	d = device_get_softc(dev);
339 	d->dev = dev;
340 	mtx_init(&d->lock, device_get_nameunit(dev), "sound cdev", MTX_DEF);
341 	cv_init(&d->cv, device_get_nameunit(dev));
342 
343 	i = 0;
344 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
345 	    "vpc", &i) != 0 || i != 0)
346 		d->flags |= SD_F_VPC;
347 
348 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
349 	    "bitperfect", &i) == 0 && i != 0)
350 		d->flags |= SD_F_BITPERFECT;
351 
352 	d->devinfo = devinfo;
353 	d->reccount = 0;
354 	d->playcount = 0;
355 	d->pvchancount = 0;
356 	d->rvchancount = 0;
357 	d->pvchanrate = 0;
358 	d->pvchanformat = 0;
359 	d->rvchanrate = 0;
360 	d->rvchanformat = 0;
361 	d->p_unr = new_unrhdr(0, INT_MAX, NULL);
362 	d->vp_unr = new_unrhdr(0, INT_MAX, NULL);
363 	d->r_unr = new_unrhdr(0, INT_MAX, NULL);
364 	d->vr_unr = new_unrhdr(0, INT_MAX, NULL);
365 
366 	CHN_INIT(d, channels.pcm);
367 	CHN_INIT(d, channels.pcm.busy);
368 	CHN_INIT(d, channels.pcm.opened);
369 	CHN_INIT(d, channels.pcm.primary);
370 }
371 
372 int
pcm_register(device_t dev,char * str)373 pcm_register(device_t dev, char *str)
374 {
375 	struct snddev_info *d = device_get_softc(dev);
376 
377 	/* should only be called once */
378 	if (d->flags & SD_F_REGISTERED)
379 		return (EINVAL);
380 
381 	if (d->playcount == 0 || d->reccount == 0)
382 		d->flags |= SD_F_SIMPLEX;
383 	if (d->playcount > 0)
384 		d->flags |= SD_F_PVCHANS;
385 	if (d->reccount > 0)
386 		d->flags |= SD_F_RVCHANS;
387 
388 	strlcpy(d->status, str, SND_STATUSLEN);
389 
390 	/* Done, we're ready.. */
391 	d->flags |= SD_F_REGISTERED;
392 
393 	/*
394 	 * Create all sysctls once SD_F_REGISTERED is set else
395 	 * tunable sysctls won't work:
396 	 */
397 	sysctl_ctx_init(&d->play_sysctl_ctx);
398 	d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx,
399 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "play",
400 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "playback channels node");
401 	sysctl_ctx_init(&d->rec_sysctl_ctx);
402 	d->rec_sysctl_tree = SYSCTL_ADD_NODE(&d->rec_sysctl_ctx,
403 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "rec",
404 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "recording channels node");
405 
406 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
407 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
408 	    "bitperfect", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, d,
409 	    sizeof(d), sysctl_dev_pcm_bitperfect, "I",
410 	    "bit-perfect playback/recording (0=disable, 1=enable)");
411 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
412 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
413 	    "mode", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, d, sizeof(d),
414 	    sysctl_dev_pcm_mode, "I",
415 	    "mode (1=mixer, 2=play, 4=rec. The values are OR'ed if more than "
416 	    "one mode is supported)");
417 	vchan_initsys(dev);
418 	if (d->flags & SD_F_EQ)
419 		feeder_eq_initsys(dev);
420 
421 	if (snd_unit_auto < 0)
422 		snd_unit_auto = (snd_unit < 0) ? 1 : 0;
423 	if (snd_unit < 0 || snd_unit_auto > 1)
424 		snd_unit = device_get_unit(dev);
425 	else if (snd_unit_auto == 1)
426 		snd_unit = pcm_best_unit(snd_unit);
427 
428 	sndstat_register(dev, SNDST_TYPE_PCM, d->status);
429 
430 	return (dsp_make_dev(dev));
431 }
432 
433 int
pcm_unregister(device_t dev)434 pcm_unregister(device_t dev)
435 {
436 	struct snddev_info *d;
437 
438 	d = device_get_softc(dev);
439 
440 	if (!PCM_REGISTERED(d)) {
441 		device_printf(dev, "unregister: device not configured\n");
442 		return (0);
443 	}
444 
445 	PCM_LOCK(d);
446 	PCM_WAIT(d);
447 
448 	d->flags &= ~SD_F_REGISTERED;
449 
450 	PCM_ACQUIRE(d);
451 	PCM_UNLOCK(d);
452 
453 	pcm_killchans(d);
454 
455 	PCM_RELEASE_QUICK(d);
456 
457 	if (d->play_sysctl_tree != NULL) {
458 		sysctl_ctx_free(&d->play_sysctl_ctx);
459 		d->play_sysctl_tree = NULL;
460 	}
461 	if (d->rec_sysctl_tree != NULL) {
462 		sysctl_ctx_free(&d->rec_sysctl_ctx);
463 		d->rec_sysctl_tree = NULL;
464 	}
465 
466 	sndstat_unregister(dev);
467 	mixer_uninit(dev);
468 	dsp_destroy_dev(dev);
469 
470 	cv_destroy(&d->cv);
471 	mtx_destroy(&d->lock);
472 
473 	if (snd_unit == device_get_unit(dev)) {
474 		snd_unit = pcm_best_unit(-1);
475 		if (snd_unit_auto == 0)
476 			snd_unit_auto = 1;
477 		if (snd_unit < 0)
478 			devctl_notify("SND", "CONN", "NODEV", NULL);
479 	}
480 
481 	return (0);
482 }
483 
484 /************************************************************************/
485 
486 /**
487  * @brief	Handle OSSv4 SNDCTL_SYSINFO ioctl.
488  *
489  * @param si	Pointer to oss_sysinfo struct where information about the
490  * 		sound subsystem will be written/copied.
491  *
492  * This routine returns information about the sound system, such as the
493  * current OSS version, number of audio, MIDI, and mixer drivers, etc.
494  * Also includes a bitmask showing which of the above types of devices
495  * are open (busy).
496  *
497  * @note
498  * Calling threads must not hold any snddev_info or pcm_channel locks.
499  *
500  * @author	Ryan Beasley <ryanb@FreeBSD.org>
501  */
502 void
sound_oss_sysinfo(oss_sysinfo * si)503 sound_oss_sysinfo(oss_sysinfo *si)
504 {
505 	static char si_product[] = "FreeBSD native OSS ABI";
506 	static char si_version[] = __XSTRING(__FreeBSD_version);
507 	static char si_license[] = "BSD";
508 	static int intnbits = sizeof(int) * 8;	/* Better suited as macro?
509 						   Must pester a C guru. */
510 
511 	struct snddev_info *d;
512 	struct pcm_channel *c;
513 	int j;
514 	size_t i;
515 
516 	strlcpy(si->product, si_product, sizeof(si->product));
517 	strlcpy(si->version, si_version, sizeof(si->version));
518 	si->versionnum = SOUND_VERSION;
519 	strlcpy(si->license, si_license, sizeof(si->license));
520 
521 	/*
522 	 * Iterate over PCM devices and their channels, gathering up data
523 	 * for the numaudioengines and openedaudio fields.
524 	 */
525 	si->numaudioengines = 0;
526 	bzero((void *)&si->openedaudio, sizeof(si->openedaudio));
527 
528 	j = 0;
529 
530 	bus_topo_lock();
531 	for (i = 0; pcm_devclass != NULL &&
532 	    i < devclass_get_maxunit(pcm_devclass); i++) {
533 		d = devclass_get_softc(pcm_devclass, i);
534 		if (!PCM_REGISTERED(d))
535 			continue;
536 
537 		/* XXX Need Giant magic entry ??? */
538 
539 		/* See note in function's docblock */
540 		PCM_UNLOCKASSERT(d);
541 		PCM_LOCK(d);
542 
543 		si->numaudioengines += PCM_CHANCOUNT(d);
544 
545 		CHN_FOREACH(c, d, channels.pcm) {
546 			CHN_UNLOCKASSERT(c);
547 			CHN_LOCK(c);
548 			if (c->flags & CHN_F_BUSY)
549 				si->openedaudio[j / intnbits] |=
550 				    (1 << (j % intnbits));
551 			CHN_UNLOCK(c);
552 			j++;
553 		}
554 
555 		PCM_UNLOCK(d);
556 	}
557 	bus_topo_unlock();
558 
559 	si->numsynths = 0;	/* OSSv4 docs:  this field is obsolete */
560 	/**
561 	 * @todo	Collect num{midis,timers}.
562 	 *
563 	 * Need access to sound/midi/midi.c::midistat_lock in order
564 	 * to safely touch midi_devices and get a head count of, well,
565 	 * MIDI devices.  midistat_lock is a global static (i.e., local to
566 	 * midi.c), but midi_devices is a regular global; should the mutex
567 	 * be publicized, or is there another way to get this information?
568 	 *
569 	 * NB:	MIDI/sequencer stuff is currently on hold.
570 	 */
571 	si->nummidis = 0;
572 	si->numtimers = 0;
573 	/*
574 	 * Set this to the maximum unit number so that applications will not
575 	 * break if they try to loop through all mixers and some of them are
576 	 * not available.
577 	 */
578 	bus_topo_lock();
579 	si->nummixers = devclass_get_maxunit(pcm_devclass);
580 	si->numcards = devclass_get_maxunit(pcm_devclass);
581 	si->numaudios = devclass_get_maxunit(pcm_devclass);
582 	bus_topo_unlock();
583 		/* OSSv4 docs:	Intended only for test apps; API doesn't
584 		   really have much of a concept of cards.  Shouldn't be
585 		   used by applications. */
586 
587 	/**
588 	 * @todo	Fill in "busy devices" fields.
589 	 *
590 	 *  si->openedmidi = " MIDI devices
591 	 */
592 	bzero((void *)&si->openedmidi, sizeof(si->openedmidi));
593 
594 	/*
595 	 * Si->filler is a reserved array, but according to docs each
596 	 * element should be set to -1.
597 	 */
598 	for (i = 0; i < nitems(si->filler); i++)
599 		si->filler[i] = -1;
600 }
601 
602 int
sound_oss_card_info(oss_card_info * si)603 sound_oss_card_info(oss_card_info *si)
604 {
605 	struct snddev_info *d;
606 	int i;
607 
608 	bus_topo_lock();
609 	for (i = 0; pcm_devclass != NULL &&
610 	    i < devclass_get_maxunit(pcm_devclass); i++) {
611 		d = devclass_get_softc(pcm_devclass, i);
612 		if (i != si->card)
613 			continue;
614 
615 		if (!PCM_REGISTERED(d)) {
616 			snprintf(si->shortname, sizeof(si->shortname),
617 			    "pcm%d (n/a)", i);
618 			strlcpy(si->longname, "Device unavailable",
619 			    sizeof(si->longname));
620 			si->hw_info[0] = '\0';
621 			si->intr_count = si->ack_count = 0;
622 		} else {
623 			PCM_UNLOCKASSERT(d);
624 			PCM_LOCK(d);
625 
626 			strlcpy(si->shortname, device_get_nameunit(d->dev),
627 			    sizeof(si->shortname));
628 			strlcpy(si->longname, device_get_desc(d->dev),
629 			    sizeof(si->longname));
630 			strlcpy(si->hw_info, d->status, sizeof(si->hw_info));
631 			si->intr_count = si->ack_count = 0;
632 
633 			PCM_UNLOCK(d);
634 		}
635 
636 		bus_topo_unlock();
637 		return (0);
638 	}
639 	bus_topo_unlock();
640 
641 	return (ENXIO);
642 }
643 
644 /************************************************************************/
645 
646 static void
sound_global_init(void)647 sound_global_init(void)
648 {
649 	if (snd_verbose < 0 || snd_verbose > 4)
650 		snd_verbose = 1;
651 
652 	if (snd_unit < 0)
653 		snd_unit = -1;
654 
655 	snd_vchans_enable = true;
656 
657 	if (chn_latency < CHN_LATENCY_MIN ||
658 	    chn_latency > CHN_LATENCY_MAX)
659 		chn_latency = CHN_LATENCY_DEFAULT;
660 
661 	if (chn_latency_profile < CHN_LATENCY_PROFILE_MIN ||
662 	    chn_latency_profile > CHN_LATENCY_PROFILE_MAX)
663 		chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT;
664 
665 	if (feeder_rate_min < FEEDRATE_MIN ||
666 		    feeder_rate_max < FEEDRATE_MIN ||
667 		    feeder_rate_min > FEEDRATE_MAX ||
668 		    feeder_rate_max > FEEDRATE_MAX ||
669 		    !(feeder_rate_min < feeder_rate_max)) {
670 		feeder_rate_min = FEEDRATE_RATEMIN;
671 		feeder_rate_max = FEEDRATE_RATEMAX;
672 	}
673 
674 	if (feeder_rate_round < FEEDRATE_ROUNDHZ_MIN ||
675 		    feeder_rate_round > FEEDRATE_ROUNDHZ_MAX)
676 		feeder_rate_round = FEEDRATE_ROUNDHZ;
677 
678 	if (bootverbose)
679 		printf("%s: snd_unit=%d snd_vchans_enable=%d "
680 		    "latency=%d "
681 		    "feeder_rate_min=%d feeder_rate_max=%d "
682 		    "feeder_rate_round=%d\n",
683 		    __func__, snd_unit, snd_vchans_enable,
684 		    chn_latency,
685 		    feeder_rate_min, feeder_rate_max,
686 		    feeder_rate_round);
687 }
688 
689 static int
sound_modevent(module_t mod,int type,void * data)690 sound_modevent(module_t mod, int type, void *data)
691 {
692 	int ret;
693 
694 	ret = 0;
695 	switch (type) {
696 	case MOD_LOAD:
697 		pcm_devclass = devclass_create("pcm");
698 		pcmsg_unrhdr = new_unrhdr(1, INT_MAX, NULL);
699 		sound_global_init();
700 		break;
701 	case MOD_UNLOAD:
702 		if (pcmsg_unrhdr != NULL) {
703 			delete_unrhdr(pcmsg_unrhdr);
704 			pcmsg_unrhdr = NULL;
705 		}
706 		break;
707 	case MOD_SHUTDOWN:
708 		break;
709 	default:
710 		ret = ENOTSUP;
711 	}
712 
713 	return ret;
714 }
715 
716 DEV_MODULE(sound, sound_modevent, NULL);
717 MODULE_VERSION(sound, SOUND_MODVER);
718