xref: /freebsd/sys/dev/sound/pcm/sound.c (revision 2aa16666e2fa5c98be8d330cd0c32c9dc3801ba7)
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 	int error, unit;
85 
86 	unit = snd_unit;
87 	error = sysctl_handle_int(oidp, &unit, 0, req);
88 	if (error == 0 && req->newptr != NULL) {
89 		bus_topo_lock();
90 		d = devclass_get_softc(pcm_devclass, unit);
91 		if (!PCM_REGISTERED(d) || CHN_EMPTY(d, channels.pcm)) {
92 			bus_topo_unlock();
93 			return EINVAL;
94 		}
95 		snd_unit = unit;
96 		snd_unit_auto = 0;
97 		bus_topo_unlock();
98 	}
99 	return (error);
100 }
101 /* XXX: do we need a way to let the user change the default unit? */
102 SYSCTL_PROC(_hw_snd, OID_AUTO, default_unit,
103     CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_ANYBODY | CTLFLAG_MPSAFE, 0,
104     sizeof(int), sysctl_hw_snd_default_unit, "I",
105     "default sound device");
106 
107 int
pcm_addchan(device_t dev,int dir,kobj_class_t cls,void * devinfo)108 pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo)
109 {
110 	struct snddev_info *d = device_get_softc(dev);
111 	struct pcm_channel *ch;
112 	int err = 0;
113 
114 	PCM_LOCK(d);
115 	PCM_WAIT(d);
116 	PCM_ACQUIRE(d);
117 	ch = chn_init(d, NULL, cls, dir, devinfo);
118 	if (!ch) {
119 		device_printf(d->dev, "chn_init(%s, %d, %p) failed\n",
120 		    cls->name, dir, devinfo);
121 		err = ENODEV;
122 	}
123 	PCM_RELEASE(d);
124 	PCM_UNLOCK(d);
125 
126 	return (err);
127 }
128 
129 static void
pcm_killchans(struct snddev_info * d)130 pcm_killchans(struct snddev_info *d)
131 {
132 	struct pcm_channel *ch;
133 	bool again;
134 
135 	PCM_BUSYASSERT(d);
136 	KASSERT(!PCM_REGISTERED(d), ("%s(): still registered\n", __func__));
137 
138 	for (;;) {
139 		again = false;
140 		/* Make sure all channels are stopped. */
141 		CHN_FOREACH(ch, d, channels.pcm) {
142 			CHN_LOCK(ch);
143 			if (ch->inprog == 0 && ch->sleeping == 0 &&
144 			    CHN_STOPPED(ch)) {
145 				CHN_UNLOCK(ch);
146 				continue;
147 			}
148 			chn_shutdown(ch);
149 			if (ch->direction == PCMDIR_PLAY)
150 				chn_flush(ch);
151 			else
152 				chn_abort(ch);
153 			CHN_UNLOCK(ch);
154 			again = true;
155 		}
156 		/*
157 		 * Some channels are still active. Sleep for a bit and try
158 		 * again.
159 		 */
160 		if (again)
161 			pause_sbt("pcmkillchans", mstosbt(5), 0, 0);
162 		else
163 			break;
164 	}
165 
166 	/* All channels are finally dead. */
167 	while (!CHN_EMPTY(d, channels.pcm)) {
168 		ch = CHN_FIRST(d, channels.pcm);
169 		chn_kill(ch);
170 	}
171 
172 	if (d->p_unr != NULL)
173 		delete_unrhdr(d->p_unr);
174 	if (d->vp_unr != NULL)
175 		delete_unrhdr(d->vp_unr);
176 	if (d->r_unr != NULL)
177 		delete_unrhdr(d->r_unr);
178 	if (d->vr_unr != NULL)
179 		delete_unrhdr(d->vr_unr);
180 }
181 
182 static int
pcm_best_unit(int old)183 pcm_best_unit(int old)
184 {
185 	struct snddev_info *d;
186 	int i, best, bestprio, prio;
187 
188 	best = -1;
189 	bestprio = -100;
190 	bus_topo_lock();
191 	for (i = 0; pcm_devclass != NULL &&
192 	    i < devclass_get_maxunit(pcm_devclass); i++) {
193 		d = devclass_get_softc(pcm_devclass, i);
194 		if (!PCM_REGISTERED(d))
195 			continue;
196 		prio = 0;
197 		if (d->playcount == 0)
198 			prio -= 10;
199 		if (d->reccount == 0)
200 			prio -= 2;
201 		if (prio > bestprio || (prio == bestprio && i == old)) {
202 			best = i;
203 			bestprio = prio;
204 		}
205 	}
206 	bus_topo_unlock();
207 
208 	return (best);
209 }
210 
211 uint32_t
pcm_getflags(device_t dev)212 pcm_getflags(device_t dev)
213 {
214 	struct snddev_info *d = device_get_softc(dev);
215 
216 	return d->flags;
217 }
218 
219 void
pcm_setflags(device_t dev,uint32_t val)220 pcm_setflags(device_t dev, uint32_t val)
221 {
222 	struct snddev_info *d = device_get_softc(dev);
223 
224 	d->flags = val;
225 }
226 
227 void *
pcm_getdevinfo(device_t dev)228 pcm_getdevinfo(device_t dev)
229 {
230 	struct snddev_info *d = device_get_softc(dev);
231 
232 	return d->devinfo;
233 }
234 
235 unsigned int
pcm_getbuffersize(device_t dev,unsigned int minbufsz,unsigned int deflt,unsigned int maxbufsz)236 pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz)
237 {
238 	struct snddev_info *d = device_get_softc(dev);
239 	int sz, x;
240 
241 	sz = 0;
242 	if (resource_int_value(device_get_name(dev), device_get_unit(dev), "buffersize", &sz) == 0) {
243 		x = sz;
244 		RANGE(sz, minbufsz, maxbufsz);
245 		if (x != sz)
246 			device_printf(dev, "'buffersize=%d' hint is out of range (%d-%d), using %d\n", x, minbufsz, maxbufsz, sz);
247 		x = minbufsz;
248 		while (x < sz)
249 			x <<= 1;
250 		if (x > sz)
251 			x >>= 1;
252 		if (x != sz) {
253 			device_printf(dev, "'buffersize=%d' hint is not a power of 2, using %d\n", sz, x);
254 			sz = x;
255 		}
256 	} else {
257 		sz = deflt;
258 	}
259 
260 	d->bufsz = sz;
261 
262 	return sz;
263 }
264 
265 static int
sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)266 sysctl_dev_pcm_bitperfect(SYSCTL_HANDLER_ARGS)
267 {
268 	struct snddev_info *d;
269 	int err, val;
270 
271 	d = oidp->oid_arg1;
272 	if (!PCM_REGISTERED(d))
273 		return (ENODEV);
274 
275 	PCM_LOCK(d);
276 	PCM_WAIT(d);
277 	val = (d->flags & SD_F_BITPERFECT) ? 1 : 0;
278 	PCM_ACQUIRE(d);
279 	PCM_UNLOCK(d);
280 
281 	err = sysctl_handle_int(oidp, &val, 0, req);
282 
283 	if (err == 0 && req->newptr != NULL) {
284 		if (!(val == 0 || val == 1)) {
285 			PCM_RELEASE_QUICK(d);
286 			return (EINVAL);
287 		}
288 
289 		PCM_LOCK(d);
290 
291 		d->flags &= ~SD_F_BITPERFECT;
292 		d->flags |= (val != 0) ? SD_F_BITPERFECT : 0;
293 
294 		PCM_RELEASE(d);
295 		PCM_UNLOCK(d);
296 	} else
297 		PCM_RELEASE_QUICK(d);
298 
299 	return (err);
300 }
301 
302 static int
sysctl_dev_pcm_mode(SYSCTL_HANDLER_ARGS)303 sysctl_dev_pcm_mode(SYSCTL_HANDLER_ARGS)
304 {
305 	struct snddev_info *d;
306 	int mode = 0;
307 
308 	d = oidp->oid_arg1;
309 	if (!PCM_REGISTERED(d))
310 		return (ENODEV);
311 
312 	PCM_LOCK(d);
313 	if (d->playcount > 0)
314 		mode |= PCM_MODE_PLAY;
315 	if (d->reccount > 0)
316 		mode |= PCM_MODE_REC;
317 	if (d->mixer_dev != NULL)
318 		mode |= PCM_MODE_MIXER;
319 	PCM_UNLOCK(d);
320 
321 	return (sysctl_handle_int(oidp, &mode, 0, req));
322 }
323 
324 /*
325  * Basic initialization so that drivers can use pcm_addchan() before
326  * pcm_register().
327  */
328 void
pcm_init(device_t dev,void * devinfo)329 pcm_init(device_t dev, void *devinfo)
330 {
331 	struct snddev_info *d;
332 	int i;
333 
334 	d = device_get_softc(dev);
335 	d->dev = dev;
336 	mtx_init(&d->lock, device_get_nameunit(dev), "sound cdev", MTX_DEF);
337 	cv_init(&d->cv, device_get_nameunit(dev));
338 
339 	i = 0;
340 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
341 	    "vpc", &i) != 0 || i != 0)
342 		d->flags |= SD_F_VPC;
343 
344 	if (resource_int_value(device_get_name(dev), device_get_unit(dev),
345 	    "bitperfect", &i) == 0 && i != 0)
346 		d->flags |= SD_F_BITPERFECT;
347 
348 	d->devinfo = devinfo;
349 	d->reccount = 0;
350 	d->playcount = 0;
351 	d->pvchancount = 0;
352 	d->rvchancount = 0;
353 	d->pvchanrate = 0;
354 	d->pvchanformat = 0;
355 	d->rvchanrate = 0;
356 	d->rvchanformat = 0;
357 	d->p_unr = new_unrhdr(0, INT_MAX, NULL);
358 	d->vp_unr = new_unrhdr(0, INT_MAX, NULL);
359 	d->r_unr = new_unrhdr(0, INT_MAX, NULL);
360 	d->vr_unr = new_unrhdr(0, INT_MAX, NULL);
361 
362 	CHN_INIT(d, channels.pcm);
363 	CHN_INIT(d, channels.pcm.busy);
364 	CHN_INIT(d, channels.pcm.opened);
365 	CHN_INIT(d, channels.pcm.primary);
366 }
367 
368 int
pcm_register(device_t dev,char * str)369 pcm_register(device_t dev, char *str)
370 {
371 	struct snddev_info *d = device_get_softc(dev);
372 
373 	/* should only be called once */
374 	if (d->flags & SD_F_REGISTERED)
375 		return (EINVAL);
376 
377 	if (d->playcount == 0 || d->reccount == 0)
378 		d->flags |= SD_F_SIMPLEX;
379 	if (d->playcount > 0)
380 		d->flags |= SD_F_PVCHANS;
381 	if (d->reccount > 0)
382 		d->flags |= SD_F_RVCHANS;
383 
384 	strlcpy(d->status, str, SND_STATUSLEN);
385 
386 	/* Done, we're ready.. */
387 	d->flags |= SD_F_REGISTERED;
388 
389 	/*
390 	 * Create all sysctls once SD_F_REGISTERED is set else
391 	 * tunable sysctls won't work:
392 	 */
393 	sysctl_ctx_init(&d->play_sysctl_ctx);
394 	d->play_sysctl_tree = SYSCTL_ADD_NODE(&d->play_sysctl_ctx,
395 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "play",
396 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "playback channels node");
397 	sysctl_ctx_init(&d->rec_sysctl_ctx);
398 	d->rec_sysctl_tree = SYSCTL_ADD_NODE(&d->rec_sysctl_ctx,
399 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "rec",
400 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "recording channels node");
401 
402 	/* XXX: a user should be able to set this with a control tool, the
403 	   sysadmin then needs min+max sysctls for this */
404 	SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
405 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
406             OID_AUTO, "buffersize", CTLFLAG_RD, &d->bufsz, 0,
407 	    "allocated buffer size");
408 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
409 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
410 	    "bitperfect", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, d,
411 	    sizeof(d), sysctl_dev_pcm_bitperfect, "I",
412 	    "bit-perfect playback/recording (0=disable, 1=enable)");
413 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
414 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
415 	    "mode", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, d, sizeof(d),
416 	    sysctl_dev_pcm_mode, "I",
417 	    "mode (1=mixer, 2=play, 4=rec. The values are OR'ed if more than "
418 	    "one mode is supported)");
419 	vchan_initsys(dev);
420 	if (d->flags & SD_F_EQ)
421 		feeder_eq_initsys(dev);
422 
423 	if (snd_unit_auto < 0)
424 		snd_unit_auto = (snd_unit < 0) ? 1 : 0;
425 	if (snd_unit < 0 || snd_unit_auto > 1)
426 		snd_unit = device_get_unit(dev);
427 	else if (snd_unit_auto == 1)
428 		snd_unit = pcm_best_unit(snd_unit);
429 
430 	sndstat_register(dev, SNDST_TYPE_PCM, d->status);
431 
432 	return (dsp_make_dev(dev));
433 }
434 
435 int
pcm_unregister(device_t dev)436 pcm_unregister(device_t dev)
437 {
438 	struct snddev_info *d;
439 
440 	d = device_get_softc(dev);
441 
442 	if (!PCM_REGISTERED(d)) {
443 		device_printf(dev, "unregister: device not configured\n");
444 		return (0);
445 	}
446 
447 	PCM_LOCK(d);
448 	PCM_WAIT(d);
449 
450 	d->flags &= ~SD_F_REGISTERED;
451 
452 	PCM_ACQUIRE(d);
453 	PCM_UNLOCK(d);
454 
455 	pcm_killchans(d);
456 
457 	PCM_RELEASE_QUICK(d);
458 
459 	if (d->play_sysctl_tree != NULL) {
460 		sysctl_ctx_free(&d->play_sysctl_ctx);
461 		d->play_sysctl_tree = NULL;
462 	}
463 	if (d->rec_sysctl_tree != NULL) {
464 		sysctl_ctx_free(&d->rec_sysctl_ctx);
465 		d->rec_sysctl_tree = NULL;
466 	}
467 
468 	sndstat_unregister(dev);
469 	mixer_uninit(dev);
470 	dsp_destroy_dev(dev);
471 
472 	cv_destroy(&d->cv);
473 	mtx_destroy(&d->lock);
474 
475 	if (snd_unit == device_get_unit(dev)) {
476 		snd_unit = pcm_best_unit(-1);
477 		if (snd_unit_auto == 0)
478 			snd_unit_auto = 1;
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