12a1ad637SFrançois Tigeot /*-
22a1ad637SFrançois Tigeot * Copyright (c) 2007 Ariff Abdullah <ariff@FreeBSD.org>
32a1ad637SFrançois Tigeot * All rights reserved.
42a1ad637SFrançois Tigeot *
52a1ad637SFrançois Tigeot * Redistribution and use in source and binary forms, with or without
62a1ad637SFrançois Tigeot * modification, are permitted provided that the following conditions
72a1ad637SFrançois Tigeot * are met:
82a1ad637SFrançois Tigeot * 1. Redistributions of source code must retain the above copyright
92a1ad637SFrançois Tigeot * notice, this list of conditions and the following disclaimer.
102a1ad637SFrançois Tigeot * 2. Redistributions in binary form must reproduce the above copyright
112a1ad637SFrançois Tigeot * notice, this list of conditions and the following disclaimer in the
122a1ad637SFrançois Tigeot * documentation and/or other materials provided with the distribution.
132a1ad637SFrançois Tigeot *
142a1ad637SFrançois Tigeot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
152a1ad637SFrançois Tigeot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
162a1ad637SFrançois Tigeot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172a1ad637SFrançois Tigeot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
182a1ad637SFrançois Tigeot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
192a1ad637SFrançois Tigeot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202a1ad637SFrançois Tigeot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
212a1ad637SFrançois Tigeot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
222a1ad637SFrançois Tigeot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
232a1ad637SFrançois Tigeot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
242a1ad637SFrançois Tigeot * SUCH DAMAGE.
252a1ad637SFrançois Tigeot *
262a1ad637SFrançois Tigeot * $FreeBSD: head/sys/dev/sound/unit.c 193640 2009-06-07 19:12:08Z ariff $
272a1ad637SFrançois Tigeot */
282a1ad637SFrançois Tigeot
292a1ad637SFrançois Tigeot #include <sys/param.h>
302a1ad637SFrançois Tigeot #include <sys/systm.h>
312a1ad637SFrançois Tigeot
322a1ad637SFrançois Tigeot #ifdef HAVE_KERNEL_OPTION_HEADERS
332a1ad637SFrançois Tigeot #include "opt_snd.h"
342a1ad637SFrançois Tigeot #endif
352a1ad637SFrançois Tigeot
362a1ad637SFrançois Tigeot #include <dev/sound/unit.h>
372a1ad637SFrançois Tigeot
38*5f097292SMatthew Dillon #if 0
39*5f097292SMatthew Dillon
402a1ad637SFrançois Tigeot /*
412a1ad637SFrançois Tigeot * Unit magic allocator for sound driver.
422a1ad637SFrançois Tigeot *
432a1ad637SFrançois Tigeot * 'u' = Unit of attached soundcards
442a1ad637SFrançois Tigeot * 'd' = Device type
452a1ad637SFrançois Tigeot * 'c' = Channel number
462a1ad637SFrançois Tigeot *
472a1ad637SFrançois Tigeot * eg: dsp0.p1 - u=0, d=p, c=1
482a1ad637SFrançois Tigeot * dsp1.vp0 - u=1, d=vp, c=0
492a1ad637SFrançois Tigeot * dsp0.10 - u=0, d=clone, c=allocated clone (see further explanation)
502a1ad637SFrançois Tigeot *
512a1ad637SFrançois Tigeot * Maximum unit of soundcards can be tuned through "hw.snd.maxunit", which
522a1ad637SFrançois Tigeot * is between SND_UNIT_UMIN (16) and SND_UNIT_UMAX (2048). By design,
532a1ad637SFrançois Tigeot * maximum allowable allocated channel is 256, with exception for clone
542a1ad637SFrançois Tigeot * devices which doesn't have any notion of channel numbering. The use of
552a1ad637SFrançois Tigeot * channel numbering in a clone device is simply to provide uniqueness among
562a1ad637SFrançois Tigeot * allocated clones. This also means that the maximum allowable clonable
572a1ad637SFrançois Tigeot * device is largely dependant and dynamically tuned depending on
582a1ad637SFrançois Tigeot * hw.snd.maxunit.
592a1ad637SFrançois Tigeot */
602a1ad637SFrançois Tigeot
612a1ad637SFrançois Tigeot /* Default width */
622a1ad637SFrançois Tigeot static int snd_u_shift = 9; /* 0 - 0x1ff : 512 distinct soundcards */
632a1ad637SFrançois Tigeot static int snd_d_shift = 5; /* 0 - 0x1f : 32 distinct device types */
642a1ad637SFrançois Tigeot static int snd_c_shift = 10; /* 0 - 0x3ff : 1024 distinct channels
652a1ad637SFrançois Tigeot (256 limit "by design",
662a1ad637SFrançois Tigeot except for clone devices) */
672a1ad637SFrançois Tigeot
682a1ad637SFrançois Tigeot static int snd_unit_initialized = 0;
692a1ad637SFrançois Tigeot
702a1ad637SFrançois Tigeot #ifdef SND_DIAGNOSTIC
712a1ad637SFrançois Tigeot #define SND_UNIT_ASSERT() do { \
722a1ad637SFrançois Tigeot if (snd_unit_initialized == 0) \
732a1ad637SFrançois Tigeot panic("%s(): Uninitialized sound unit!", __func__); \
742a1ad637SFrançois Tigeot } while (0)
752a1ad637SFrançois Tigeot #else
762a1ad637SFrançois Tigeot #define SND_UNIT_ASSERT() KASSERT(snd_unit_initialized != 0, \
772a1ad637SFrançois Tigeot ("%s(): Uninitialized sound unit!", \
782a1ad637SFrançois Tigeot __func__))
792a1ad637SFrançois Tigeot #endif
802a1ad637SFrançois Tigeot
812a1ad637SFrançois Tigeot #define MKMASK(x) ((1 << snd_##x##_shift) - 1)
822a1ad637SFrançois Tigeot
832a1ad637SFrançois Tigeot int
842a1ad637SFrançois Tigeot snd_max_u(void)
852a1ad637SFrançois Tigeot {
862a1ad637SFrançois Tigeot SND_UNIT_ASSERT();
872a1ad637SFrançois Tigeot
882a1ad637SFrançois Tigeot return (MKMASK(u));
892a1ad637SFrançois Tigeot }
902a1ad637SFrançois Tigeot
912a1ad637SFrançois Tigeot int
922a1ad637SFrançois Tigeot snd_max_d(void)
932a1ad637SFrançois Tigeot {
942a1ad637SFrançois Tigeot SND_UNIT_ASSERT();
952a1ad637SFrançois Tigeot
962a1ad637SFrançois Tigeot return (MKMASK(d));
972a1ad637SFrançois Tigeot }
982a1ad637SFrançois Tigeot
992a1ad637SFrançois Tigeot int
1002a1ad637SFrançois Tigeot snd_max_c(void)
1012a1ad637SFrançois Tigeot {
1022a1ad637SFrançois Tigeot SND_UNIT_ASSERT();
1032a1ad637SFrançois Tigeot
1042a1ad637SFrançois Tigeot return (MKMASK(c));
1052a1ad637SFrançois Tigeot }
1062a1ad637SFrançois Tigeot
1072a1ad637SFrançois Tigeot int
1082a1ad637SFrançois Tigeot snd_unit2u(int unit)
1092a1ad637SFrançois Tigeot {
1102a1ad637SFrançois Tigeot SND_UNIT_ASSERT();
1112a1ad637SFrançois Tigeot
1122a1ad637SFrançois Tigeot return ((unit >> (snd_c_shift + snd_d_shift)) & MKMASK(u));
1132a1ad637SFrançois Tigeot }
1142a1ad637SFrançois Tigeot
1152a1ad637SFrançois Tigeot int
1162a1ad637SFrançois Tigeot snd_unit2d(int unit)
1172a1ad637SFrançois Tigeot {
1182a1ad637SFrançois Tigeot SND_UNIT_ASSERT();
1192a1ad637SFrançois Tigeot
1202a1ad637SFrançois Tigeot return ((unit >> snd_c_shift) & MKMASK(d));
1212a1ad637SFrançois Tigeot }
1222a1ad637SFrançois Tigeot
1232a1ad637SFrançois Tigeot int
1242a1ad637SFrançois Tigeot snd_unit2c(int unit)
1252a1ad637SFrançois Tigeot {
1262a1ad637SFrançois Tigeot SND_UNIT_ASSERT();
1272a1ad637SFrançois Tigeot
1282a1ad637SFrançois Tigeot return (unit & MKMASK(c));
1292a1ad637SFrançois Tigeot }
1302a1ad637SFrançois Tigeot
1312a1ad637SFrançois Tigeot int
1322a1ad637SFrançois Tigeot snd_u2unit(int u)
1332a1ad637SFrançois Tigeot {
1342a1ad637SFrançois Tigeot SND_UNIT_ASSERT();
1352a1ad637SFrançois Tigeot
1362a1ad637SFrançois Tigeot return ((u & MKMASK(u)) << (snd_c_shift + snd_d_shift));
1372a1ad637SFrançois Tigeot }
1382a1ad637SFrançois Tigeot
1392a1ad637SFrançois Tigeot int
1402a1ad637SFrançois Tigeot snd_d2unit(int d)
1412a1ad637SFrançois Tigeot {
1422a1ad637SFrançois Tigeot SND_UNIT_ASSERT();
1432a1ad637SFrançois Tigeot
1442a1ad637SFrançois Tigeot return ((d & MKMASK(d)) << snd_c_shift);
1452a1ad637SFrançois Tigeot }
1462a1ad637SFrançois Tigeot
1472a1ad637SFrançois Tigeot int
1482a1ad637SFrançois Tigeot snd_c2unit(int c)
1492a1ad637SFrançois Tigeot {
1502a1ad637SFrançois Tigeot SND_UNIT_ASSERT();
1512a1ad637SFrançois Tigeot
1522a1ad637SFrançois Tigeot return (c & MKMASK(c));
1532a1ad637SFrançois Tigeot }
1542a1ad637SFrançois Tigeot
1552a1ad637SFrançois Tigeot int
1562a1ad637SFrançois Tigeot snd_mkunit(int u, int d, int c)
1572a1ad637SFrançois Tigeot {
1582a1ad637SFrançois Tigeot SND_UNIT_ASSERT();
1592a1ad637SFrançois Tigeot
1602a1ad637SFrançois Tigeot return ((c & MKMASK(c)) | ((d & MKMASK(d)) << snd_c_shift) |
1612a1ad637SFrançois Tigeot ((u & MKMASK(u)) << (snd_c_shift + snd_d_shift)));
1622a1ad637SFrançois Tigeot }
1632a1ad637SFrançois Tigeot
1642a1ad637SFrançois Tigeot /*
1652a1ad637SFrançois Tigeot * This *must* be called first before any of the functions above!!!
1662a1ad637SFrançois Tigeot */
1672a1ad637SFrançois Tigeot void
1682a1ad637SFrançois Tigeot snd_unit_init(void)
1692a1ad637SFrançois Tigeot {
1702a1ad637SFrançois Tigeot int i;
1712a1ad637SFrançois Tigeot
1722a1ad637SFrançois Tigeot if (snd_unit_initialized != 0)
1732a1ad637SFrançois Tigeot return;
1742a1ad637SFrançois Tigeot
1752a1ad637SFrançois Tigeot snd_unit_initialized = 1;
1762a1ad637SFrançois Tigeot
17767931cc4SFrançois Tigeot if (kgetenv_int("hw.snd.maxunit", &i) != 0) {
1782a1ad637SFrançois Tigeot if (i < SND_UNIT_UMIN)
1792a1ad637SFrançois Tigeot i = SND_UNIT_UMIN;
1802a1ad637SFrançois Tigeot else if (i > SND_UNIT_UMAX)
1812a1ad637SFrançois Tigeot i = SND_UNIT_UMAX;
1822a1ad637SFrançois Tigeot else
1832a1ad637SFrançois Tigeot i = roundup2(i, 2);
1842a1ad637SFrançois Tigeot
1852a1ad637SFrançois Tigeot for (snd_u_shift = 0; (i >> (snd_u_shift + 1)) != 0;
1862a1ad637SFrançois Tigeot snd_u_shift++)
1872a1ad637SFrançois Tigeot ;
1882a1ad637SFrançois Tigeot
1892a1ad637SFrançois Tigeot /*
1902a1ad637SFrançois Tigeot * Make room for channels/clones allocation unit
1912a1ad637SFrançois Tigeot * to fit within 24bit MAXMINOR limit.
1922a1ad637SFrançois Tigeot */
1932a1ad637SFrançois Tigeot snd_c_shift = 24 - snd_u_shift - snd_d_shift;
1942a1ad637SFrançois Tigeot }
1952a1ad637SFrançois Tigeot
1962a1ad637SFrançois Tigeot if (bootverbose != 0)
19767931cc4SFrançois Tigeot kprintf("%s() u=0x%08x [%d] d=0x%08x [%d] c=0x%08x [%d]\n",
1982a1ad637SFrançois Tigeot __func__, SND_U_MASK, snd_max_u() + 1,
1992a1ad637SFrançois Tigeot SND_D_MASK, snd_max_d() + 1, SND_C_MASK, snd_max_c() + 1);
2002a1ad637SFrançois Tigeot }
201*5f097292SMatthew Dillon #endif
202*5f097292SMatthew Dillon
203*5f097292SMatthew Dillon void
snd_unit_init(void)204*5f097292SMatthew Dillon snd_unit_init(void)
205*5f097292SMatthew Dillon {
206*5f097292SMatthew Dillon }
207