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 382a1ad637SFrançois Tigeot /* 392a1ad637SFrançois Tigeot * Unit magic allocator for sound driver. 402a1ad637SFrançois Tigeot * 412a1ad637SFrançois Tigeot * 'u' = Unit of attached soundcards 422a1ad637SFrançois Tigeot * 'd' = Device type 432a1ad637SFrançois Tigeot * 'c' = Channel number 442a1ad637SFrançois Tigeot * 452a1ad637SFrançois Tigeot * eg: dsp0.p1 - u=0, d=p, c=1 462a1ad637SFrançois Tigeot * dsp1.vp0 - u=1, d=vp, c=0 472a1ad637SFrançois Tigeot * dsp0.10 - u=0, d=clone, c=allocated clone (see further explanation) 482a1ad637SFrançois Tigeot * 492a1ad637SFrançois Tigeot * Maximum unit of soundcards can be tuned through "hw.snd.maxunit", which 502a1ad637SFrançois Tigeot * is between SND_UNIT_UMIN (16) and SND_UNIT_UMAX (2048). By design, 512a1ad637SFrançois Tigeot * maximum allowable allocated channel is 256, with exception for clone 522a1ad637SFrançois Tigeot * devices which doesn't have any notion of channel numbering. The use of 532a1ad637SFrançois Tigeot * channel numbering in a clone device is simply to provide uniqueness among 542a1ad637SFrançois Tigeot * allocated clones. This also means that the maximum allowable clonable 552a1ad637SFrançois Tigeot * device is largely dependant and dynamically tuned depending on 562a1ad637SFrançois Tigeot * hw.snd.maxunit. 572a1ad637SFrançois Tigeot */ 582a1ad637SFrançois Tigeot 592a1ad637SFrançois Tigeot /* Default width */ 602a1ad637SFrançois Tigeot static int snd_u_shift = 9; /* 0 - 0x1ff : 512 distinct soundcards */ 612a1ad637SFrançois Tigeot static int snd_d_shift = 5; /* 0 - 0x1f : 32 distinct device types */ 622a1ad637SFrançois Tigeot static int snd_c_shift = 10; /* 0 - 0x3ff : 1024 distinct channels 632a1ad637SFrançois Tigeot (256 limit "by design", 642a1ad637SFrançois Tigeot except for clone devices) */ 652a1ad637SFrançois Tigeot 662a1ad637SFrançois Tigeot static int snd_unit_initialized = 0; 672a1ad637SFrançois Tigeot 682a1ad637SFrançois Tigeot #ifdef SND_DIAGNOSTIC 692a1ad637SFrançois Tigeot #define SND_UNIT_ASSERT() do { \ 702a1ad637SFrançois Tigeot if (snd_unit_initialized == 0) \ 712a1ad637SFrançois Tigeot panic("%s(): Uninitialized sound unit!", __func__); \ 722a1ad637SFrançois Tigeot } while (0) 732a1ad637SFrançois Tigeot #else 742a1ad637SFrançois Tigeot #define SND_UNIT_ASSERT() KASSERT(snd_unit_initialized != 0, \ 752a1ad637SFrançois Tigeot ("%s(): Uninitialized sound unit!", \ 762a1ad637SFrançois Tigeot __func__)) 772a1ad637SFrançois Tigeot #endif 782a1ad637SFrançois Tigeot 792a1ad637SFrançois Tigeot #define MKMASK(x) ((1 << snd_##x##_shift) - 1) 802a1ad637SFrançois Tigeot 812a1ad637SFrançois Tigeot int 822a1ad637SFrançois Tigeot snd_max_u(void) 832a1ad637SFrançois Tigeot { 842a1ad637SFrançois Tigeot SND_UNIT_ASSERT(); 852a1ad637SFrançois Tigeot 862a1ad637SFrançois Tigeot return (MKMASK(u)); 872a1ad637SFrançois Tigeot } 882a1ad637SFrançois Tigeot 892a1ad637SFrançois Tigeot int 902a1ad637SFrançois Tigeot snd_max_d(void) 912a1ad637SFrançois Tigeot { 922a1ad637SFrançois Tigeot SND_UNIT_ASSERT(); 932a1ad637SFrançois Tigeot 942a1ad637SFrançois Tigeot return (MKMASK(d)); 952a1ad637SFrançois Tigeot } 962a1ad637SFrançois Tigeot 972a1ad637SFrançois Tigeot int 982a1ad637SFrançois Tigeot snd_max_c(void) 992a1ad637SFrançois Tigeot { 1002a1ad637SFrançois Tigeot SND_UNIT_ASSERT(); 1012a1ad637SFrançois Tigeot 1022a1ad637SFrançois Tigeot return (MKMASK(c)); 1032a1ad637SFrançois Tigeot } 1042a1ad637SFrançois Tigeot 1052a1ad637SFrançois Tigeot int 1062a1ad637SFrançois Tigeot snd_unit2u(int unit) 1072a1ad637SFrançois Tigeot { 1082a1ad637SFrançois Tigeot SND_UNIT_ASSERT(); 1092a1ad637SFrançois Tigeot 1102a1ad637SFrançois Tigeot return ((unit >> (snd_c_shift + snd_d_shift)) & MKMASK(u)); 1112a1ad637SFrançois Tigeot } 1122a1ad637SFrançois Tigeot 1132a1ad637SFrançois Tigeot int 1142a1ad637SFrançois Tigeot snd_unit2d(int unit) 1152a1ad637SFrançois Tigeot { 1162a1ad637SFrançois Tigeot SND_UNIT_ASSERT(); 1172a1ad637SFrançois Tigeot 1182a1ad637SFrançois Tigeot return ((unit >> snd_c_shift) & MKMASK(d)); 1192a1ad637SFrançois Tigeot } 1202a1ad637SFrançois Tigeot 1212a1ad637SFrançois Tigeot int 1222a1ad637SFrançois Tigeot snd_unit2c(int unit) 1232a1ad637SFrançois Tigeot { 1242a1ad637SFrançois Tigeot SND_UNIT_ASSERT(); 1252a1ad637SFrançois Tigeot 1262a1ad637SFrançois Tigeot return (unit & MKMASK(c)); 1272a1ad637SFrançois Tigeot } 1282a1ad637SFrançois Tigeot 1292a1ad637SFrançois Tigeot int 1302a1ad637SFrançois Tigeot snd_u2unit(int u) 1312a1ad637SFrançois Tigeot { 1322a1ad637SFrançois Tigeot SND_UNIT_ASSERT(); 1332a1ad637SFrançois Tigeot 1342a1ad637SFrançois Tigeot return ((u & MKMASK(u)) << (snd_c_shift + snd_d_shift)); 1352a1ad637SFrançois Tigeot } 1362a1ad637SFrançois Tigeot 1372a1ad637SFrançois Tigeot int 1382a1ad637SFrançois Tigeot snd_d2unit(int d) 1392a1ad637SFrançois Tigeot { 1402a1ad637SFrançois Tigeot SND_UNIT_ASSERT(); 1412a1ad637SFrançois Tigeot 1422a1ad637SFrançois Tigeot return ((d & MKMASK(d)) << snd_c_shift); 1432a1ad637SFrançois Tigeot } 1442a1ad637SFrançois Tigeot 1452a1ad637SFrançois Tigeot int 1462a1ad637SFrançois Tigeot snd_c2unit(int c) 1472a1ad637SFrançois Tigeot { 1482a1ad637SFrançois Tigeot SND_UNIT_ASSERT(); 1492a1ad637SFrançois Tigeot 1502a1ad637SFrançois Tigeot return (c & MKMASK(c)); 1512a1ad637SFrançois Tigeot } 1522a1ad637SFrançois Tigeot 1532a1ad637SFrançois Tigeot int 1542a1ad637SFrançois Tigeot snd_mkunit(int u, int d, int c) 1552a1ad637SFrançois Tigeot { 1562a1ad637SFrançois Tigeot SND_UNIT_ASSERT(); 1572a1ad637SFrançois Tigeot 1582a1ad637SFrançois Tigeot return ((c & MKMASK(c)) | ((d & MKMASK(d)) << snd_c_shift) | 1592a1ad637SFrançois Tigeot ((u & MKMASK(u)) << (snd_c_shift + snd_d_shift))); 1602a1ad637SFrançois Tigeot } 1612a1ad637SFrançois Tigeot 1622a1ad637SFrançois Tigeot /* 1632a1ad637SFrançois Tigeot * This *must* be called first before any of the functions above!!! 1642a1ad637SFrançois Tigeot */ 1652a1ad637SFrançois Tigeot void 1662a1ad637SFrançois Tigeot snd_unit_init(void) 1672a1ad637SFrançois Tigeot { 1682a1ad637SFrançois Tigeot int i; 1692a1ad637SFrançois Tigeot 1702a1ad637SFrançois Tigeot if (snd_unit_initialized != 0) 1712a1ad637SFrançois Tigeot return; 1722a1ad637SFrançois Tigeot 1732a1ad637SFrançois Tigeot snd_unit_initialized = 1; 1742a1ad637SFrançois Tigeot 175*67931cc4SFrançois Tigeot if (kgetenv_int("hw.snd.maxunit", &i) != 0) { 1762a1ad637SFrançois Tigeot if (i < SND_UNIT_UMIN) 1772a1ad637SFrançois Tigeot i = SND_UNIT_UMIN; 1782a1ad637SFrançois Tigeot else if (i > SND_UNIT_UMAX) 1792a1ad637SFrançois Tigeot i = SND_UNIT_UMAX; 1802a1ad637SFrançois Tigeot else 1812a1ad637SFrançois Tigeot i = roundup2(i, 2); 1822a1ad637SFrançois Tigeot 1832a1ad637SFrançois Tigeot for (snd_u_shift = 0; (i >> (snd_u_shift + 1)) != 0; 1842a1ad637SFrançois Tigeot snd_u_shift++) 1852a1ad637SFrançois Tigeot ; 1862a1ad637SFrançois Tigeot 1872a1ad637SFrançois Tigeot /* 1882a1ad637SFrançois Tigeot * Make room for channels/clones allocation unit 1892a1ad637SFrançois Tigeot * to fit within 24bit MAXMINOR limit. 1902a1ad637SFrançois Tigeot */ 1912a1ad637SFrançois Tigeot snd_c_shift = 24 - snd_u_shift - snd_d_shift; 1922a1ad637SFrançois Tigeot } 1932a1ad637SFrançois Tigeot 1942a1ad637SFrançois Tigeot if (bootverbose != 0) 195*67931cc4SFrançois Tigeot kprintf("%s() u=0x%08x [%d] d=0x%08x [%d] c=0x%08x [%d]\n", 1962a1ad637SFrançois Tigeot __func__, SND_U_MASK, snd_max_u() + 1, 1972a1ad637SFrançois Tigeot SND_D_MASK, snd_max_d() + 1, SND_C_MASK, snd_max_c() + 1); 1982a1ad637SFrançois Tigeot } 199