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