Merge branch 'vendor/GCC50'
[dragonfly.git] / sys / dev / sound / unit.c
1 /*-
2  * Copyright (c) 2007 Ariff Abdullah <ariff@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: head/sys/dev/sound/unit.c 193640 2009-06-07 19:12:08Z ariff $
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31
32 #ifdef HAVE_KERNEL_OPTION_HEADERS
33 #include "opt_snd.h"
34 #endif
35
36 #include <dev/sound/unit.h>
37
38 #if 0
39
40 /*
41  * Unit magic allocator for sound driver.
42  *
43  * 'u' = Unit of attached soundcards
44  * 'd' = Device type
45  * 'c' = Channel number
46  *
47  * eg: dsp0.p1  - u=0, d=p, c=1
48  *     dsp1.vp0 - u=1, d=vp, c=0
49  *     dsp0.10  - u=0, d=clone, c=allocated clone (see further explanation)
50  *
51  * Maximum unit of soundcards can be tuned through "hw.snd.maxunit", which
52  * is between SND_UNIT_UMIN (16) and SND_UNIT_UMAX (2048). By design,
53  * maximum allowable allocated channel is 256, with exception for clone
54  * devices which doesn't have any notion of channel numbering. The use of
55  * channel numbering in a clone device is simply to provide uniqueness among
56  * allocated clones. This also means that the maximum allowable clonable
57  * device is largely dependant and dynamically tuned depending on
58  * hw.snd.maxunit.
59  */
60
61 /* Default width */
62 static int snd_u_shift = 9;     /* 0 - 0x1ff :  512 distinct soundcards   */
63 static int snd_d_shift = 5;     /* 0 - 0x1f  :   32 distinct device types */
64 static int snd_c_shift = 10;    /* 0 - 0x3ff : 1024 distinct channels
65                                                (256 limit "by design",
66                                                except for clone devices)  */
67
68 static int snd_unit_initialized = 0;
69
70 #ifdef SND_DIAGNOSTIC
71 #define SND_UNIT_ASSERT()       do {                                    \
72         if (snd_unit_initialized == 0)                                  \
73                 panic("%s(): Uninitialized sound unit!", __func__);     \
74 } while (0)
75 #else
76 #define SND_UNIT_ASSERT()       KASSERT(snd_unit_initialized != 0,      \
77                                 ("%s(): Uninitialized sound unit!",     \
78                                 __func__))
79 #endif
80
81 #define MKMASK(x)       ((1 << snd_##x##_shift) - 1)
82
83 int
84 snd_max_u(void)
85 {
86         SND_UNIT_ASSERT();
87
88         return (MKMASK(u));
89 }
90
91 int
92 snd_max_d(void)
93 {
94         SND_UNIT_ASSERT();
95
96         return (MKMASK(d));
97 }
98
99 int
100 snd_max_c(void)
101 {
102         SND_UNIT_ASSERT();
103
104         return (MKMASK(c));
105 }
106
107 int
108 snd_unit2u(int unit)
109 {
110         SND_UNIT_ASSERT();
111
112         return ((unit >> (snd_c_shift + snd_d_shift)) & MKMASK(u));
113 }
114
115 int
116 snd_unit2d(int unit)
117 {
118         SND_UNIT_ASSERT();
119
120         return ((unit >> snd_c_shift) & MKMASK(d));
121 }
122
123 int
124 snd_unit2c(int unit)
125 {
126         SND_UNIT_ASSERT();
127
128         return (unit & MKMASK(c));
129 }
130
131 int
132 snd_u2unit(int u)
133 {
134         SND_UNIT_ASSERT();
135
136         return ((u & MKMASK(u)) << (snd_c_shift + snd_d_shift));
137 }
138
139 int
140 snd_d2unit(int d)
141 {
142         SND_UNIT_ASSERT();
143
144         return ((d & MKMASK(d)) << snd_c_shift);
145 }
146
147 int
148 snd_c2unit(int c)
149 {
150         SND_UNIT_ASSERT();
151
152         return (c & MKMASK(c));
153 }
154
155 int
156 snd_mkunit(int u, int d, int c)
157 {
158         SND_UNIT_ASSERT();
159
160         return ((c & MKMASK(c)) | ((d & MKMASK(d)) << snd_c_shift) |
161             ((u & MKMASK(u)) << (snd_c_shift + snd_d_shift)));
162 }
163
164 /*
165  * This *must* be called first before any of the functions above!!!
166  */
167 void
168 snd_unit_init(void)
169 {
170         int i;
171
172         if (snd_unit_initialized != 0)
173                 return;
174
175         snd_unit_initialized = 1;
176
177         if (kgetenv_int("hw.snd.maxunit", &i) != 0) {
178                 if (i < SND_UNIT_UMIN)
179                         i = SND_UNIT_UMIN;
180                 else if (i > SND_UNIT_UMAX)
181                         i = SND_UNIT_UMAX;
182                 else
183                         i = roundup2(i, 2);
184
185                 for (snd_u_shift = 0; (i >> (snd_u_shift + 1)) != 0;
186                     snd_u_shift++)
187                         ;
188
189                 /*
190                  * Make room for channels/clones allocation unit
191                  * to fit within 24bit MAXMINOR limit.
192                  */
193                 snd_c_shift = 24 - snd_u_shift - snd_d_shift;
194         }
195
196         if (bootverbose != 0)
197                 kprintf("%s() u=0x%08x [%d] d=0x%08x [%d] c=0x%08x [%d]\n",
198                     __func__, SND_U_MASK, snd_max_u() + 1,
199                     SND_D_MASK, snd_max_d() + 1, SND_C_MASK, snd_max_c() + 1);
200 }
201 #endif
202
203 void
204 snd_unit_init(void)
205 {
206 }