/kern_oII/sound/core/pcm_lib.c

http://omnia2droid.googlecode.com/ · C · 2237 lines · 1765 code · 178 blank · 294 comment · 416 complexity · 32055b193e6b41eca9d1516cf354f07b MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * Digital Audio (PCM) abstract layer
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. * Abramo Bagnara <abramo@alsa-project.org>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/slab.h>
  23. #include <linux/time.h>
  24. #include <sound/core.h>
  25. #include <sound/control.h>
  26. #include <sound/info.h>
  27. #include <sound/pcm.h>
  28. #include <sound/pcm_params.h>
  29. #include <sound/timer.h>
  30. #undef CONFIG_SND_S3C64XX_SOC_I2S_REC_DOWNSAMPLING
  31. #ifdef CONFIG_SND_S3C64XX_SOC_I2S_REC_DOWNSAMPLING
  32. #include "smallfilter.h"
  33. #define MAX_HWORD (32767)
  34. #define MIN_HWORD (-32768)
  35. /* Conversion constants */
  36. #define Nhc 8
  37. #define Na 7
  38. #define Np (Nhc+Na)
  39. #define Npc (1<<Nhc)
  40. #define Amask ((1<<Na)-1)
  41. #define Pmask ((1<<Np)-1)
  42. #define Nh 16
  43. #define Nb 16
  44. #define Nhxn 14
  45. #define Nhg (Nh-Nhxn)
  46. #define NLpScl 13
  47. #ifndef MAX
  48. #define MAX(x,y) ((x)>(y) ?(x):(y))
  49. #endif
  50. #ifndef MIN
  51. #define MIN(x,y) ((x)<(y) ?(x):(y))
  52. #endif
  53. #define MUS_SAMPLE_BITS 24
  54. #define MUS_SAMPLE_TYPE_TO_HWORD(x) ((short)((x)>>(MUS_SAMPLE_BITS-16)))
  55. #define HWORD_TO_MUS_SAMPLE_TYPE(x) ((mus_sample_t)((x)<<(MUS_SAMPLE_BITS-16)))
  56. #define OLDSAMPLE 44100
  57. #define NEWSAMPLE 8000
  58. static char *buf_user;
  59. static char *buf_user_org;
  60. static unsigned short Xoff = 48;
  61. static unsigned short Xp = 48;
  62. static unsigned short Xread = 48; /* Position in input array to read into */
  63. static unsigned int Time = (48<<Np); /* Current-time pointer for converter */
  64. static char isFirst = 1;
  65. #endif
  66. unsigned int ring_buf_index = 0;
  67. unsigned int period_index = 0;
  68. /*
  69. * fill ring buffer with silence
  70. * runtime->silence_start: starting pointer to silence area
  71. * runtime->silence_filled: size filled with silence
  72. * runtime->silence_threshold: threshold from application
  73. * runtime->silence_size: maximal size from application
  74. *
  75. * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
  76. */
  77. void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
  78. {
  79. struct snd_pcm_runtime *runtime = substream->runtime;
  80. snd_pcm_uframes_t frames, ofs, transfer;
  81. if (runtime->silence_size < runtime->boundary) {
  82. snd_pcm_sframes_t noise_dist, n;
  83. if (runtime->silence_start != runtime->control->appl_ptr) {
  84. n = runtime->control->appl_ptr - runtime->silence_start;
  85. if (n < 0)
  86. n += runtime->boundary;
  87. if ((snd_pcm_uframes_t)n < runtime->silence_filled)
  88. runtime->silence_filled -= n;
  89. else
  90. runtime->silence_filled = 0;
  91. runtime->silence_start = runtime->control->appl_ptr;
  92. }
  93. if (runtime->silence_filled >= runtime->buffer_size)
  94. return;
  95. noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
  96. if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
  97. return;
  98. frames = runtime->silence_threshold - noise_dist;
  99. if (frames > runtime->silence_size)
  100. frames = runtime->silence_size;
  101. } else {
  102. if (new_hw_ptr == ULONG_MAX) { /* initialization */
  103. snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
  104. runtime->silence_filled = avail > 0 ? avail : 0;
  105. runtime->silence_start = (runtime->status->hw_ptr +
  106. runtime->silence_filled) %
  107. runtime->boundary;
  108. } else {
  109. ofs = runtime->status->hw_ptr;
  110. frames = new_hw_ptr - ofs;
  111. if ((snd_pcm_sframes_t)frames < 0)
  112. frames += runtime->boundary;
  113. runtime->silence_filled -= frames;
  114. if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
  115. runtime->silence_filled = 0;
  116. runtime->silence_start = new_hw_ptr;
  117. } else {
  118. runtime->silence_start = ofs;
  119. }
  120. }
  121. frames = runtime->buffer_size - runtime->silence_filled;
  122. }
  123. if (snd_BUG_ON(frames > runtime->buffer_size))
  124. return;
  125. if (frames == 0)
  126. return;
  127. ofs = runtime->silence_start % runtime->buffer_size;
  128. while (frames > 0) {
  129. transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
  130. if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
  131. runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
  132. if (substream->ops->silence) {
  133. int err;
  134. err = substream->ops->silence(substream, -1, ofs, transfer);
  135. snd_BUG_ON(err < 0);
  136. } else {
  137. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
  138. snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
  139. }
  140. } else {
  141. unsigned int c;
  142. unsigned int channels = runtime->channels;
  143. if (substream->ops->silence) {
  144. for (c = 0; c < channels; ++c) {
  145. int err;
  146. err = substream->ops->silence(substream, c, ofs, transfer);
  147. snd_BUG_ON(err < 0);
  148. }
  149. } else {
  150. size_t dma_csize = runtime->dma_bytes / channels;
  151. for (c = 0; c < channels; ++c) {
  152. char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
  153. snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
  154. }
  155. }
  156. }
  157. runtime->silence_filled += transfer;
  158. frames -= transfer;
  159. ofs = 0;
  160. }
  161. }
  162. static void xrun(struct snd_pcm_substream *substream)
  163. {
  164. // printk("%s: [%d]occured XRUN!\n", __func__, substream->stream);
  165. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  166. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  167. if (substream->pstr->xrun_debug) {
  168. snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
  169. substream->pcm->card->number,
  170. substream->pcm->device,
  171. substream->stream ? 'c' : 'p');
  172. if (substream->pstr->xrun_debug > 1)
  173. dump_stack();
  174. }
  175. #endif
  176. }
  177. static inline snd_pcm_uframes_t snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
  178. struct snd_pcm_runtime *runtime)
  179. {
  180. snd_pcm_uframes_t pos;
  181. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
  182. snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
  183. pos = substream->ops->pointer(substream);
  184. if (pos == SNDRV_PCM_POS_XRUN)
  185. return pos; /* XRUN */
  186. #ifdef CONFIG_SND_DEBUG
  187. if (pos >= (runtime->buffer_size * ANDROID_BUF_NUM)) {
  188. snd_printk(KERN_ERR "BUG: stream = %i, pos = 0x%lx, buffer size = 0x%lx, period size = 0x%lx\n", substream->stream, pos, runtime->buffer_size, runtime->period_size);
  189. }
  190. #endif
  191. pos -= pos % runtime->min_align;
  192. return pos;
  193. }
  194. static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
  195. struct snd_pcm_runtime *runtime)
  196. {
  197. snd_pcm_uframes_t avail;
  198. unsigned int stop_threshold = 0;
  199. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  200. avail = snd_pcm_playback_avail(runtime);
  201. else
  202. avail = snd_pcm_capture_avail(runtime);
  203. if (avail > runtime->avail_max)
  204. runtime->avail_max = avail;
  205. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  206. stop_threshold = runtime->stop_threshold * ANDROID_BUF_NUM;
  207. else
  208. stop_threshold = runtime->stop_threshold;
  209. if (avail >= stop_threshold) {
  210. if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
  211. snd_pcm_drain_done(substream);
  212. else
  213. xrun(substream);
  214. return -EPIPE;
  215. }
  216. if (avail >= runtime->control->avail_min)
  217. wake_up(&runtime->sleep);
  218. return 0;
  219. }
  220. static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
  221. {
  222. struct snd_pcm_runtime *runtime = substream->runtime;
  223. snd_pcm_uframes_t pos;
  224. snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt;
  225. snd_pcm_sframes_t delta;
  226. pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
  227. if (pos == SNDRV_PCM_POS_XRUN) {
  228. xrun(substream);
  229. return -EPIPE;
  230. }
  231. if (runtime->period_size == runtime->buffer_size)
  232. goto __next_buf;
  233. new_hw_ptr = runtime->hw_ptr_base + pos;
  234. hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
  235. delta = hw_ptr_interrupt - new_hw_ptr;
  236. if (delta > 0) {
  237. if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
  238. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  239. if (runtime->periods > 1 && substream->pstr->xrun_debug) {
  240. snd_printd(KERN_ERR "Unexpected hw_pointer value [1] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2);
  241. if (substream->pstr->xrun_debug > 1)
  242. dump_stack();
  243. }
  244. #endif
  245. return 0;
  246. }
  247. __next_buf:
  248. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  249. runtime->hw_ptr_base += runtime->buffer_size * ANDROID_BUF_NUM;
  250. else
  251. runtime->hw_ptr_base += runtime->buffer_size;
  252. if (runtime->hw_ptr_base == runtime->boundary)
  253. runtime->hw_ptr_base = 0;
  254. new_hw_ptr = runtime->hw_ptr_base + pos;
  255. }
  256. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  257. runtime->silence_size > 0)
  258. snd_pcm_playback_silence(substream, new_hw_ptr);
  259. runtime->status->hw_ptr = new_hw_ptr;
  260. runtime->hw_ptr_interrupt = new_hw_ptr - new_hw_ptr % runtime->period_size;
  261. return snd_pcm_update_hw_ptr_post(substream, runtime);
  262. }
  263. /* CAUTION: call it with irq disabled */
  264. int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
  265. {
  266. struct snd_pcm_runtime *runtime = substream->runtime;
  267. snd_pcm_uframes_t pos;
  268. snd_pcm_uframes_t old_hw_ptr, new_hw_ptr;
  269. snd_pcm_sframes_t delta;
  270. old_hw_ptr = runtime->status->hw_ptr;
  271. pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
  272. if (pos == SNDRV_PCM_POS_XRUN) {
  273. xrun(substream);
  274. return -EPIPE;
  275. }
  276. new_hw_ptr = runtime->hw_ptr_base + pos;
  277. delta = old_hw_ptr - new_hw_ptr;
  278. if (delta > 0) {
  279. if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
  280. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  281. if (runtime->periods > 2 && substream->pstr->xrun_debug) {
  282. snd_printd(KERN_ERR "Unexpected hw_pointer value [2] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2);
  283. if (substream->pstr->xrun_debug > 1)
  284. dump_stack();
  285. }
  286. #endif
  287. return 0;
  288. }
  289. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  290. runtime->hw_ptr_base += runtime->buffer_size * ANDROID_BUF_NUM;
  291. else
  292. runtime->hw_ptr_base += runtime->buffer_size;
  293. if (runtime->hw_ptr_base == runtime->boundary)
  294. runtime->hw_ptr_base = 0;
  295. new_hw_ptr = runtime->hw_ptr_base + pos;
  296. }
  297. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  298. runtime->silence_size > 0)
  299. snd_pcm_playback_silence(substream, new_hw_ptr);
  300. runtime->status->hw_ptr = new_hw_ptr;
  301. return snd_pcm_update_hw_ptr_post(substream, runtime);
  302. }
  303. /**
  304. * snd_pcm_set_ops - set the PCM operators
  305. * @pcm: the pcm instance
  306. * @direction: stream direction, SNDRV_PCM_STREAM_XXX
  307. * @ops: the operator table
  308. *
  309. * Sets the given PCM operators to the pcm instance.
  310. */
  311. void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
  312. {
  313. struct snd_pcm_str *stream = &pcm->streams[direction];
  314. struct snd_pcm_substream *substream;
  315. for (substream = stream->substream; substream != NULL; substream = substream->next)
  316. substream->ops = ops;
  317. }
  318. EXPORT_SYMBOL(snd_pcm_set_ops);
  319. /**
  320. * snd_pcm_sync - set the PCM sync id
  321. * @substream: the pcm substream
  322. *
  323. * Sets the PCM sync identifier for the card.
  324. */
  325. void snd_pcm_set_sync(struct snd_pcm_substream *substream)
  326. {
  327. struct snd_pcm_runtime *runtime = substream->runtime;
  328. runtime->sync.id32[0] = substream->pcm->card->number;
  329. runtime->sync.id32[1] = -1;
  330. runtime->sync.id32[2] = -1;
  331. runtime->sync.id32[3] = -1;
  332. }
  333. EXPORT_SYMBOL(snd_pcm_set_sync);
  334. /*
  335. * Standard ioctl routine
  336. */
  337. static inline unsigned int div32(unsigned int a, unsigned int b,
  338. unsigned int *r)
  339. {
  340. if (b == 0) {
  341. *r = 0;
  342. return UINT_MAX;
  343. }
  344. *r = a % b;
  345. return a / b;
  346. }
  347. static inline unsigned int div_down(unsigned int a, unsigned int b)
  348. {
  349. if (b == 0)
  350. return UINT_MAX;
  351. return a / b;
  352. }
  353. static inline unsigned int div_up(unsigned int a, unsigned int b)
  354. {
  355. unsigned int r;
  356. unsigned int q;
  357. if (b == 0)
  358. return UINT_MAX;
  359. q = div32(a, b, &r);
  360. if (r)
  361. ++q;
  362. return q;
  363. }
  364. static inline unsigned int mul(unsigned int a, unsigned int b)
  365. {
  366. if (a == 0)
  367. return 0;
  368. if (div_down(UINT_MAX, a) < b)
  369. return UINT_MAX;
  370. return a * b;
  371. }
  372. static inline unsigned int muldiv32(unsigned int a, unsigned int b,
  373. unsigned int c, unsigned int *r)
  374. {
  375. u_int64_t n = (u_int64_t) a * b;
  376. if (c == 0) {
  377. snd_BUG_ON(!n);
  378. *r = 0;
  379. return UINT_MAX;
  380. }
  381. div64_32(&n, c, r);
  382. if (n >= UINT_MAX) {
  383. *r = 0;
  384. return UINT_MAX;
  385. }
  386. return n;
  387. }
  388. /**
  389. * snd_interval_refine - refine the interval value of configurator
  390. * @i: the interval value to refine
  391. * @v: the interval value to refer to
  392. *
  393. * Refines the interval value with the reference value.
  394. * The interval is changed to the range satisfying both intervals.
  395. * The interval status (min, max, integer, etc.) are evaluated.
  396. *
  397. * Returns non-zero if the value is changed, zero if not changed.
  398. */
  399. int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
  400. {
  401. int changed = 0;
  402. if (snd_BUG_ON(snd_interval_empty(i)))
  403. return -EINVAL;
  404. if (i->min < v->min) {
  405. i->min = v->min;
  406. i->openmin = v->openmin;
  407. changed = 1;
  408. } else if (i->min == v->min && !i->openmin && v->openmin) {
  409. i->openmin = 1;
  410. changed = 1;
  411. }
  412. if (i->max > v->max) {
  413. i->max = v->max;
  414. i->openmax = v->openmax;
  415. changed = 1;
  416. } else if (i->max == v->max && !i->openmax && v->openmax) {
  417. i->openmax = 1;
  418. changed = 1;
  419. }
  420. if (!i->integer && v->integer) {
  421. i->integer = 1;
  422. changed = 1;
  423. }
  424. if (i->integer) {
  425. if (i->openmin) {
  426. i->min++;
  427. i->openmin = 0;
  428. }
  429. if (i->openmax) {
  430. i->max--;
  431. i->openmax = 0;
  432. }
  433. } else if (!i->openmin && !i->openmax && i->min == i->max)
  434. i->integer = 1;
  435. if (snd_interval_checkempty(i)) {
  436. snd_interval_none(i);
  437. return -EINVAL;
  438. }
  439. return changed;
  440. }
  441. EXPORT_SYMBOL(snd_interval_refine);
  442. static int snd_interval_refine_first(struct snd_interval *i)
  443. {
  444. if (snd_BUG_ON(snd_interval_empty(i)))
  445. return -EINVAL;
  446. if (snd_interval_single(i))
  447. return 0;
  448. i->max = i->min;
  449. i->openmax = i->openmin;
  450. if (i->openmax)
  451. i->max++;
  452. return 1;
  453. }
  454. static int snd_interval_refine_last(struct snd_interval *i)
  455. {
  456. if (snd_BUG_ON(snd_interval_empty(i)))
  457. return -EINVAL;
  458. if (snd_interval_single(i))
  459. return 0;
  460. i->min = i->max;
  461. i->openmin = i->openmax;
  462. if (i->openmin)
  463. i->min--;
  464. return 1;
  465. }
  466. void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
  467. {
  468. if (a->empty || b->empty) {
  469. snd_interval_none(c);
  470. return;
  471. }
  472. c->empty = 0;
  473. c->min = mul(a->min, b->min);
  474. c->openmin = (a->openmin || b->openmin);
  475. c->max = mul(a->max, b->max);
  476. c->openmax = (a->openmax || b->openmax);
  477. c->integer = (a->integer && b->integer);
  478. }
  479. /**
  480. * snd_interval_div - refine the interval value with division
  481. * @a: dividend
  482. * @b: divisor
  483. * @c: quotient
  484. *
  485. * c = a / b
  486. *
  487. * Returns non-zero if the value is changed, zero if not changed.
  488. */
  489. void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
  490. {
  491. unsigned int r;
  492. if (a->empty || b->empty) {
  493. snd_interval_none(c);
  494. return;
  495. }
  496. c->empty = 0;
  497. c->min = div32(a->min, b->max, &r);
  498. c->openmin = (r || a->openmin || b->openmax);
  499. if (b->min > 0) {
  500. c->max = div32(a->max, b->min, &r);
  501. if (r) {
  502. c->max++;
  503. c->openmax = 1;
  504. } else
  505. c->openmax = (a->openmax || b->openmin);
  506. } else {
  507. c->max = UINT_MAX;
  508. c->openmax = 0;
  509. }
  510. c->integer = 0;
  511. }
  512. /**
  513. * snd_interval_muldivk - refine the interval value
  514. * @a: dividend 1
  515. * @b: dividend 2
  516. * @k: divisor (as integer)
  517. * @c: result
  518. *
  519. * c = a * b / k
  520. *
  521. * Returns non-zero if the value is changed, zero if not changed.
  522. */
  523. void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
  524. unsigned int k, struct snd_interval *c)
  525. {
  526. unsigned int r;
  527. if (a->empty || b->empty) {
  528. snd_interval_none(c);
  529. return;
  530. }
  531. c->empty = 0;
  532. c->min = muldiv32(a->min, b->min, k, &r);
  533. c->openmin = (r || a->openmin || b->openmin);
  534. c->max = muldiv32(a->max, b->max, k, &r);
  535. if (r) {
  536. c->max++;
  537. c->openmax = 1;
  538. } else
  539. c->openmax = (a->openmax || b->openmax);
  540. c->integer = 0;
  541. }
  542. /**
  543. * snd_interval_mulkdiv - refine the interval value
  544. * @a: dividend 1
  545. * @k: dividend 2 (as integer)
  546. * @b: divisor
  547. * @c: result
  548. *
  549. * c = a * k / b
  550. *
  551. * Returns non-zero if the value is changed, zero if not changed.
  552. */
  553. void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
  554. const struct snd_interval *b, struct snd_interval *c)
  555. {
  556. unsigned int r;
  557. if (a->empty || b->empty) {
  558. snd_interval_none(c);
  559. return;
  560. }
  561. c->empty = 0;
  562. c->min = muldiv32(a->min, k, b->max, &r);
  563. c->openmin = (r || a->openmin || b->openmax);
  564. if (b->min > 0) {
  565. c->max = muldiv32(a->max, k, b->min, &r);
  566. if (r) {
  567. c->max++;
  568. c->openmax = 1;
  569. } else
  570. c->openmax = (a->openmax || b->openmin);
  571. } else {
  572. c->max = UINT_MAX;
  573. c->openmax = 0;
  574. }
  575. c->integer = 0;
  576. }
  577. /* ---- */
  578. /**
  579. * snd_interval_ratnum - refine the interval value
  580. * @i: interval to refine
  581. * @rats_count: number of ratnum_t
  582. * @rats: ratnum_t array
  583. * @nump: pointer to store the resultant numerator
  584. * @denp: pointer to store the resultant denominator
  585. *
  586. * Returns non-zero if the value is changed, zero if not changed.
  587. */
  588. int snd_interval_ratnum(struct snd_interval *i,
  589. unsigned int rats_count, struct snd_ratnum *rats,
  590. unsigned int *nump, unsigned int *denp)
  591. {
  592. unsigned int best_num, best_diff, best_den;
  593. unsigned int k;
  594. struct snd_interval t;
  595. int err;
  596. best_num = best_den = best_diff = 0;
  597. for (k = 0; k < rats_count; ++k) {
  598. unsigned int num = rats[k].num;
  599. unsigned int den;
  600. unsigned int q = i->min;
  601. int diff;
  602. if (q == 0)
  603. q = 1;
  604. den = div_down(num, q);
  605. if (den < rats[k].den_min)
  606. continue;
  607. if (den > rats[k].den_max)
  608. den = rats[k].den_max;
  609. else {
  610. unsigned int r;
  611. r = (den - rats[k].den_min) % rats[k].den_step;
  612. if (r != 0)
  613. den -= r;
  614. }
  615. diff = num - q * den;
  616. if (best_num == 0 ||
  617. diff * best_den < best_diff * den) {
  618. best_diff = diff;
  619. best_den = den;
  620. best_num = num;
  621. }
  622. }
  623. if (best_den == 0) {
  624. i->empty = 1;
  625. return -EINVAL;
  626. }
  627. t.min = div_down(best_num, best_den);
  628. t.openmin = !!(best_num % best_den);
  629. best_num = best_den = best_diff = 0;
  630. for (k = 0; k < rats_count; ++k) {
  631. unsigned int num = rats[k].num;
  632. unsigned int den;
  633. unsigned int q = i->max;
  634. int diff;
  635. if (q == 0) {
  636. i->empty = 1;
  637. return -EINVAL;
  638. }
  639. den = div_up(num, q);
  640. if (den > rats[k].den_max)
  641. continue;
  642. if (den < rats[k].den_min)
  643. den = rats[k].den_min;
  644. else {
  645. unsigned int r;
  646. r = (den - rats[k].den_min) % rats[k].den_step;
  647. if (r != 0)
  648. den += rats[k].den_step - r;
  649. }
  650. diff = q * den - num;
  651. if (best_num == 0 ||
  652. diff * best_den < best_diff * den) {
  653. best_diff = diff;
  654. best_den = den;
  655. best_num = num;
  656. }
  657. }
  658. if (best_den == 0) {
  659. i->empty = 1;
  660. return -EINVAL;
  661. }
  662. t.max = div_up(best_num, best_den);
  663. t.openmax = !!(best_num % best_den);
  664. t.integer = 0;
  665. err = snd_interval_refine(i, &t);
  666. if (err < 0)
  667. return err;
  668. if (snd_interval_single(i)) {
  669. if (nump)
  670. *nump = best_num;
  671. if (denp)
  672. *denp = best_den;
  673. }
  674. return err;
  675. }
  676. EXPORT_SYMBOL(snd_interval_ratnum);
  677. /**
  678. * snd_interval_ratden - refine the interval value
  679. * @i: interval to refine
  680. * @rats_count: number of struct ratden
  681. * @rats: struct ratden array
  682. * @nump: pointer to store the resultant numerator
  683. * @denp: pointer to store the resultant denominator
  684. *
  685. * Returns non-zero if the value is changed, zero if not changed.
  686. */
  687. static int snd_interval_ratden(struct snd_interval *i,
  688. unsigned int rats_count, struct snd_ratden *rats,
  689. unsigned int *nump, unsigned int *denp)
  690. {
  691. unsigned int best_num, best_diff, best_den;
  692. unsigned int k;
  693. struct snd_interval t;
  694. int err;
  695. best_num = best_den = best_diff = 0;
  696. for (k = 0; k < rats_count; ++k) {
  697. unsigned int num;
  698. unsigned int den = rats[k].den;
  699. unsigned int q = i->min;
  700. int diff;
  701. num = mul(q, den);
  702. if (num > rats[k].num_max)
  703. continue;
  704. if (num < rats[k].num_min)
  705. num = rats[k].num_max;
  706. else {
  707. unsigned int r;
  708. r = (num - rats[k].num_min) % rats[k].num_step;
  709. if (r != 0)
  710. num += rats[k].num_step - r;
  711. }
  712. diff = num - q * den;
  713. if (best_num == 0 ||
  714. diff * best_den < best_diff * den) {
  715. best_diff = diff;
  716. best_den = den;
  717. best_num = num;
  718. }
  719. }
  720. if (best_den == 0) {
  721. i->empty = 1;
  722. return -EINVAL;
  723. }
  724. t.min = div_down(best_num, best_den);
  725. t.openmin = !!(best_num % best_den);
  726. best_num = best_den = best_diff = 0;
  727. for (k = 0; k < rats_count; ++k) {
  728. unsigned int num;
  729. unsigned int den = rats[k].den;
  730. unsigned int q = i->max;
  731. int diff;
  732. num = mul(q, den);
  733. if (num < rats[k].num_min)
  734. continue;
  735. if (num > rats[k].num_max)
  736. num = rats[k].num_max;
  737. else {
  738. unsigned int r;
  739. r = (num - rats[k].num_min) % rats[k].num_step;
  740. if (r != 0)
  741. num -= r;
  742. }
  743. diff = q * den - num;
  744. if (best_num == 0 ||
  745. diff * best_den < best_diff * den) {
  746. best_diff = diff;
  747. best_den = den;
  748. best_num = num;
  749. }
  750. }
  751. if (best_den == 0) {
  752. i->empty = 1;
  753. return -EINVAL;
  754. }
  755. t.max = div_up(best_num, best_den);
  756. t.openmax = !!(best_num % best_den);
  757. t.integer = 0;
  758. err = snd_interval_refine(i, &t);
  759. if (err < 0)
  760. return err;
  761. if (snd_interval_single(i)) {
  762. if (nump)
  763. *nump = best_num;
  764. if (denp)
  765. *denp = best_den;
  766. }
  767. return err;
  768. }
  769. /**
  770. * snd_interval_list - refine the interval value from the list
  771. * @i: the interval value to refine
  772. * @count: the number of elements in the list
  773. * @list: the value list
  774. * @mask: the bit-mask to evaluate
  775. *
  776. * Refines the interval value from the list.
  777. * When mask is non-zero, only the elements corresponding to bit 1 are
  778. * evaluated.
  779. *
  780. * Returns non-zero if the value is changed, zero if not changed.
  781. */
  782. int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
  783. {
  784. unsigned int k;
  785. int changed = 0;
  786. if (!count) {
  787. i->empty = 1;
  788. return -EINVAL;
  789. }
  790. for (k = 0; k < count; k++) {
  791. if (mask && !(mask & (1 << k)))
  792. continue;
  793. if (i->min == list[k] && !i->openmin)
  794. goto _l1;
  795. if (i->min < list[k]) {
  796. i->min = list[k];
  797. i->openmin = 0;
  798. changed = 1;
  799. goto _l1;
  800. }
  801. }
  802. i->empty = 1;
  803. return -EINVAL;
  804. _l1:
  805. for (k = count; k-- > 0;) {
  806. if (mask && !(mask & (1 << k)))
  807. continue;
  808. if (i->max == list[k] && !i->openmax)
  809. goto _l2;
  810. if (i->max > list[k]) {
  811. i->max = list[k];
  812. i->openmax = 0;
  813. changed = 1;
  814. goto _l2;
  815. }
  816. }
  817. i->empty = 1;
  818. return -EINVAL;
  819. _l2:
  820. if (snd_interval_checkempty(i)) {
  821. i->empty = 1;
  822. return -EINVAL;
  823. }
  824. return changed;
  825. }
  826. EXPORT_SYMBOL(snd_interval_list);
  827. static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
  828. {
  829. unsigned int n;
  830. int changed = 0;
  831. n = (i->min - min) % step;
  832. if (n != 0 || i->openmin) {
  833. i->min += step - n;
  834. changed = 1;
  835. }
  836. n = (i->max - min) % step;
  837. if (n != 0 || i->openmax) {
  838. i->max -= n;
  839. changed = 1;
  840. }
  841. if (snd_interval_checkempty(i)) {
  842. i->empty = 1;
  843. return -EINVAL;
  844. }
  845. return changed;
  846. }
  847. /* Info constraints helpers */
  848. /**
  849. * snd_pcm_hw_rule_add - add the hw-constraint rule
  850. * @runtime: the pcm runtime instance
  851. * @cond: condition bits
  852. * @var: the variable to evaluate
  853. * @func: the evaluation function
  854. * @private: the private data pointer passed to function
  855. * @dep: the dependent variables
  856. *
  857. * Returns zero if successful, or a negative error code on failure.
  858. */
  859. int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
  860. int var,
  861. snd_pcm_hw_rule_func_t func, void *private,
  862. int dep, ...)
  863. {
  864. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  865. struct snd_pcm_hw_rule *c;
  866. unsigned int k;
  867. va_list args;
  868. va_start(args, dep);
  869. if (constrs->rules_num >= constrs->rules_all) {
  870. struct snd_pcm_hw_rule *new;
  871. unsigned int new_rules = constrs->rules_all + 16;
  872. new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
  873. if (!new)
  874. return -ENOMEM;
  875. if (constrs->rules) {
  876. memcpy(new, constrs->rules,
  877. constrs->rules_num * sizeof(*c));
  878. kfree(constrs->rules);
  879. }
  880. constrs->rules = new;
  881. constrs->rules_all = new_rules;
  882. }
  883. c = &constrs->rules[constrs->rules_num];
  884. c->cond = cond;
  885. c->func = func;
  886. c->var = var;
  887. c->private = private;
  888. k = 0;
  889. while (1) {
  890. if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
  891. return -EINVAL;
  892. c->deps[k++] = dep;
  893. if (dep < 0)
  894. break;
  895. dep = va_arg(args, int);
  896. }
  897. constrs->rules_num++;
  898. va_end(args);
  899. return 0;
  900. }
  901. EXPORT_SYMBOL(snd_pcm_hw_rule_add);
  902. /**
  903. * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
  904. * @runtime: PCM runtime instance
  905. * @var: hw_params variable to apply the mask
  906. * @mask: the bitmap mask
  907. *
  908. * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
  909. */
  910. int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  911. u_int32_t mask)
  912. {
  913. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  914. struct snd_mask *maskp = constrs_mask(constrs, var);
  915. *maskp->bits &= mask;
  916. memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
  917. if (*maskp->bits == 0)
  918. return -EINVAL;
  919. return 0;
  920. }
  921. /**
  922. * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
  923. * @runtime: PCM runtime instance
  924. * @var: hw_params variable to apply the mask
  925. * @mask: the 64bit bitmap mask
  926. *
  927. * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
  928. */
  929. int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  930. u_int64_t mask)
  931. {
  932. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  933. struct snd_mask *maskp = constrs_mask(constrs, var);
  934. maskp->bits[0] &= (u_int32_t)mask;
  935. maskp->bits[1] &= (u_int32_t)(mask >> 32);
  936. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
  937. if (! maskp->bits[0] && ! maskp->bits[1])
  938. return -EINVAL;
  939. return 0;
  940. }
  941. /**
  942. * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
  943. * @runtime: PCM runtime instance
  944. * @var: hw_params variable to apply the integer constraint
  945. *
  946. * Apply the constraint of integer to an interval parameter.
  947. */
  948. int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
  949. {
  950. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  951. return snd_interval_setinteger(constrs_interval(constrs, var));
  952. }
  953. EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
  954. /**
  955. * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
  956. * @runtime: PCM runtime instance
  957. * @var: hw_params variable to apply the range
  958. * @min: the minimal value
  959. * @max: the maximal value
  960. *
  961. * Apply the min/max range constraint to an interval parameter.
  962. */
  963. int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  964. unsigned int min, unsigned int max)
  965. {
  966. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  967. struct snd_interval t;
  968. t.min = min;
  969. t.max = max;
  970. t.openmin = t.openmax = 0;
  971. t.integer = 0;
  972. return snd_interval_refine(constrs_interval(constrs, var), &t);
  973. }
  974. EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
  975. static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
  976. struct snd_pcm_hw_rule *rule)
  977. {
  978. struct snd_pcm_hw_constraint_list *list = rule->private;
  979. return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
  980. }
  981. /**
  982. * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
  983. * @runtime: PCM runtime instance
  984. * @cond: condition bits
  985. * @var: hw_params variable to apply the list constraint
  986. * @l: list
  987. *
  988. * Apply the list of constraints to an interval parameter.
  989. */
  990. int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
  991. unsigned int cond,
  992. snd_pcm_hw_param_t var,
  993. struct snd_pcm_hw_constraint_list *l)
  994. {
  995. return snd_pcm_hw_rule_add(runtime, cond, var,
  996. snd_pcm_hw_rule_list, l,
  997. var, -1);
  998. }
  999. EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
  1000. static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
  1001. struct snd_pcm_hw_rule *rule)
  1002. {
  1003. struct snd_pcm_hw_constraint_ratnums *r = rule->private;
  1004. unsigned int num = 0, den = 0;
  1005. int err;
  1006. err = snd_interval_ratnum(hw_param_interval(params, rule->var),
  1007. r->nrats, r->rats, &num, &den);
  1008. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1009. params->rate_num = num;
  1010. params->rate_den = den;
  1011. }
  1012. return err;
  1013. }
  1014. /**
  1015. * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
  1016. * @runtime: PCM runtime instance
  1017. * @cond: condition bits
  1018. * @var: hw_params variable to apply the ratnums constraint
  1019. * @r: struct snd_ratnums constriants
  1020. */
  1021. int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
  1022. unsigned int cond,
  1023. snd_pcm_hw_param_t var,
  1024. struct snd_pcm_hw_constraint_ratnums *r)
  1025. {
  1026. return snd_pcm_hw_rule_add(runtime, cond, var,
  1027. snd_pcm_hw_rule_ratnums, r,
  1028. var, -1);
  1029. }
  1030. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
  1031. static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
  1032. struct snd_pcm_hw_rule *rule)
  1033. {
  1034. struct snd_pcm_hw_constraint_ratdens *r = rule->private;
  1035. unsigned int num = 0, den = 0;
  1036. int err = snd_interval_ratden(hw_param_interval(params, rule->var),
  1037. r->nrats, r->rats, &num, &den);
  1038. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1039. params->rate_num = num;
  1040. params->rate_den = den;
  1041. }
  1042. return err;
  1043. }
  1044. /**
  1045. * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
  1046. * @runtime: PCM runtime instance
  1047. * @cond: condition bits
  1048. * @var: hw_params variable to apply the ratdens constraint
  1049. * @r: struct snd_ratdens constriants
  1050. */
  1051. int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
  1052. unsigned int cond,
  1053. snd_pcm_hw_param_t var,
  1054. struct snd_pcm_hw_constraint_ratdens *r)
  1055. {
  1056. return snd_pcm_hw_rule_add(runtime, cond, var,
  1057. snd_pcm_hw_rule_ratdens, r,
  1058. var, -1);
  1059. }
  1060. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
  1061. static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
  1062. struct snd_pcm_hw_rule *rule)
  1063. {
  1064. unsigned int l = (unsigned long) rule->private;
  1065. int width = l & 0xffff;
  1066. unsigned int msbits = l >> 16;
  1067. struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
  1068. if (snd_interval_single(i) && snd_interval_value(i) == width)
  1069. params->msbits = msbits;
  1070. return 0;
  1071. }
  1072. /**
  1073. * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
  1074. * @runtime: PCM runtime instance
  1075. * @cond: condition bits
  1076. * @width: sample bits width
  1077. * @msbits: msbits width
  1078. */
  1079. int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
  1080. unsigned int cond,
  1081. unsigned int width,
  1082. unsigned int msbits)
  1083. {
  1084. unsigned long l = (msbits << 16) | width;
  1085. return snd_pcm_hw_rule_add(runtime, cond, -1,
  1086. snd_pcm_hw_rule_msbits,
  1087. (void*) l,
  1088. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1089. }
  1090. EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
  1091. static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
  1092. struct snd_pcm_hw_rule *rule)
  1093. {
  1094. unsigned long step = (unsigned long) rule->private;
  1095. return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
  1096. }
  1097. /**
  1098. * snd_pcm_hw_constraint_step - add a hw constraint step rule
  1099. * @runtime: PCM runtime instance
  1100. * @cond: condition bits
  1101. * @var: hw_params variable to apply the step constraint
  1102. * @step: step size
  1103. */
  1104. int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
  1105. unsigned int cond,
  1106. snd_pcm_hw_param_t var,
  1107. unsigned long step)
  1108. {
  1109. return snd_pcm_hw_rule_add(runtime, cond, var,
  1110. snd_pcm_hw_rule_step, (void *) step,
  1111. var, -1);
  1112. }
  1113. EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
  1114. static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
  1115. {
  1116. static unsigned int pow2_sizes[] = {
  1117. 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
  1118. 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
  1119. 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
  1120. 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
  1121. };
  1122. return snd_interval_list(hw_param_interval(params, rule->var),
  1123. ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
  1124. }
  1125. /**
  1126. * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
  1127. * @runtime: PCM runtime instance
  1128. * @cond: condition bits
  1129. * @var: hw_params variable to apply the power-of-2 constraint
  1130. */
  1131. int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
  1132. unsigned int cond,
  1133. snd_pcm_hw_param_t var)
  1134. {
  1135. return snd_pcm_hw_rule_add(runtime, cond, var,
  1136. snd_pcm_hw_rule_pow2, NULL,
  1137. var, -1);
  1138. }
  1139. EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
  1140. static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
  1141. snd_pcm_hw_param_t var)
  1142. {
  1143. if (hw_is_mask(var)) {
  1144. snd_mask_any(hw_param_mask(params, var));
  1145. params->cmask |= 1 << var;
  1146. params->rmask |= 1 << var;
  1147. return;
  1148. }
  1149. if (hw_is_interval(var)) {
  1150. snd_interval_any(hw_param_interval(params, var));
  1151. params->cmask |= 1 << var;
  1152. params->rmask |= 1 << var;
  1153. return;
  1154. }
  1155. snd_BUG();
  1156. }
  1157. void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
  1158. {
  1159. unsigned int k;
  1160. memset(params, 0, sizeof(*params));
  1161. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
  1162. _snd_pcm_hw_param_any(params, k);
  1163. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
  1164. _snd_pcm_hw_param_any(params, k);
  1165. params->info = ~0U;
  1166. }
  1167. EXPORT_SYMBOL(_snd_pcm_hw_params_any);
  1168. /**
  1169. * snd_pcm_hw_param_value - return @params field @var value
  1170. * @params: the hw_params instance
  1171. * @var: parameter to retrieve
  1172. * @dir: pointer to the direction (-1,0,1) or %NULL
  1173. *
  1174. * Return the value for field @var if it's fixed in configuration space
  1175. * defined by @params. Return -%EINVAL otherwise.
  1176. */
  1177. int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
  1178. snd_pcm_hw_param_t var, int *dir)
  1179. {
  1180. if (hw_is_mask(var)) {
  1181. const struct snd_mask *mask = hw_param_mask_c(params, var);
  1182. if (!snd_mask_single(mask))
  1183. return -EINVAL;
  1184. if (dir)
  1185. *dir = 0;
  1186. return snd_mask_value(mask);
  1187. }
  1188. if (hw_is_interval(var)) {
  1189. const struct snd_interval *i = hw_param_interval_c(params, var);
  1190. if (!snd_interval_single(i))
  1191. return -EINVAL;
  1192. if (dir)
  1193. *dir = i->openmin;
  1194. return snd_interval_value(i);
  1195. }
  1196. return -EINVAL;
  1197. }
  1198. EXPORT_SYMBOL(snd_pcm_hw_param_value);
  1199. void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
  1200. snd_pcm_hw_param_t var)
  1201. {
  1202. if (hw_is_mask(var)) {
  1203. snd_mask_none(hw_param_mask(params, var));
  1204. params->cmask |= 1 << var;
  1205. params->rmask |= 1 << var;
  1206. } else if (hw_is_interval(var)) {
  1207. snd_interval_none(hw_param_interval(params, var));
  1208. params->cmask |= 1 << var;
  1209. params->rmask |= 1 << var;
  1210. } else {
  1211. snd_BUG();
  1212. }
  1213. }
  1214. EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
  1215. static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
  1216. snd_pcm_hw_param_t var)
  1217. {
  1218. int changed;
  1219. if (hw_is_mask(var))
  1220. changed = snd_mask_refine_first(hw_param_mask(params, var));
  1221. else if (hw_is_interval(var))
  1222. changed = snd_interval_refine_first(hw_param_interval(params, var));
  1223. else
  1224. return -EINVAL;
  1225. if (changed) {
  1226. params->cmask |= 1 << var;
  1227. params->rmask |= 1 << var;
  1228. }
  1229. return changed;
  1230. }
  1231. /**
  1232. * snd_pcm_hw_param_first - refine config space and return minimum value
  1233. * @pcm: PCM instance
  1234. * @params: the hw_params instance
  1235. * @var: parameter to retrieve
  1236. * @dir: pointer to the direction (-1,0,1) or %NULL
  1237. *
  1238. * Inside configuration space defined by @params remove from @var all
  1239. * values > minimum. Reduce configuration space accordingly.
  1240. * Return the minimum.
  1241. */
  1242. int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
  1243. struct snd_pcm_hw_params *params,
  1244. snd_pcm_hw_param_t var, int *dir)
  1245. {
  1246. int changed = _snd_pcm_hw_param_first(params, var);
  1247. if (changed < 0)
  1248. return changed;
  1249. if (params->rmask) {
  1250. int err = snd_pcm_hw_refine(pcm, params);
  1251. if (snd_BUG_ON(err < 0))
  1252. return err;
  1253. }
  1254. return snd_pcm_hw_param_value(params, var, dir);
  1255. }
  1256. EXPORT_SYMBOL(snd_pcm_hw_param_first);
  1257. static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
  1258. snd_pcm_hw_param_t var)
  1259. {
  1260. int changed;
  1261. if (hw_is_mask(var))
  1262. changed = snd_mask_refine_last(hw_param_mask(params, var));
  1263. else if (hw_is_interval(var))
  1264. changed = snd_interval_refine_last(hw_param_interval(params, var));
  1265. else
  1266. return -EINVAL;
  1267. if (changed) {
  1268. params->cmask |= 1 << var;
  1269. params->rmask |= 1 << var;
  1270. }
  1271. return changed;
  1272. }
  1273. /**
  1274. * snd_pcm_hw_param_last - refine config space and return maximum value
  1275. * @pcm: PCM instance
  1276. * @params: the hw_params instance
  1277. * @var: parameter to retrieve
  1278. * @dir: pointer to the direction (-1,0,1) or %NULL
  1279. *
  1280. * Inside configuration space defined by @params remove from @var all
  1281. * values < maximum. Reduce configuration space accordingly.
  1282. * Return the maximum.
  1283. */
  1284. int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
  1285. struct snd_pcm_hw_params *params,
  1286. snd_pcm_hw_param_t var, int *dir)
  1287. {
  1288. int changed = _snd_pcm_hw_param_last(params, var);
  1289. if (changed < 0)
  1290. return changed;
  1291. if (params->rmask) {
  1292. int err = snd_pcm_hw_refine(pcm, params);
  1293. if (snd_BUG_ON(err < 0))
  1294. return err;
  1295. }
  1296. return snd_pcm_hw_param_value(params, var, dir);
  1297. }
  1298. EXPORT_SYMBOL(snd_pcm_hw_param_last);
  1299. /**
  1300. * snd_pcm_hw_param_choose - choose a configuration defined by @params
  1301. * @pcm: PCM instance
  1302. * @params: the hw_params instance
  1303. *
  1304. * Choose one configuration from configuration space defined by @params.
  1305. * The configuration chosen is that obtained fixing in this order:
  1306. * first access, first format, first subformat, min channels,
  1307. * min rate, min period time, max buffer size, min tick time
  1308. */
  1309. int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
  1310. struct snd_pcm_hw_params *params)
  1311. {
  1312. static int vars[] = {
  1313. SNDRV_PCM_HW_PARAM_ACCESS,
  1314. SNDRV_PCM_HW_PARAM_FORMAT,
  1315. SNDRV_PCM_HW_PARAM_SUBFORMAT,
  1316. SNDRV_PCM_HW_PARAM_CHANNELS,
  1317. SNDRV_PCM_HW_PARAM_RATE,
  1318. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1319. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1320. SNDRV_PCM_HW_PARAM_TICK_TIME,
  1321. -1
  1322. };
  1323. int err, *v;
  1324. for (v = vars; *v != -1; v++) {
  1325. if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
  1326. err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
  1327. else
  1328. err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
  1329. if (snd_BUG_ON(err < 0))
  1330. return err;
  1331. }
  1332. return 0;
  1333. }
  1334. static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
  1335. void *arg)
  1336. {
  1337. struct snd_pcm_runtime *runtime = substream->runtime;
  1338. unsigned long flags;
  1339. snd_pcm_stream_lock_irqsave(substream, flags);
  1340. if (snd_pcm_running(substream) &&
  1341. snd_pcm_update_hw_ptr(substream) >= 0)
  1342. runtime->status->hw_ptr %= runtime->buffer_size;
  1343. else
  1344. runtime->status->hw_ptr = 0;
  1345. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1346. return 0;
  1347. }
  1348. static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
  1349. void *arg)
  1350. {
  1351. struct snd_pcm_channel_info *info = arg;
  1352. struct snd_pcm_runtime *runtime = substream->runtime;
  1353. int width;
  1354. if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
  1355. info->offset = -1;
  1356. return 0;
  1357. }
  1358. width = snd_pcm_format_physical_width(runtime->format);
  1359. if (width < 0)
  1360. return width;
  1361. info->offset = 0;
  1362. switch (runtime->access) {
  1363. case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
  1364. case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
  1365. info->first = info->channel * width;
  1366. info->step = runtime->channels * width;
  1367. break;
  1368. case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
  1369. case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
  1370. {
  1371. size_t size = runtime->dma_bytes / runtime->channels;
  1372. info->first = info->channel * size * 8;
  1373. info->step = width;
  1374. break;
  1375. }
  1376. default:
  1377. snd_BUG();
  1378. break;
  1379. }
  1380. return 0;
  1381. }
  1382. /**
  1383. * snd_pcm_lib_ioctl - a generic PCM ioctl callback
  1384. * @substream: the pcm substream instance
  1385. * @cmd: ioctl command
  1386. * @arg: ioctl argument
  1387. *
  1388. * Processes the generic ioctl commands for PCM.
  1389. * Can be passed as the ioctl callback for PCM ops.
  1390. *
  1391. * Returns zero if successful, or a negative error code on failure.
  1392. */
  1393. int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  1394. unsigned int cmd, void *arg)
  1395. {
  1396. switch (cmd) {
  1397. case SNDRV_PCM_IOCTL1_INFO:
  1398. return 0;
  1399. case SNDRV_PCM_IOCTL1_RESET:
  1400. return snd_pcm_lib_ioctl_reset(substream, arg);
  1401. case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
  1402. return snd_pcm_lib_ioctl_channel_info(substream, arg);
  1403. }
  1404. return -ENXIO;
  1405. }
  1406. EXPORT_SYMBOL(snd_pcm_lib_ioctl);
  1407. /**
  1408. * snd_pcm_period_elapsed - update the pcm status for the next period
  1409. * @substream: the pcm substream instance
  1410. *
  1411. * This function is called from the interrupt handler when the
  1412. * PCM has processed the period size. It will update the current
  1413. * pointer, wake up sleepers, etc.
  1414. *
  1415. * Even if more than one periods have elapsed since the last call, you
  1416. * have to call this only once.
  1417. */
  1418. void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
  1419. {
  1420. struct snd_pcm_runtime *runtime;
  1421. unsigned long flags;
  1422. if (PCM_RUNTIME_CHECK(substream))
  1423. return;
  1424. runtime = substream->runtime;
  1425. if (runtime->transfer_ack_begin)
  1426. runtime->transfer_ack_begin(substream);
  1427. snd_pcm_stream_lock_irqsave(substream, flags);
  1428. if (!snd_pcm_running(substream) ||
  1429. snd_pcm_update_hw_ptr_interrupt(substream) < 0)
  1430. goto _end;
  1431. if (substream->timer_running)
  1432. snd_timer_interrupt(substream->timer, 1);
  1433. _end:
  1434. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1435. if (runtime->transfer_ack_end)
  1436. runtime->transfer_ack_end(substream);
  1437. kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
  1438. }
  1439. EXPORT_SYMBOL(snd_pcm_period_elapsed);
  1440. /*
  1441. * Wait until avail_min data becomes available
  1442. * Returns a negative error code if any error occurs during operation.
  1443. * The available space is stored on availp. When err = 0 and avail = 0
  1444. * on the capture stream, it indicates the stream is in DRAINING state.
  1445. */
  1446. static int wait_for_avail_min(struct snd_pcm_substream *substream,
  1447. snd_pcm_uframes_t *availp)
  1448. {
  1449. struct snd_pcm_runtime *runtime = substream->runtime;
  1450. int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  1451. wait_queue_t wait;
  1452. int err = 0;
  1453. snd_pcm_uframes_t avail = 0;
  1454. long tout;
  1455. init_waitqueue_entry(&wait, current);
  1456. add_wait_queue(&runtime->sleep, &wait);
  1457. for (;;) {
  1458. if (signal_pending(current)) {
  1459. err = -ERESTARTSYS;
  1460. break;
  1461. }
  1462. set_current_state(TASK_INTERRUPTIBLE);
  1463. snd_pcm_stream_unlock_irq(substream);
  1464. tout = schedule_timeout(msecs_to_jiffies(10000));
  1465. snd_pcm_stream_lock_irq(substream);
  1466. switch (runtime->status->state) {
  1467. case SNDRV_PCM_STATE_SUSPENDED:
  1468. err = -ESTRPIPE;
  1469. goto _endloop;
  1470. case SNDRV_PCM_STATE_XRUN:
  1471. err = -EPIPE;
  1472. goto _endloop;
  1473. case SNDRV_PCM_STATE_DRAINING:
  1474. if (is_playback)
  1475. err = -EPIPE;
  1476. else
  1477. avail = 0; /* indicate draining */
  1478. goto _endloop;
  1479. case SNDRV_PCM_STATE_OPEN:
  1480. case SNDRV_PCM_STATE_SETUP:
  1481. case SNDRV_PCM_STATE_DISCONNECTED:
  1482. err = -EBADFD;
  1483. goto _endloop;
  1484. }
  1485. if (!tout) {
  1486. snd_printd("%s write error (DMA or IRQ trouble?)\n",
  1487. is_playback ? "playback" : "capture");
  1488. err = -EIO;
  1489. break;
  1490. }
  1491. if (is_playback)
  1492. avail = snd_pcm_playback_avail(runtime);
  1493. else
  1494. avail = snd_pcm_capture_avail(runtime);
  1495. if (avail >= runtime->control->avail_min)
  1496. break;
  1497. }
  1498. _endloop:
  1499. remove_wait_queue(&runtime->sleep, &wait);
  1500. *availp = avail;
  1501. return err;
  1502. }
  1503. static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
  1504. unsigned int hwoff,
  1505. unsigned long data, unsigned int off,
  1506. snd_pcm_uframes_t frames)
  1507. {
  1508. struct snd_pcm_runtime *runtime = substream->runtime;
  1509. int err;
  1510. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  1511. if (substream->ops->copy) {
  1512. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  1513. return err;
  1514. } else {
  1515. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff)
  1516. + (ring_buf_index * frames_to_bytes(runtime, runtime->buffer_size));
  1517. if(frames == runtime->buffer_size)
  1518. ring_buf_index = (ring_buf_index + 1) % ANDROID_BUF_NUM;
  1519. else if(frames != runtime->buffer_size) {
  1520. period_index += frames;
  1521. if(period_index >= runtime->buffer_size) {
  1522. ring_buf_index = (ring_buf_index + 1) % ANDROID_BUF_NUM;
  1523. period_index -= runtime->buffer_size;
  1524. }
  1525. }
  1526. if (snd_BUG_ON(runtime->dma_area)) return -EFAULT;
  1527. // printk("########### frames = %d #########\n", frames);
  1528. if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
  1529. return -EFAULT;
  1530. }
  1531. return 0;
  1532. }
  1533. typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
  1534. unsigned long data, unsigned int off,
  1535. snd_pcm_uframes_t size);
  1536. static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
  1537. unsigned long data,
  1538. snd_pcm_uframes_t size,
  1539. int nonblock,
  1540. transfer_f transfer)
  1541. {
  1542. struct snd_pcm_runtime *runtime = substream->runtime;
  1543. snd_pcm_uframes_t xfer = 0;
  1544. snd_pcm_uframes_t offset = 0;
  1545. int err = 0;
  1546. if (size == 0)
  1547. return 0;
  1548. snd_pcm_stream_lock_irq(substream);
  1549. switch (runtime->status->state) {
  1550. case SNDRV_PCM_STATE_PREPARED:
  1551. case SNDRV_PCM_STATE_RUNNING:
  1552. case SNDRV_PCM_STATE_PAUSED:
  1553. break;
  1554. case SNDRV_PCM_STATE_XRUN:
  1555. err = -EPIPE;
  1556. goto _end_unlock;
  1557. case SNDRV_PCM_STATE_SUSPENDED:
  1558. err = -ESTRPIPE;
  1559. goto _end_unlock;
  1560. default:
  1561. err = -EBADFD;
  1562. goto _end_unlock;
  1563. }
  1564. while (size > 0) {
  1565. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  1566. snd_pcm_uframes_t avail;
  1567. snd_pcm_uframes_t cont;
  1568. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1569. snd_pcm_update_hw_ptr(substream);
  1570. avail = snd_pcm_playback_avail(runtime);
  1571. if (!avail) {
  1572. if (nonblock) {
  1573. err = -EAGAIN;
  1574. goto _end_unlock;
  1575. }
  1576. err = wait_for_avail_min(substream, &avail);
  1577. if (err < 0)
  1578. goto _end_unlock;
  1579. }
  1580. if(avail > runtime->buffer_size)
  1581. avail = runtime->buffer_size;
  1582. frames = size > avail ? avail : size;
  1583. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  1584. if (frames > cont)
  1585. frames = cont;
  1586. if (snd_BUG_ON(!frames)) {
  1587. snd_pcm_stream_unlock_irq(substream);
  1588. return -EINVAL;
  1589. }
  1590. appl_ptr = runtime->control->appl_ptr;
  1591. appl_ofs = appl_ptr % runtime->buffer_size;
  1592. snd_pcm_stream_unlock_irq(substream);
  1593. if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
  1594. goto _end;
  1595. snd_pcm_stream_lock_irq(substream);
  1596. switch (runtime->status->state) {
  1597. case SNDRV_PCM_STATE_XRUN:
  1598. err = -EPIPE;
  1599. goto _end_unlock;
  1600. case SNDRV_PCM_STATE_SUSPENDED:
  1601. err = -ESTRPIPE;
  1602. goto _end_unlock;
  1603. default:
  1604. break;
  1605. }
  1606. appl_ptr += frames;
  1607. if (appl_ptr >= runtime->boundary)
  1608. appl_ptr -= runtime->boundary;
  1609. runtime->control->appl_ptr = appl_ptr;
  1610. if (substream->ops->ack)
  1611. substream->ops->ack(substream);
  1612. offset += frames;
  1613. size -= frames;
  1614. xfer += frames;
  1615. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
  1616. snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
  1617. err = snd_pcm_start(substream);
  1618. if (err < 0)
  1619. goto _end_unlock;
  1620. }
  1621. }
  1622. _end_unlock:
  1623. snd_pcm_stream_unlock_irq(substream);
  1624. _end:
  1625. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  1626. }
  1627. /* sanity-check for read/write methods */
  1628. static int pcm_sanity_check(struct snd_pcm_substream *substream)
  1629. {
  1630. struct snd_pcm_runtime *runtime;
  1631. if (PCM_RUNTIME_CHECK(substream))
  1632. return -ENXIO;
  1633. runtime = substream->runtime;
  1634. if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
  1635. return -EINVAL;
  1636. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1637. return -EBADFD;
  1638. return 0;
  1639. }
  1640. snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
  1641. {
  1642. struct snd_pcm_runtime *runtime;
  1643. int nonblock;
  1644. int err;
  1645. err = pcm_sanity_check(substream);
  1646. if (err < 0)
  1647. return err;
  1648. runtime = substream->runtime;
  1649. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1650. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
  1651. runtime->channels > 1)
  1652. return -EINVAL;
  1653. return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
  1654. snd_pcm_lib_write_transfer);
  1655. }
  1656. EXPORT_SYMBOL(snd_pcm_lib_write);
  1657. static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
  1658. unsigned int hwoff,
  1659. unsigned long data, unsigned int off,
  1660. snd_pcm_uframes_t frames)
  1661. {
  1662. struct snd_pcm_runtime *runtime = substream->runtime;
  1663. int err;
  1664. void __user **bufs = (void __user **)data;
  1665. int channels = runtime->channels;
  1666. int c;
  1667. if (substream->ops->copy) {
  1668. if (snd_BUG_ON(!substream->ops->silence))
  1669. return -EINVAL;
  1670. for (c = 0; c < channels; ++c, ++bufs) {
  1671. if (*bufs == NULL) {
  1672. if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
  1673. return err;
  1674. } else {
  1675. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  1676. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  1677. return err;
  1678. }
  1679. }
  1680. } else {
  1681. /* default transfer behaviour */
  1682. size_t dma_csize = runtime->dma_bytes / channels;
  1683. for (c = 0; c < channels; ++c, ++bufs) {
  1684. char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  1685. if (*bufs == NULL) {
  1686. snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
  1687. } else {
  1688. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  1689. if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
  1690. return -EFAULT;
  1691. }
  1692. }
  1693. }
  1694. return 0;
  1695. }
  1696. snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *subst