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