pcm_params.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef __SOUND_PCM_PARAMS_H
  3. #define __SOUND_PCM_PARAMS_H
  4. /*
  5. * PCM params helpers
  6. * Copyright (c) by Abramo Bagnara <[email protected]>
  7. */
  8. #include <sound/pcm.h>
  9. int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
  10. struct snd_pcm_hw_params *params,
  11. snd_pcm_hw_param_t var, int *dir);
  12. int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
  13. struct snd_pcm_hw_params *params,
  14. snd_pcm_hw_param_t var, int *dir);
  15. int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
  16. snd_pcm_hw_param_t var, int *dir);
  17. #define SNDRV_MASK_BITS 64 /* we use so far 64bits only */
  18. #define SNDRV_MASK_SIZE (SNDRV_MASK_BITS / 32)
  19. #define MASK_OFS(i) ((i) >> 5)
  20. #define MASK_BIT(i) (1U << ((i) & 31))
  21. static inline void snd_mask_none(struct snd_mask *mask)
  22. {
  23. memset(mask, 0, sizeof(*mask));
  24. }
  25. static inline void snd_mask_any(struct snd_mask *mask)
  26. {
  27. memset(mask, 0xff, SNDRV_MASK_SIZE * sizeof(u_int32_t));
  28. }
  29. static inline int snd_mask_empty(const struct snd_mask *mask)
  30. {
  31. int i;
  32. for (i = 0; i < SNDRV_MASK_SIZE; i++)
  33. if (mask->bits[i])
  34. return 0;
  35. return 1;
  36. }
  37. static inline unsigned int snd_mask_min(const struct snd_mask *mask)
  38. {
  39. int i;
  40. for (i = 0; i < SNDRV_MASK_SIZE; i++) {
  41. if (mask->bits[i])
  42. return __ffs(mask->bits[i]) + (i << 5);
  43. }
  44. return 0;
  45. }
  46. static inline unsigned int snd_mask_max(const struct snd_mask *mask)
  47. {
  48. int i;
  49. for (i = SNDRV_MASK_SIZE - 1; i >= 0; i--) {
  50. if (mask->bits[i])
  51. return __fls(mask->bits[i]) + (i << 5);
  52. }
  53. return 0;
  54. }
  55. static inline void snd_mask_set(struct snd_mask *mask, unsigned int val)
  56. {
  57. mask->bits[MASK_OFS(val)] |= MASK_BIT(val);
  58. }
  59. /* Most of drivers need only this one */
  60. static inline void snd_mask_set_format(struct snd_mask *mask,
  61. snd_pcm_format_t format)
  62. {
  63. snd_mask_set(mask, (__force unsigned int)format);
  64. }
  65. static inline void snd_mask_reset(struct snd_mask *mask, unsigned int val)
  66. {
  67. mask->bits[MASK_OFS(val)] &= ~MASK_BIT(val);
  68. }
  69. static inline void snd_mask_set_range(struct snd_mask *mask,
  70. unsigned int from, unsigned int to)
  71. {
  72. unsigned int i;
  73. for (i = from; i <= to; i++)
  74. mask->bits[MASK_OFS(i)] |= MASK_BIT(i);
  75. }
  76. static inline void snd_mask_reset_range(struct snd_mask *mask,
  77. unsigned int from, unsigned int to)
  78. {
  79. unsigned int i;
  80. for (i = from; i <= to; i++)
  81. mask->bits[MASK_OFS(i)] &= ~MASK_BIT(i);
  82. }
  83. static inline void snd_mask_leave(struct snd_mask *mask, unsigned int val)
  84. {
  85. unsigned int v;
  86. v = mask->bits[MASK_OFS(val)] & MASK_BIT(val);
  87. snd_mask_none(mask);
  88. mask->bits[MASK_OFS(val)] = v;
  89. }
  90. static inline void snd_mask_intersect(struct snd_mask *mask,
  91. const struct snd_mask *v)
  92. {
  93. int i;
  94. for (i = 0; i < SNDRV_MASK_SIZE; i++)
  95. mask->bits[i] &= v->bits[i];
  96. }
  97. static inline int snd_mask_eq(const struct snd_mask *mask,
  98. const struct snd_mask *v)
  99. {
  100. return ! memcmp(mask, v, SNDRV_MASK_SIZE * sizeof(u_int32_t));
  101. }
  102. static inline void snd_mask_copy(struct snd_mask *mask,
  103. const struct snd_mask *v)
  104. {
  105. *mask = *v;
  106. }
  107. static inline int snd_mask_test(const struct snd_mask *mask, unsigned int val)
  108. {
  109. return mask->bits[MASK_OFS(val)] & MASK_BIT(val);
  110. }
  111. /* Most of drivers need only this one */
  112. static inline int snd_mask_test_format(const struct snd_mask *mask,
  113. snd_pcm_format_t format)
  114. {
  115. return snd_mask_test(mask, (__force unsigned int)format);
  116. }
  117. static inline int snd_mask_single(const struct snd_mask *mask)
  118. {
  119. int i, c = 0;
  120. for (i = 0; i < SNDRV_MASK_SIZE; i++) {
  121. if (! mask->bits[i])
  122. continue;
  123. if (mask->bits[i] & (mask->bits[i] - 1))
  124. return 0;
  125. if (c)
  126. return 0;
  127. c++;
  128. }
  129. return 1;
  130. }
  131. static inline int snd_mask_refine(struct snd_mask *mask,
  132. const struct snd_mask *v)
  133. {
  134. struct snd_mask old;
  135. snd_mask_copy(&old, mask);
  136. snd_mask_intersect(mask, v);
  137. if (snd_mask_empty(mask))
  138. return -EINVAL;
  139. return !snd_mask_eq(mask, &old);
  140. }
  141. static inline int snd_mask_refine_first(struct snd_mask *mask)
  142. {
  143. if (snd_mask_single(mask))
  144. return 0;
  145. snd_mask_leave(mask, snd_mask_min(mask));
  146. return 1;
  147. }
  148. static inline int snd_mask_refine_last(struct snd_mask *mask)
  149. {
  150. if (snd_mask_single(mask))
  151. return 0;
  152. snd_mask_leave(mask, snd_mask_max(mask));
  153. return 1;
  154. }
  155. static inline int snd_mask_refine_min(struct snd_mask *mask, unsigned int val)
  156. {
  157. if (snd_mask_min(mask) >= val)
  158. return 0;
  159. snd_mask_reset_range(mask, 0, val - 1);
  160. if (snd_mask_empty(mask))
  161. return -EINVAL;
  162. return 1;
  163. }
  164. static inline int snd_mask_refine_max(struct snd_mask *mask, unsigned int val)
  165. {
  166. if (snd_mask_max(mask) <= val)
  167. return 0;
  168. snd_mask_reset_range(mask, val + 1, SNDRV_MASK_BITS);
  169. if (snd_mask_empty(mask))
  170. return -EINVAL;
  171. return 1;
  172. }
  173. static inline int snd_mask_refine_set(struct snd_mask *mask, unsigned int val)
  174. {
  175. int changed;
  176. changed = !snd_mask_single(mask);
  177. snd_mask_leave(mask, val);
  178. if (snd_mask_empty(mask))
  179. return -EINVAL;
  180. return changed;
  181. }
  182. static inline int snd_mask_value(const struct snd_mask *mask)
  183. {
  184. return snd_mask_min(mask);
  185. }
  186. static inline void snd_interval_any(struct snd_interval *i)
  187. {
  188. i->min = 0;
  189. i->openmin = 0;
  190. i->max = UINT_MAX;
  191. i->openmax = 0;
  192. i->integer = 0;
  193. i->empty = 0;
  194. }
  195. static inline void snd_interval_none(struct snd_interval *i)
  196. {
  197. i->empty = 1;
  198. }
  199. static inline int snd_interval_checkempty(const struct snd_interval *i)
  200. {
  201. return (i->min > i->max ||
  202. (i->min == i->max && (i->openmin || i->openmax)));
  203. }
  204. static inline int snd_interval_empty(const struct snd_interval *i)
  205. {
  206. return i->empty;
  207. }
  208. static inline int snd_interval_single(const struct snd_interval *i)
  209. {
  210. return (i->min == i->max ||
  211. (i->min + 1 == i->max && (i->openmin || i->openmax)));
  212. }
  213. static inline int snd_interval_value(const struct snd_interval *i)
  214. {
  215. if (i->openmin && !i->openmax)
  216. return i->max;
  217. return i->min;
  218. }
  219. static inline int snd_interval_min(const struct snd_interval *i)
  220. {
  221. return i->min;
  222. }
  223. static inline int snd_interval_max(const struct snd_interval *i)
  224. {
  225. unsigned int v;
  226. v = i->max;
  227. if (i->openmax)
  228. v--;
  229. return v;
  230. }
  231. static inline int snd_interval_test(const struct snd_interval *i, unsigned int val)
  232. {
  233. return !((i->min > val || (i->min == val && i->openmin) ||
  234. i->max < val || (i->max == val && i->openmax)));
  235. }
  236. static inline void snd_interval_copy(struct snd_interval *d, const struct snd_interval *s)
  237. {
  238. *d = *s;
  239. }
  240. static inline int snd_interval_setinteger(struct snd_interval *i)
  241. {
  242. if (i->integer)
  243. return 0;
  244. if (i->openmin && i->openmax && i->min == i->max)
  245. return -EINVAL;
  246. i->integer = 1;
  247. return 1;
  248. }
  249. static inline int snd_interval_eq(const struct snd_interval *i1, const struct snd_interval *i2)
  250. {
  251. if (i1->empty)
  252. return i2->empty;
  253. if (i2->empty)
  254. return i1->empty;
  255. return i1->min == i2->min && i1->openmin == i2->openmin &&
  256. i1->max == i2->max && i1->openmax == i2->openmax;
  257. }
  258. /**
  259. * params_access - get the access type from the hw params
  260. * @p: hw params
  261. */
  262. static inline snd_pcm_access_t params_access(const struct snd_pcm_hw_params *p)
  263. {
  264. return (__force snd_pcm_access_t)snd_mask_min(hw_param_mask_c(p,
  265. SNDRV_PCM_HW_PARAM_ACCESS));
  266. }
  267. /**
  268. * params_format - get the sample format from the hw params
  269. * @p: hw params
  270. */
  271. static inline snd_pcm_format_t params_format(const struct snd_pcm_hw_params *p)
  272. {
  273. return (__force snd_pcm_format_t)snd_mask_min(hw_param_mask_c(p,
  274. SNDRV_PCM_HW_PARAM_FORMAT));
  275. }
  276. /**
  277. * params_subformat - get the sample subformat from the hw params
  278. * @p: hw params
  279. */
  280. static inline snd_pcm_subformat_t
  281. params_subformat(const struct snd_pcm_hw_params *p)
  282. {
  283. return (__force snd_pcm_subformat_t)snd_mask_min(hw_param_mask_c(p,
  284. SNDRV_PCM_HW_PARAM_SUBFORMAT));
  285. }
  286. /**
  287. * params_period_bytes - get the period size (in bytes) from the hw params
  288. * @p: hw params
  289. */
  290. static inline unsigned int
  291. params_period_bytes(const struct snd_pcm_hw_params *p)
  292. {
  293. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_PERIOD_BYTES)->min;
  294. }
  295. /**
  296. * params_width - get the number of bits of the sample format from the hw params
  297. * @p: hw params
  298. *
  299. * This function returns the number of bits per sample that the selected sample
  300. * format of the hw params has.
  301. */
  302. static inline int params_width(const struct snd_pcm_hw_params *p)
  303. {
  304. return snd_pcm_format_width(params_format(p));
  305. }
  306. /*
  307. * params_physical_width - get the storage size of the sample format from the hw params
  308. * @p: hw params
  309. *
  310. * This functions returns the number of bits per sample that the selected sample
  311. * format of the hw params takes up in memory. This will be equal or larger than
  312. * params_width().
  313. */
  314. static inline int params_physical_width(const struct snd_pcm_hw_params *p)
  315. {
  316. return snd_pcm_format_physical_width(params_format(p));
  317. }
  318. static inline void
  319. params_set_format(struct snd_pcm_hw_params *p, snd_pcm_format_t fmt)
  320. {
  321. snd_mask_set_format(hw_param_mask(p, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
  322. }
  323. #endif /* __SOUND_PCM_PARAMS_H */