xref: /src/sys/dev/sound/pcm/sound.c (revision 428517a7712e44b58e0687fbee4037a8ebe5bf5a)
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 	struct snddev_info *d = device_get_softc(dev);
246 	int sz, x;
247 
248 	sz = 0;
249 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "buffersize", &sz) == 0) {
250 		x = sz;
251 		RANGE(sz, minbufsz, maxbufsz);
252 		if (x != sz)
253 			device_printf(dev, "'buffersize=%d' hint is out of range (%d-%d), using %d\n", x, minbufsz, maxbufsz, sz);
254 		x = minbufsz;
255 		while (x < sz)
256 			x <<= 1;
257 		if (x > sz)
258 			x >>= 1;
259 		if (x != sz) {
260 			device_printf(dev, "'buffersize=%d' hint is not a power of 2, using %d\n", sz, x);
261 			sz = x;
262 		}
263 	} else {
264 		sz = deflt;
265 	}
266 
267 	d->bufsz = sz;
268 
269 	return sz;
270 }
271 
272 static int
sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)273 sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)
274 {
275 	struct snddev_info *d;
276 	int err, val;
277 
278 	d = oidp->oid_arg1;
279 	if (!PCM_REGISTERED(d))
280 		return (ENODEV);
281 
282 	PCM_LOCK(d);
283 	PCM_WAIT(d);
284 	val = (d->flags & SD_F_BITPERFECT) ? 1 : 0;
285 	PCM_ACQUIRE(d);
286 	PCM_UNLOCK(d);
287 
288 	err = sysctl_handle_int(oidp, &val, 0, req);
289 
290 	if (err == 0 && req->newptr != NULL) {
291 		if (!(val == 0 || val == 1)) {
292 			PCM_RELEASE_QUICK(d);
293 			return (EINVAL);
294 		}
295 
296 		PCM_LOCK(d);
297 
298 		d->flags &= ~SD_F_BITPERFECT;
299 		d->flags |= (val != 0) ? SD_F_BITPERFECT : 0;
300 
301 		PCM_RELEASE(d);
302 		PCM_UNLOCK(d);
303 	} else
304 		PCM_RELEASE_QUICK(d);
305 
306 	return (err);
307 }
308 
309 static int
sysctl_dev_pcm_mode(SYSCTL_HANDLER_ARGS)310 sysctl_dev_pcm_mode(SYSCTL_HANDLER_ARGS)
311 {
312 	struct snddev_info *d;
313 	int mode = 0;
314 
315 	d = oidp->oid_arg1;
316 	if (!PCM_REGISTERED(d))
317 		return (ENODEV);
318 
319 	PCM_LOCK(d);
320 	if (d->playcount > 0)
321 		mode |= PCM_MODE_PLAY;
322 	if (d->reccount > 0)
323 		mode |= PCM_MODE_REC;
324 	if (d->mixer_dev != NULL)
325 		mode |= PCM_MODE_MIXER;
326 	PCM_UNLOCK(d);
327 
328 	return (sysctl_handle_int(oidp, &mode, 0, req));
329 }
330 
331 /*
332  * Basic initialization so that drivers can use pcm_addchan() before
333  * pcm_register().
334  */
335 void
pcm_init(device_t dev,void * devinfo)336 pcm_init(device_t dev, void *devinfo)
337 {
338 	struct snddev_info *d;
339 	int i;
340 
341 	d = device_get_softc(dev);
342 	d->dev = dev;
343 	mtx_init(&d->lock, device_get_nameunit(dev), "sound cdev", MTX_DEF);
344 	cv_init(&d->cv, device_get_nameunit(dev));
345 
346 	i = 0;
347 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
348 	    "vpc", &i) != 0 || i != 0)
349 		d->flags |= SD_F_VPC;
350 
351 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
352 	    "bitperfect", &i) == 0 && i != 0)
353 		d->flags |= SD_F_BITPERFECT;
354 
355 	d->devinfo = devinfo;
356 	d->reccount = 0;
357 	d->playcount = 0;
358 	d->pvchancount = 0;
359 	d->rvchancount = 0;
360 	d->pvchanrate = 0;
361 	d->pvchanformat = 0;
362 	d->rvchanrate = 0;
363 	d->rvchanformat = 0;
364 	d->p_unr = new_unrhdr(0, INT_MAX, NULL);
365 	d->vp_unr = new_unrhdr(0, INT_MAX, NULL);
366 	d->r_unr = new_unrhdr(0, INT_MAX, NULL);
367 	d->vr_unr = new_unrhdr(0, INT_MAX, NULL);
368 
369 	CHN_INIT(d, channels.pcm);
370 	CHN_INIT(d, channels.pcm.busy);
371 	CHN_INIT(d, channels.pcm.opened);
372 	CHN_INIT(d, channels.pcm.primary);
373 }
374 
375 int
pcm_register(device_t dev,char * str)376 pcm_register(device_t dev, char *str)
377 {
378 	struct snddev_info *d = device_get_softc(dev);
379 
380 	/* should only be called once */
381 	if (d->flags & SD_F_REGISTERED)
382 		return (EINVAL);
383 
384 	if (d->playcount == 0 || d->reccount == 0)
385 		d->flags |= SD_F_SIMPLEX;
386 	if (d->playcount > 0)
387 		d->flags |= SD_F_PVCHANS;
388 	if (d->reccount > 0)
389 		d->flags |= SD_F_RVCHANS;
390 
391 	strlcpy(d->status, str, SND_STATUSLEN);
392 
393 	/* Done, we're ready.. */
394 	d->flags |= SD_F_REGISTERED;
395 
396 	/*
397 	 * Create all sysctls once SD_F_REGISTERED is set else
398 	 * tunable sysctls won't work:
399 	 */
400 	sysctl_ctx_init(&d->play_sysctl_ctx);
401 	d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx,
402 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "play",
403 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "playback channels node");
404 	sysctl_ctx_init(&d->rec_sysctl_ctx);
405 	d->rec_sysctl_tree = SYSCTL_ADD_NODE(&d->rec_sysctl_ctx,
406 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "rec",
407 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "recording channels node");
408 
409 	/* XXX: a user should be able to set this with a control tool, the
410 	   sysadmin then needs min+max sysctls for this */
411 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
412 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
413             OID_AUTO, "buffersize", CTLFLAG_RD, &d->bufsz, 0,
414 	    "allocated buffer size");
415 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
416 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
417 	    "bitperfect", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, d,
418 	    sizeof(d), sysctl_dev_pcm_bitperfect, "I",
419 	    "bit-perfect playback/recording (0=disable, 1=enable)");
420 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
421 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
422 	    "mode", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, d, sizeof(d),
423 	    sysctl_dev_pcm_mode, "I",
424 	    "mode (1=mixer, 2=play, 4=rec. The values are OR'ed if more than "
425 	    "one mode is supported)");
426 	vchan_initsys(dev);
427 	if (d->flags & SD_F_EQ)
428 		feeder_eq_initsys(dev);
429 
430 	if (snd_unit_auto < 0)
431 		snd_unit_auto = (snd_unit < 0) ? 1 : 0;
432 	if (snd_unit < 0 || snd_unit_auto > 1)
433 		snd_unit = device_get_unit(dev);
434 	else if (snd_unit_auto == 1)
435 		snd_unit = pcm_best_unit(snd_unit);
436 
437 	sndstat_register(dev, SNDST_TYPE_PCM, d->status);
438 
439 	return (dsp_make_dev(dev));
440 }
441 
442 int
pcm_unregister(device_t dev)443 pcm_unregister(device_t dev)
444 {
445 	struct snddev_info *d;
446 
447 	d = device_get_softc(dev);
448 
449 	if (!PCM_REGISTERED(d)) {
450 		device_printf(dev, "unregister: device not configured\n");
451 		return (0);
452 	}
453 
454 	PCM_LOCK(d);
455 	PCM_WAIT(d);
456 
457 	d->flags &= ~SD_F_REGISTERED;
458 
459 	PCM_ACQUIRE(d);
460 	PCM_UNLOCK(d);
461 
462 	pcm_killchans(d);
463 
464 	PCM_RELEASE_QUICK(d);
465 
466 	if (d->play_sysctl_tree != NULL) {
467 		sysctl_ctx_free(&d->play_sysctl_ctx);
468 		d->play_sysctl_tree = NULL;
469 	}
470 	if (d->rec_sysctl_tree != NULL) {
471 		sysctl_ctx_free(&d->rec_sysctl_ctx);
472 		d->rec_sysctl_tree = NULL;
473 	}
474 
475 	sndstat_unregister(dev);
476 	mixer_uninit(dev);
477 	dsp_destroy_dev(dev);
478 
479 	cv_destroy(&d->cv);
480 	mtx_destroy(&d->lock);
481 
482 	if (snd_unit == device_get_unit(dev)) {
483 		snd_unit = pcm_best_unit(-1);
484 		if (snd_unit_auto == 0)
485 			snd_unit_auto = 1;
486 		if (snd_unit < 0)
487 			devctl_notify("SND", "CONN", "NODEV", NULL);
488 	}
489 
490 	return (0);
491 }
492 
493 /************************************************************************/
494 
495 /**
496  * @brief	Handle OSSv4 SNDCTL_SYSINFO ioctl.
497  *
498  * @param si	Pointer to oss_sysinfo struct where information about the
499  * 		sound subsystem will be written/copied.
500  *
501  * This routine returns information about the sound system, such as the
502  * current OSS version, number of audio, MIDI, and mixer drivers, etc.
503  * Also includes a bitmask showing which of the above types of devices
504  * are open (busy).
505  *
506  * @note
507  * Calling threads must not hold any snddev_info or pcm_channel locks.
508  *
509  * @author	Ryan Beasley <ryanb@FreeBSD.org>
510  */
511 void
sound_oss_sysinfo(oss_sysinfo * si)512 sound_oss_sysinfo(oss_sysinfo *si)
513 {
514 	static char si_product[] = "FreeBSD native OSS ABI";
515 	static char si_version[] = __XSTRING(__FreeBSD_version);
516 	static char si_license[] = "BSD";
517 	static int intnbits = sizeof(int) * 8;	/* Better suited as macro?
518 						   Must pester a C guru. */
519 
520 	struct snddev_info *d;
521 	struct pcm_channel *c;
522 	int j;
523 	size_t i;
524 
525 	strlcpy(si->product, si_product, sizeof(si->product));
526 	strlcpy(si->version, si_version, sizeof(si->version));
527 	si->versionnum = SOUND_VERSION;
528 	strlcpy(si->license, si_license, sizeof(si->license));
529 
530 	/*
531 	 * Iterate over PCM devices and their channels, gathering up data
532 	 * for the numaudioengines and openedaudio fields.
533 	 */
534 	si->numaudioengines = 0;
535 	bzero((void *)&si->openedaudio, sizeof(si->openedaudio));
536 
537 	j = 0;
538 
539 	bus_topo_lock();
540 	for (i = 0; pcm_devclass != NULL &&
541 	    i < devclass_get_maxunit(pcm_devclass); i++) {
542 		d = devclass_get_softc(pcm_devclass, i);
543 		if (!PCM_REGISTERED(d))
544 			continue;
545 
546 		/* XXX Need Giant magic entry ??? */
547 
548 		/* See note in function's docblock */
549 		PCM_UNLOCKASSERT(d);
550 		PCM_LOCK(d);
551 
552 		si->numaudioengines += PCM_CHANCOUNT(d);
553 
554 		CHN_FOREACH(c, d, channels.pcm) {
555 			CHN_UNLOCKASSERT(c);
556 			CHN_LOCK(c);
557 			if (c->flags & CHN_F_BUSY)
558 				si->openedaudio[j / intnbits] |=
559 				    (1 << (j % intnbits));
560 			CHN_UNLOCK(c);
561 			j++;
562 		}
563 
564 		PCM_UNLOCK(d);
565 	}
566 	bus_topo_unlock();
567 
568 	si->numsynths = 0;	/* OSSv4 docs:  this field is obsolete */
569 	/**
570 	 * @todo	Collect num{midis,timers}.
571 	 *
572 	 * Need access to sound/midi/midi.c::midistat_lock in order
573 	 * to safely touch midi_devices and get a head count of, well,
574 	 * MIDI devices.  midistat_lock is a global static (i.e., local to
575 	 * midi.c), but midi_devices is a regular global; should the mutex
576 	 * be publicized, or is there another way to get this information?
577 	 *
578 	 * NB:	MIDI/sequencer stuff is currently on hold.
579 	 */
580 	si->nummidis = 0;
581 	si->numtimers = 0;
582 	/*
583 	 * Set this to the maximum unit number so that applications will not
584 	 * break if they try to loop through all mixers and some of them are
585 	 * not available.
586 	 */
587 	bus_topo_lock();
588 	si->nummixers = devclass_get_maxunit(pcm_devclass);
589 	si->numcards = devclass_get_maxunit(pcm_devclass);
590 	si->numaudios = devclass_get_maxunit(pcm_devclass);
591 	bus_topo_unlock();
592 		/* OSSv4 docs:	Intended only for test apps; API doesn't
593 		   really have much of a concept of cards.  Shouldn't be
594 		   used by applications. */
595 
596 	/**
597 	 * @todo	Fill in "busy devices" fields.
598 	 *
599 	 *  si->openedmidi = " MIDI devices
600 	 */
601 	bzero((void *)&si->openedmidi, sizeof(si->openedmidi));
602 
603 	/*
604 	 * Si->filler is a reserved array, but according to docs each
605 	 * element should be set to -1.
606 	 */
607 	for (i = 0; i < nitems(si->filler); i++)
608 		si->filler[i] = -1;
609 }
610 
611 int
sound_oss_card_info(oss_card_info * si)612 sound_oss_card_info(oss_card_info *si)
613 {
614 	struct snddev_info *d;
615 	int i;
616 
617 	bus_topo_lock();
618 	for (i = 0; pcm_devclass != NULL &&
619 	    i < devclass_get_maxunit(pcm_devclass); i++) {
620 		d = devclass_get_softc(pcm_devclass, i);
621 		if (i != si->card)
622 			continue;
623 
624 		if (!PCM_REGISTERED(d)) {
625 			snprintf(si->shortname, sizeof(si->shortname),
626 			    "pcm%d (n/a)", i);
627 			strlcpy(si->longname, "Device unavailable",
628 			    sizeof(si->longname));
629 			si->hw_info[0] = '\0';
630 			si->intr_count = si->ack_count = 0;
631 		} else {
632 			PCM_UNLOCKASSERT(d);
633 			PCM_LOCK(d);
634 
635 			strlcpy(si->shortname, device_get_nameunit(d->dev),
636 			    sizeof(si->shortname));
637 			strlcpy(si->longname, device_get_desc(d->dev),
638 			    sizeof(si->longname));
639 			strlcpy(si->hw_info, d->status, sizeof(si->hw_info));
640 			si->intr_count = si->ack_count = 0;
641 
642 			PCM_UNLOCK(d);
643 		}
644 
645 		bus_topo_unlock();
646 		return (0);
647 	}
648 	bus_topo_unlock();
649 
650 	return (ENXIO);
651 }
652 
653 /************************************************************************/
654 
655 static void
sound_global_init(void)656 sound_global_init(void)
657 {
658 	if (snd_verbose < 0 || snd_verbose > 4)
659 		snd_verbose = 1;
660 
661 	if (snd_unit < 0)
662 		snd_unit = -1;
663 
664 	snd_vchans_enable = true;
665 
666 	if (chn_latency < CHN_LATENCY_MIN ||
667 	    chn_latency > CHN_LATENCY_MAX)
668 		chn_latency = CHN_LATENCY_DEFAULT;
669 
670 	if (chn_latency_profile < CHN_LATENCY_PROFILE_MIN ||
671 	    chn_latency_profile > CHN_LATENCY_PROFILE_MAX)
672 		chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT;
673 
674 	if (feeder_rate_min < FEEDRATE_MIN ||
675 		    feeder_rate_max < FEEDRATE_MIN ||
676 		    feeder_rate_min > FEEDRATE_MAX ||
677 		    feeder_rate_max > FEEDRATE_MAX ||
678 		    !(feeder_rate_min < feeder_rate_max)) {
679 		feeder_rate_min = FEEDRATE_RATEMIN;
680 		feeder_rate_max = FEEDRATE_RATEMAX;
681 	}
682 
683 	if (feeder_rate_round < FEEDRATE_ROUNDHZ_MIN ||
684 		    feeder_rate_round > FEEDRATE_ROUNDHZ_MAX)
685 		feeder_rate_round = FEEDRATE_ROUNDHZ;
686 
687 	if (bootverbose)
688 		printf("%s: snd_unit=%d snd_vchans_enable=%d "
689 		    "latency=%d "
690 		    "feeder_rate_min=%d feeder_rate_max=%d "
691 		    "feeder_rate_round=%d\n",
692 		    __func__, snd_unit, snd_vchans_enable,
693 		    chn_latency,
694 		    feeder_rate_min, feeder_rate_max,
695 		    feeder_rate_round);
696 }
697 
698 static int
sound_modevent(module_t mod,int type,void * data)699 sound_modevent(module_t mod, int type, void *data)
700 {
701 	int ret;
702 
703 	ret = 0;
704 	switch (type) {
705 	case MOD_LOAD:
706 		pcm_devclass = devclass_create("pcm");
707 		pcmsg_unrhdr = new_unrhdr(1, INT_MAX, NULL);
708 		sound_global_init();
709 		break;
710 	case MOD_UNLOAD:
711 		if (pcmsg_unrhdr != NULL) {
712 			delete_unrhdr(pcmsg_unrhdr);
713 			pcmsg_unrhdr = NULL;
714 		}
715 		break;
716 	case MOD_SHUTDOWN:
717 		break;
718 	default:
719 		ret = ENOTSUP;
720 	}
721 
722 	return ret;
723 }
724 
725 DEV_MODULE(sound, sound_modevent, NULL);
726 MODULE_VERSION(sound, SOUND_MODVER);
727