blob: e3bebd98e1cee11712384c543d970abdaa8ea33f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __SOUND_PCM_PARAMS_H
2#define __SOUND_PCM_PARAMS_H
3
4/*
5 * PCM params helpers
6 * Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
Takashi Iwai877211f2005-11-17 13:59:38 +010025extern int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
26 snd_pcm_hw_param_t var, const struct snd_mask *val);
27extern unsigned int snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 snd_pcm_hw_param_t var, int *dir);
Takashi Iwai877211f2005-11-17 13:59:38 +010029extern unsigned int snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 snd_pcm_hw_param_t var, int *dir);
Takashi Iwai877211f2005-11-17 13:59:38 +010031extern int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 snd_pcm_hw_param_t var, unsigned int val, int dir);
Takashi Iwai877211f2005-11-17 13:59:38 +010033extern int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 snd_pcm_hw_param_t var);
Takashi Iwai877211f2005-11-17 13:59:38 +010035extern int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 snd_pcm_hw_param_t var, unsigned int val, int dir);
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define SNDRV_MASK_BITS 64 /* we use so far 64bits only */
39#define SNDRV_MASK_SIZE (SNDRV_MASK_BITS / 32)
40#define MASK_OFS(i) ((i) >> 5)
41#define MASK_BIT(i) (1U << ((i) & 31))
42
Takashi Iwai9bb22e22006-04-28 15:13:39 +020043static inline unsigned int ld2(u_int32_t v)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 unsigned r = 0;
46
47 if (v >= 0x10000) {
48 v >>= 16;
49 r += 16;
50 }
51 if (v >= 0x100) {
52 v >>= 8;
53 r += 8;
54 }
55 if (v >= 0x10) {
56 v >>= 4;
57 r += 4;
58 }
59 if (v >= 4) {
60 v >>= 2;
61 r += 2;
62 }
63 if (v >= 2)
64 r++;
65 return r;
66}
67
Takashi Iwai9bb22e22006-04-28 15:13:39 +020068static inline size_t snd_mask_sizeof(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Takashi Iwai877211f2005-11-17 13:59:38 +010070 return sizeof(struct snd_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72
Takashi Iwai9bb22e22006-04-28 15:13:39 +020073static inline void snd_mask_none(struct snd_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 memset(mask, 0, sizeof(*mask));
76}
77
Takashi Iwai9bb22e22006-04-28 15:13:39 +020078static inline void snd_mask_any(struct snd_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 memset(mask, 0xff, SNDRV_MASK_SIZE * sizeof(u_int32_t));
81}
82
Takashi Iwai9bb22e22006-04-28 15:13:39 +020083static inline int snd_mask_empty(const struct snd_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 int i;
86 for (i = 0; i < SNDRV_MASK_SIZE; i++)
87 if (mask->bits[i])
88 return 0;
89 return 1;
90}
91
Takashi Iwai9bb22e22006-04-28 15:13:39 +020092static inline unsigned int snd_mask_min(const struct snd_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 for (i = 0; i < SNDRV_MASK_SIZE; i++) {
96 if (mask->bits[i])
97 return ffs(mask->bits[i]) - 1 + (i << 5);
98 }
99 return 0;
100}
101
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200102static inline unsigned int snd_mask_max(const struct snd_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 for (i = SNDRV_MASK_SIZE - 1; i >= 0; i--) {
106 if (mask->bits[i])
107 return ld2(mask->bits[i]) + (i << 5);
108 }
109 return 0;
110}
111
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200112static inline void snd_mask_set(struct snd_mask *mask, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 mask->bits[MASK_OFS(val)] |= MASK_BIT(val);
115}
116
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200117static inline void snd_mask_reset(struct snd_mask *mask, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 mask->bits[MASK_OFS(val)] &= ~MASK_BIT(val);
120}
121
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200122static inline void snd_mask_set_range(struct snd_mask *mask,
123 unsigned int from, unsigned int to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 for (i = from; i <= to; i++)
127 mask->bits[MASK_OFS(i)] |= MASK_BIT(i);
128}
129
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200130static inline void snd_mask_reset_range(struct snd_mask *mask,
131 unsigned int from, unsigned int to)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 for (i = from; i <= to; i++)
135 mask->bits[MASK_OFS(i)] &= ~MASK_BIT(i);
136}
137
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200138static inline void snd_mask_leave(struct snd_mask *mask, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 v = mask->bits[MASK_OFS(val)] & MASK_BIT(val);
142 snd_mask_none(mask);
143 mask->bits[MASK_OFS(val)] = v;
144}
145
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200146static inline void snd_mask_intersect(struct snd_mask *mask,
147 const struct snd_mask *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 int i;
150 for (i = 0; i < SNDRV_MASK_SIZE; i++)
151 mask->bits[i] &= v->bits[i];
152}
153
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200154static inline int snd_mask_eq(const struct snd_mask *mask,
155 const struct snd_mask *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 return ! memcmp(mask, v, SNDRV_MASK_SIZE * sizeof(u_int32_t));
158}
159
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200160static inline void snd_mask_copy(struct snd_mask *mask,
161 const struct snd_mask *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 *mask = *v;
164}
165
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200166static inline int snd_mask_test(const struct snd_mask *mask, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return mask->bits[MASK_OFS(val)] & MASK_BIT(val);
169}
170
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200171static inline int snd_mask_single(const struct snd_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 int i, c = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 for (i = 0; i < SNDRV_MASK_SIZE; i++) {
175 if (! mask->bits[i])
176 continue;
177 if (mask->bits[i] & (mask->bits[i] - 1))
178 return 0;
179 if (c)
180 return 0;
181 c++;
182 }
183 return 1;
184}
185
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200186static inline int snd_mask_refine(struct snd_mask *mask,
187 const struct snd_mask *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Takashi Iwai877211f2005-11-17 13:59:38 +0100189 struct snd_mask old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 snd_mask_copy(&old, mask);
191 snd_mask_intersect(mask, v);
192 if (snd_mask_empty(mask))
193 return -EINVAL;
194 return !snd_mask_eq(mask, &old);
195}
196
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200197static inline int snd_mask_refine_first(struct snd_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (snd_mask_single(mask))
200 return 0;
201 snd_mask_leave(mask, snd_mask_min(mask));
202 return 1;
203}
204
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200205static inline int snd_mask_refine_last(struct snd_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (snd_mask_single(mask))
208 return 0;
209 snd_mask_leave(mask, snd_mask_max(mask));
210 return 1;
211}
212
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200213static inline int snd_mask_refine_min(struct snd_mask *mask, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (snd_mask_min(mask) >= val)
216 return 0;
217 snd_mask_reset_range(mask, 0, val - 1);
218 if (snd_mask_empty(mask))
219 return -EINVAL;
220 return 1;
221}
222
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200223static inline int snd_mask_refine_max(struct snd_mask *mask, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (snd_mask_max(mask) <= val)
226 return 0;
227 snd_mask_reset_range(mask, val + 1, SNDRV_MASK_BITS);
228 if (snd_mask_empty(mask))
229 return -EINVAL;
230 return 1;
231}
232
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200233static inline int snd_mask_refine_set(struct snd_mask *mask, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 int changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 changed = !snd_mask_single(mask);
237 snd_mask_leave(mask, val);
238 if (snd_mask_empty(mask))
239 return -EINVAL;
240 return changed;
241}
242
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200243static inline int snd_mask_value(const struct snd_mask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return snd_mask_min(mask);
246}
247
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200248static inline void snd_interval_any(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 i->min = 0;
251 i->openmin = 0;
252 i->max = UINT_MAX;
253 i->openmax = 0;
254 i->integer = 0;
255 i->empty = 0;
256}
257
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200258static inline void snd_interval_none(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 i->empty = 1;
261}
262
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200263static inline int snd_interval_checkempty(const struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 return (i->min > i->max ||
266 (i->min == i->max && (i->openmin || i->openmax)));
267}
268
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200269static inline int snd_interval_empty(const struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 return i->empty;
272}
273
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200274static inline int snd_interval_single(const struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return (i->min == i->max ||
277 (i->min + 1 == i->max && i->openmax));
278}
279
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200280static inline int snd_interval_value(const struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return i->min;
283}
284
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200285static inline int snd_interval_min(const struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return i->min;
288}
289
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200290static inline int snd_interval_max(const struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 v = i->max;
294 if (i->openmax)
295 v--;
296 return v;
297}
298
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200299static inline int snd_interval_test(const struct snd_interval *i, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 return !((i->min > val || (i->min == val && i->openmin) ||
302 i->max < val || (i->max == val && i->openmax)));
303}
304
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200305static inline void snd_interval_copy(struct snd_interval *d, const struct snd_interval *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 *d = *s;
308}
309
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200310static inline int snd_interval_setinteger(struct snd_interval *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 if (i->integer)
313 return 0;
314 if (i->openmin && i->openmax && i->min == i->max)
315 return -EINVAL;
316 i->integer = 1;
317 return 1;
318}
319
Takashi Iwai9bb22e22006-04-28 15:13:39 +0200320static inline int snd_interval_eq(const struct snd_interval *i1, const struct snd_interval *i2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 if (i1->empty)
323 return i2->empty;
324 if (i2->empty)
325 return i1->empty;
326 return i1->min == i2->min && i1->openmin == i2->openmin &&
327 i1->max == i2->max && i1->openmax == i2->openmax;
328}
329
330static inline unsigned int add(unsigned int a, unsigned int b)
331{
332 if (a >= UINT_MAX - b)
333 return UINT_MAX;
334 return a + b;
335}
336
337static inline unsigned int sub(unsigned int a, unsigned int b)
338{
339 if (a > b)
340 return a - b;
341 return 0;
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344#endif /* __SOUND_PCM_PARAMS_H */
345