PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/sound/core/pcm_lib.c

https://bitbucket.org/abioy/linux
C | 2182 lines | 1726 code | 158 blank | 298 comment | 394 complexity | 65181e6cbb374303cf1f382f97ca907f MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  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 <linux/math64.h>
  25. #include <sound/core.h>
  26. #include <sound/control.h>
  27. #include <sound/info.h>
  28. #include <sound/pcm.h>
  29. #include <sound/pcm_params.h>
  30. #include <sound/timer.h>
  31. /*
  32. * fill ring buffer with silence
  33. * runtime->silence_start: starting pointer to silence area
  34. * runtime->silence_filled: size filled with silence
  35. * runtime->silence_threshold: threshold from application
  36. * runtime->silence_size: maximal size from application
  37. *
  38. * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
  39. */
  40. void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
  41. {
  42. struct snd_pcm_runtime *runtime = substream->runtime;
  43. snd_pcm_uframes_t frames, ofs, transfer;
  44. if (runtime->silence_size < runtime->boundary) {
  45. snd_pcm_sframes_t noise_dist, n;
  46. if (runtime->silence_start != runtime->control->appl_ptr) {
  47. n = runtime->control->appl_ptr - runtime->silence_start;
  48. if (n < 0)
  49. n += runtime->boundary;
  50. if ((snd_pcm_uframes_t)n < runtime->silence_filled)
  51. runtime->silence_filled -= n;
  52. else
  53. runtime->silence_filled = 0;
  54. runtime->silence_start = runtime->control->appl_ptr;
  55. }
  56. if (runtime->silence_filled >= runtime->buffer_size)
  57. return;
  58. noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
  59. if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
  60. return;
  61. frames = runtime->silence_threshold - noise_dist;
  62. if (frames > runtime->silence_size)
  63. frames = runtime->silence_size;
  64. } else {
  65. if (new_hw_ptr == ULONG_MAX) { /* initialization */
  66. snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
  67. runtime->silence_filled = avail > 0 ? avail : 0;
  68. runtime->silence_start = (runtime->status->hw_ptr +
  69. runtime->silence_filled) %
  70. runtime->boundary;
  71. } else {
  72. ofs = runtime->status->hw_ptr;
  73. frames = new_hw_ptr - ofs;
  74. if ((snd_pcm_sframes_t)frames < 0)
  75. frames += runtime->boundary;
  76. runtime->silence_filled -= frames;
  77. if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
  78. runtime->silence_filled = 0;
  79. runtime->silence_start = new_hw_ptr;
  80. } else {
  81. runtime->silence_start = ofs;
  82. }
  83. }
  84. frames = runtime->buffer_size - runtime->silence_filled;
  85. }
  86. if (snd_BUG_ON(frames > runtime->buffer_size))
  87. return;
  88. if (frames == 0)
  89. return;
  90. ofs = runtime->silence_start % runtime->buffer_size;
  91. while (frames > 0) {
  92. transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
  93. if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
  94. runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
  95. if (substream->ops->silence) {
  96. int err;
  97. err = substream->ops->silence(substream, -1, ofs, transfer);
  98. snd_BUG_ON(err < 0);
  99. } else {
  100. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
  101. snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
  102. }
  103. } else {
  104. unsigned int c;
  105. unsigned int channels = runtime->channels;
  106. if (substream->ops->silence) {
  107. for (c = 0; c < channels; ++c) {
  108. int err;
  109. err = substream->ops->silence(substream, c, ofs, transfer);
  110. snd_BUG_ON(err < 0);
  111. }
  112. } else {
  113. size_t dma_csize = runtime->dma_bytes / channels;
  114. for (c = 0; c < channels; ++c) {
  115. char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
  116. snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
  117. }
  118. }
  119. }
  120. runtime->silence_filled += transfer;
  121. frames -= transfer;
  122. ofs = 0;
  123. }
  124. }
  125. static void pcm_debug_name(struct snd_pcm_substream *substream,
  126. char *name, size_t len)
  127. {
  128. snprintf(name, len, "pcmC%dD%d%c:%d",
  129. substream->pcm->card->number,
  130. substream->pcm->device,
  131. substream->stream ? 'c' : 'p',
  132. substream->number);
  133. }
  134. #define XRUN_DEBUG_BASIC (1<<0)
  135. #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
  136. #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
  137. #define XRUN_DEBUG_PERIODUPDATE (1<<3) /* full period update info */
  138. #define XRUN_DEBUG_HWPTRUPDATE (1<<4) /* full hwptr update info */
  139. #define XRUN_DEBUG_LOG (1<<5) /* show last 10 positions on err */
  140. #define XRUN_DEBUG_LOGONCE (1<<6) /* do above only once */
  141. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  142. #define xrun_debug(substream, mask) \
  143. ((substream)->pstr->xrun_debug & (mask))
  144. #else
  145. #define xrun_debug(substream, mask) 0
  146. #endif
  147. #define dump_stack_on_xrun(substream) do { \
  148. if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
  149. dump_stack(); \
  150. } while (0)
  151. static void xrun(struct snd_pcm_substream *substream)
  152. {
  153. struct snd_pcm_runtime *runtime = substream->runtime;
  154. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
  155. snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
  156. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  157. if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
  158. char name[16];
  159. pcm_debug_name(substream, name, sizeof(name));
  160. snd_printd(KERN_DEBUG "XRUN: %s\n", name);
  161. dump_stack_on_xrun(substream);
  162. }
  163. }
  164. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  165. #define hw_ptr_error(substream, fmt, args...) \
  166. do { \
  167. if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
  168. xrun_log_show(substream); \
  169. if (printk_ratelimit()) { \
  170. snd_printd("PCM: " fmt, ##args); \
  171. } \
  172. dump_stack_on_xrun(substream); \
  173. } \
  174. } while (0)
  175. #define XRUN_LOG_CNT 10
  176. struct hwptr_log_entry {
  177. unsigned long jiffies;
  178. snd_pcm_uframes_t pos;
  179. snd_pcm_uframes_t period_size;
  180. snd_pcm_uframes_t buffer_size;
  181. snd_pcm_uframes_t old_hw_ptr;
  182. snd_pcm_uframes_t hw_ptr_base;
  183. };
  184. struct snd_pcm_hwptr_log {
  185. unsigned int idx;
  186. unsigned int hit: 1;
  187. struct hwptr_log_entry entries[XRUN_LOG_CNT];
  188. };
  189. static void xrun_log(struct snd_pcm_substream *substream,
  190. snd_pcm_uframes_t pos)
  191. {
  192. struct snd_pcm_runtime *runtime = substream->runtime;
  193. struct snd_pcm_hwptr_log *log = runtime->hwptr_log;
  194. struct hwptr_log_entry *entry;
  195. if (log == NULL) {
  196. log = kzalloc(sizeof(*log), GFP_ATOMIC);
  197. if (log == NULL)
  198. return;
  199. runtime->hwptr_log = log;
  200. } else {
  201. if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
  202. return;
  203. }
  204. entry = &log->entries[log->idx];
  205. entry->jiffies = jiffies;
  206. entry->pos = pos;
  207. entry->period_size = runtime->period_size;
  208. entry->buffer_size = runtime->buffer_size;;
  209. entry->old_hw_ptr = runtime->status->hw_ptr;
  210. entry->hw_ptr_base = runtime->hw_ptr_base;
  211. log->idx = (log->idx + 1) % XRUN_LOG_CNT;
  212. }
  213. static void xrun_log_show(struct snd_pcm_substream *substream)
  214. {
  215. struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log;
  216. struct hwptr_log_entry *entry;
  217. char name[16];
  218. unsigned int idx;
  219. int cnt;
  220. if (log == NULL)
  221. return;
  222. if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
  223. return;
  224. pcm_debug_name(substream, name, sizeof(name));
  225. for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) {
  226. entry = &log->entries[idx];
  227. if (entry->period_size == 0)
  228. break;
  229. snd_printd("hwptr log: %s: j=%lu, pos=%ld/%ld/%ld, "
  230. "hwptr=%ld/%ld\n",
  231. name, entry->jiffies, (unsigned long)entry->pos,
  232. (unsigned long)entry->period_size,
  233. (unsigned long)entry->buffer_size,
  234. (unsigned long)entry->old_hw_ptr,
  235. (unsigned long)entry->hw_ptr_base);
  236. idx++;
  237. idx %= XRUN_LOG_CNT;
  238. }
  239. log->hit = 1;
  240. }
  241. #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
  242. #define hw_ptr_error(substream, fmt, args...) do { } while (0)
  243. #define xrun_log(substream, pos) do { } while (0)
  244. #define xrun_log_show(substream) do { } while (0)
  245. #endif
  246. int snd_pcm_update_state(struct snd_pcm_substream *substream,
  247. struct snd_pcm_runtime *runtime)
  248. {
  249. snd_pcm_uframes_t avail;
  250. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  251. avail = snd_pcm_playback_avail(runtime);
  252. else
  253. avail = snd_pcm_capture_avail(runtime);
  254. if (avail > runtime->avail_max)
  255. runtime->avail_max = avail;
  256. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  257. if (avail >= runtime->buffer_size) {
  258. snd_pcm_drain_done(substream);
  259. return -EPIPE;
  260. }
  261. } else {
  262. if (avail >= runtime->stop_threshold) {
  263. xrun(substream);
  264. return -EPIPE;
  265. }
  266. }
  267. if (avail >= runtime->control->avail_min)
  268. wake_up(runtime->twake ? &runtime->tsleep : &runtime->sleep);
  269. return 0;
  270. }
  271. static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
  272. unsigned int in_interrupt)
  273. {
  274. struct snd_pcm_runtime *runtime = substream->runtime;
  275. snd_pcm_uframes_t pos;
  276. snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
  277. snd_pcm_sframes_t hdelta, delta;
  278. unsigned long jdelta;
  279. old_hw_ptr = runtime->status->hw_ptr;
  280. pos = substream->ops->pointer(substream);
  281. if (pos == SNDRV_PCM_POS_XRUN) {
  282. xrun(substream);
  283. return -EPIPE;
  284. }
  285. if (pos >= runtime->buffer_size) {
  286. if (printk_ratelimit()) {
  287. char name[16];
  288. pcm_debug_name(substream, name, sizeof(name));
  289. xrun_log_show(substream);
  290. snd_printd(KERN_ERR "BUG: %s, pos = %ld, "
  291. "buffer size = %ld, period size = %ld\n",
  292. name, pos, runtime->buffer_size,
  293. runtime->period_size);
  294. }
  295. pos = 0;
  296. }
  297. pos -= pos % runtime->min_align;
  298. if (xrun_debug(substream, XRUN_DEBUG_LOG))
  299. xrun_log(substream, pos);
  300. hw_base = runtime->hw_ptr_base;
  301. new_hw_ptr = hw_base + pos;
  302. if (in_interrupt) {
  303. /* we know that one period was processed */
  304. /* delta = "expected next hw_ptr" for in_interrupt != 0 */
  305. delta = runtime->hw_ptr_interrupt + runtime->period_size;
  306. if (delta > new_hw_ptr) {
  307. hw_base += runtime->buffer_size;
  308. if (hw_base >= runtime->boundary)
  309. hw_base = 0;
  310. new_hw_ptr = hw_base + pos;
  311. goto __delta;
  312. }
  313. }
  314. /* new_hw_ptr might be lower than old_hw_ptr in case when */
  315. /* pointer crosses the end of the ring buffer */
  316. if (new_hw_ptr < old_hw_ptr) {
  317. hw_base += runtime->buffer_size;
  318. if (hw_base >= runtime->boundary)
  319. hw_base = 0;
  320. new_hw_ptr = hw_base + pos;
  321. }
  322. __delta:
  323. delta = (new_hw_ptr - old_hw_ptr) % runtime->boundary;
  324. if (xrun_debug(substream, in_interrupt ?
  325. XRUN_DEBUG_PERIODUPDATE : XRUN_DEBUG_HWPTRUPDATE)) {
  326. char name[16];
  327. pcm_debug_name(substream, name, sizeof(name));
  328. snd_printd("%s_update: %s: pos=%u/%u/%u, "
  329. "hwptr=%ld/%ld/%ld/%ld\n",
  330. in_interrupt ? "period" : "hwptr",
  331. name,
  332. (unsigned int)pos,
  333. (unsigned int)runtime->period_size,
  334. (unsigned int)runtime->buffer_size,
  335. (unsigned long)delta,
  336. (unsigned long)old_hw_ptr,
  337. (unsigned long)new_hw_ptr,
  338. (unsigned long)runtime->hw_ptr_base);
  339. }
  340. /* something must be really wrong */
  341. if (delta >= runtime->buffer_size + runtime->period_size) {
  342. hw_ptr_error(substream,
  343. "Unexpected hw_pointer value %s"
  344. "(stream=%i, pos=%ld, new_hw_ptr=%ld, "
  345. "old_hw_ptr=%ld)\n",
  346. in_interrupt ? "[Q] " : "[P]",
  347. substream->stream, (long)pos,
  348. (long)new_hw_ptr, (long)old_hw_ptr);
  349. return 0;
  350. }
  351. /* Do jiffies check only in xrun_debug mode */
  352. if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
  353. goto no_jiffies_check;
  354. /* Skip the jiffies check for hardwares with BATCH flag.
  355. * Such hardware usually just increases the position at each IRQ,
  356. * thus it can't give any strange position.
  357. */
  358. if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
  359. goto no_jiffies_check;
  360. hdelta = delta;
  361. if (hdelta < runtime->delay)
  362. goto no_jiffies_check;
  363. hdelta -= runtime->delay;
  364. jdelta = jiffies - runtime->hw_ptr_jiffies;
  365. if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
  366. delta = jdelta /
  367. (((runtime->period_size * HZ) / runtime->rate)
  368. + HZ/100);
  369. /* move new_hw_ptr according jiffies not pos variable */
  370. new_hw_ptr = old_hw_ptr;
  371. hw_base = delta;
  372. /* use loop to avoid checks for delta overflows */
  373. /* the delta value is small or zero in most cases */
  374. while (delta > 0) {
  375. new_hw_ptr += runtime->period_size;
  376. if (new_hw_ptr >= runtime->boundary)
  377. new_hw_ptr -= runtime->boundary;
  378. delta--;
  379. }
  380. /* align hw_base to buffer_size */
  381. hw_ptr_error(substream,
  382. "hw_ptr skipping! %s"
  383. "(pos=%ld, delta=%ld, period=%ld, "
  384. "jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
  385. in_interrupt ? "[Q] " : "",
  386. (long)pos, (long)hdelta,
  387. (long)runtime->period_size, jdelta,
  388. ((hdelta * HZ) / runtime->rate), hw_base,
  389. (unsigned long)old_hw_ptr,
  390. (unsigned long)new_hw_ptr);
  391. /* reset values to proper state */
  392. delta = 0;
  393. hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
  394. }
  395. no_jiffies_check:
  396. if (delta > runtime->period_size + runtime->period_size / 2) {
  397. hw_ptr_error(substream,
  398. "Lost interrupts? %s"
  399. "(stream=%i, delta=%ld, new_hw_ptr=%ld, "
  400. "old_hw_ptr=%ld)\n",
  401. in_interrupt ? "[Q] " : "",
  402. substream->stream, (long)delta,
  403. (long)new_hw_ptr,
  404. (long)old_hw_ptr);
  405. }
  406. if (runtime->status->hw_ptr == new_hw_ptr)
  407. return 0;
  408. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  409. runtime->silence_size > 0)
  410. snd_pcm_playback_silence(substream, new_hw_ptr);
  411. if (in_interrupt) {
  412. runtime->hw_ptr_interrupt = new_hw_ptr -
  413. (new_hw_ptr % runtime->period_size);
  414. }
  415. runtime->hw_ptr_base = hw_base;
  416. runtime->status->hw_ptr = new_hw_ptr;
  417. runtime->hw_ptr_jiffies = jiffies;
  418. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
  419. snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
  420. return snd_pcm_update_state(substream, runtime);
  421. }
  422. /* CAUTION: call it with irq disabled */
  423. int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
  424. {
  425. return snd_pcm_update_hw_ptr0(substream, 0);
  426. }
  427. /**
  428. * snd_pcm_set_ops - set the PCM operators
  429. * @pcm: the pcm instance
  430. * @direction: stream direction, SNDRV_PCM_STREAM_XXX
  431. * @ops: the operator table
  432. *
  433. * Sets the given PCM operators to the pcm instance.
  434. */
  435. void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
  436. {
  437. struct snd_pcm_str *stream = &pcm->streams[direction];
  438. struct snd_pcm_substream *substream;
  439. for (substream = stream->substream; substream != NULL; substream = substream->next)
  440. substream->ops = ops;
  441. }
  442. EXPORT_SYMBOL(snd_pcm_set_ops);
  443. /**
  444. * snd_pcm_sync - set the PCM sync id
  445. * @substream: the pcm substream
  446. *
  447. * Sets the PCM sync identifier for the card.
  448. */
  449. void snd_pcm_set_sync(struct snd_pcm_substream *substream)
  450. {
  451. struct snd_pcm_runtime *runtime = substream->runtime;
  452. runtime->sync.id32[0] = substream->pcm->card->number;
  453. runtime->sync.id32[1] = -1;
  454. runtime->sync.id32[2] = -1;
  455. runtime->sync.id32[3] = -1;
  456. }
  457. EXPORT_SYMBOL(snd_pcm_set_sync);
  458. /*
  459. * Standard ioctl routine
  460. */
  461. static inline unsigned int div32(unsigned int a, unsigned int b,
  462. unsigned int *r)
  463. {
  464. if (b == 0) {
  465. *r = 0;
  466. return UINT_MAX;
  467. }
  468. *r = a % b;
  469. return a / b;
  470. }
  471. static inline unsigned int div_down(unsigned int a, unsigned int b)
  472. {
  473. if (b == 0)
  474. return UINT_MAX;
  475. return a / b;
  476. }
  477. static inline unsigned int div_up(unsigned int a, unsigned int b)
  478. {
  479. unsigned int r;
  480. unsigned int q;
  481. if (b == 0)
  482. return UINT_MAX;
  483. q = div32(a, b, &r);
  484. if (r)
  485. ++q;
  486. return q;
  487. }
  488. static inline unsigned int mul(unsigned int a, unsigned int b)
  489. {
  490. if (a == 0)
  491. return 0;
  492. if (div_down(UINT_MAX, a) < b)
  493. return UINT_MAX;
  494. return a * b;
  495. }
  496. static inline unsigned int muldiv32(unsigned int a, unsigned int b,
  497. unsigned int c, unsigned int *r)
  498. {
  499. u_int64_t n = (u_int64_t) a * b;
  500. if (c == 0) {
  501. snd_BUG_ON(!n);
  502. *r = 0;
  503. return UINT_MAX;
  504. }
  505. n = div_u64_rem(n, c, r);
  506. if (n >= UINT_MAX) {
  507. *r = 0;
  508. return UINT_MAX;
  509. }
  510. return n;
  511. }
  512. /**
  513. * snd_interval_refine - refine the interval value of configurator
  514. * @i: the interval value to refine
  515. * @v: the interval value to refer to
  516. *
  517. * Refines the interval value with the reference value.
  518. * The interval is changed to the range satisfying both intervals.
  519. * The interval status (min, max, integer, etc.) are evaluated.
  520. *
  521. * Returns non-zero if the value is changed, zero if not changed.
  522. */
  523. int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
  524. {
  525. int changed = 0;
  526. if (snd_BUG_ON(snd_interval_empty(i)))
  527. return -EINVAL;
  528. if (i->min < v->min) {
  529. i->min = v->min;
  530. i->openmin = v->openmin;
  531. changed = 1;
  532. } else if (i->min == v->min && !i->openmin && v->openmin) {
  533. i->openmin = 1;
  534. changed = 1;
  535. }
  536. if (i->max > v->max) {
  537. i->max = v->max;
  538. i->openmax = v->openmax;
  539. changed = 1;
  540. } else if (i->max == v->max && !i->openmax && v->openmax) {
  541. i->openmax = 1;
  542. changed = 1;
  543. }
  544. if (!i->integer && v->integer) {
  545. i->integer = 1;
  546. changed = 1;
  547. }
  548. if (i->integer) {
  549. if (i->openmin) {
  550. i->min++;
  551. i->openmin = 0;
  552. }
  553. if (i->openmax) {
  554. i->max--;
  555. i->openmax = 0;
  556. }
  557. } else if (!i->openmin && !i->openmax && i->min == i->max)
  558. i->integer = 1;
  559. if (snd_interval_checkempty(i)) {
  560. snd_interval_none(i);
  561. return -EINVAL;
  562. }
  563. return changed;
  564. }
  565. EXPORT_SYMBOL(snd_interval_refine);
  566. static int snd_interval_refine_first(struct snd_interval *i)
  567. {
  568. if (snd_BUG_ON(snd_interval_empty(i)))
  569. return -EINVAL;
  570. if (snd_interval_single(i))
  571. return 0;
  572. i->max = i->min;
  573. i->openmax = i->openmin;
  574. if (i->openmax)
  575. i->max++;
  576. return 1;
  577. }
  578. static int snd_interval_refine_last(struct snd_interval *i)
  579. {
  580. if (snd_BUG_ON(snd_interval_empty(i)))
  581. return -EINVAL;
  582. if (snd_interval_single(i))
  583. return 0;
  584. i->min = i->max;
  585. i->openmin = i->openmax;
  586. if (i->openmin)
  587. i->min--;
  588. return 1;
  589. }
  590. void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
  591. {
  592. if (a->empty || b->empty) {
  593. snd_interval_none(c);
  594. return;
  595. }
  596. c->empty = 0;
  597. c->min = mul(a->min, b->min);
  598. c->openmin = (a->openmin || b->openmin);
  599. c->max = mul(a->max, b->max);
  600. c->openmax = (a->openmax || b->openmax);
  601. c->integer = (a->integer && b->integer);
  602. }
  603. /**
  604. * snd_interval_div - refine the interval value with division
  605. * @a: dividend
  606. * @b: divisor
  607. * @c: quotient
  608. *
  609. * c = a / b
  610. *
  611. * Returns non-zero if the value is changed, zero if not changed.
  612. */
  613. void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
  614. {
  615. unsigned int r;
  616. if (a->empty || b->empty) {
  617. snd_interval_none(c);
  618. return;
  619. }
  620. c->empty = 0;
  621. c->min = div32(a->min, b->max, &r);
  622. c->openmin = (r || a->openmin || b->openmax);
  623. if (b->min > 0) {
  624. c->max = div32(a->max, b->min, &r);
  625. if (r) {
  626. c->max++;
  627. c->openmax = 1;
  628. } else
  629. c->openmax = (a->openmax || b->openmin);
  630. } else {
  631. c->max = UINT_MAX;
  632. c->openmax = 0;
  633. }
  634. c->integer = 0;
  635. }
  636. /**
  637. * snd_interval_muldivk - refine the interval value
  638. * @a: dividend 1
  639. * @b: dividend 2
  640. * @k: divisor (as integer)
  641. * @c: result
  642. *
  643. * c = a * b / k
  644. *
  645. * Returns non-zero if the value is changed, zero if not changed.
  646. */
  647. void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
  648. unsigned int k, struct snd_interval *c)
  649. {
  650. unsigned int r;
  651. if (a->empty || b->empty) {
  652. snd_interval_none(c);
  653. return;
  654. }
  655. c->empty = 0;
  656. c->min = muldiv32(a->min, b->min, k, &r);
  657. c->openmin = (r || a->openmin || b->openmin);
  658. c->max = muldiv32(a->max, b->max, k, &r);
  659. if (r) {
  660. c->max++;
  661. c->openmax = 1;
  662. } else
  663. c->openmax = (a->openmax || b->openmax);
  664. c->integer = 0;
  665. }
  666. /**
  667. * snd_interval_mulkdiv - refine the interval value
  668. * @a: dividend 1
  669. * @k: dividend 2 (as integer)
  670. * @b: divisor
  671. * @c: result
  672. *
  673. * c = a * k / b
  674. *
  675. * Returns non-zero if the value is changed, zero if not changed.
  676. */
  677. void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
  678. const struct snd_interval *b, struct snd_interval *c)
  679. {
  680. unsigned int r;
  681. if (a->empty || b->empty) {
  682. snd_interval_none(c);
  683. return;
  684. }
  685. c->empty = 0;
  686. c->min = muldiv32(a->min, k, b->max, &r);
  687. c->openmin = (r || a->openmin || b->openmax);
  688. if (b->min > 0) {
  689. c->max = muldiv32(a->max, k, b->min, &r);
  690. if (r) {
  691. c->max++;
  692. c->openmax = 1;
  693. } else
  694. c->openmax = (a->openmax || b->openmin);
  695. } else {
  696. c->max = UINT_MAX;
  697. c->openmax = 0;
  698. }
  699. c->integer = 0;
  700. }
  701. /* ---- */
  702. /**
  703. * snd_interval_ratnum - refine the interval value
  704. * @i: interval to refine
  705. * @rats_count: number of ratnum_t
  706. * @rats: ratnum_t array
  707. * @nump: pointer to store the resultant numerator
  708. * @denp: pointer to store the resultant denominator
  709. *
  710. * Returns non-zero if the value is changed, zero if not changed.
  711. */
  712. int snd_interval_ratnum(struct snd_interval *i,
  713. unsigned int rats_count, struct snd_ratnum *rats,
  714. unsigned int *nump, unsigned int *denp)
  715. {
  716. unsigned int best_num, best_den;
  717. int best_diff;
  718. unsigned int k;
  719. struct snd_interval t;
  720. int err;
  721. unsigned int result_num, result_den;
  722. int result_diff;
  723. best_num = best_den = best_diff = 0;
  724. for (k = 0; k < rats_count; ++k) {
  725. unsigned int num = rats[k].num;
  726. unsigned int den;
  727. unsigned int q = i->min;
  728. int diff;
  729. if (q == 0)
  730. q = 1;
  731. den = div_up(num, q);
  732. if (den < rats[k].den_min)
  733. continue;
  734. if (den > rats[k].den_max)
  735. den = rats[k].den_max;
  736. else {
  737. unsigned int r;
  738. r = (den - rats[k].den_min) % rats[k].den_step;
  739. if (r != 0)
  740. den -= r;
  741. }
  742. diff = num - q * den;
  743. if (diff < 0)
  744. diff = -diff;
  745. if (best_num == 0 ||
  746. diff * best_den < best_diff * den) {
  747. best_diff = diff;
  748. best_den = den;
  749. best_num = num;
  750. }
  751. }
  752. if (best_den == 0) {
  753. i->empty = 1;
  754. return -EINVAL;
  755. }
  756. t.min = div_down(best_num, best_den);
  757. t.openmin = !!(best_num % best_den);
  758. result_num = best_num;
  759. result_diff = best_diff;
  760. result_den = best_den;
  761. best_num = best_den = best_diff = 0;
  762. for (k = 0; k < rats_count; ++k) {
  763. unsigned int num = rats[k].num;
  764. unsigned int den;
  765. unsigned int q = i->max;
  766. int diff;
  767. if (q == 0) {
  768. i->empty = 1;
  769. return -EINVAL;
  770. }
  771. den = div_down(num, q);
  772. if (den > rats[k].den_max)
  773. continue;
  774. if (den < rats[k].den_min)
  775. den = rats[k].den_min;
  776. else {
  777. unsigned int r;
  778. r = (den - rats[k].den_min) % rats[k].den_step;
  779. if (r != 0)
  780. den += rats[k].den_step - r;
  781. }
  782. diff = q * den - num;
  783. if (diff < 0)
  784. diff = -diff;
  785. if (best_num == 0 ||
  786. diff * best_den < best_diff * den) {
  787. best_diff = diff;
  788. best_den = den;
  789. best_num = num;
  790. }
  791. }
  792. if (best_den == 0) {
  793. i->empty = 1;
  794. return -EINVAL;
  795. }
  796. t.max = div_up(best_num, best_den);
  797. t.openmax = !!(best_num % best_den);
  798. t.integer = 0;
  799. err = snd_interval_refine(i, &t);
  800. if (err < 0)
  801. return err;
  802. if (snd_interval_single(i)) {
  803. if (best_diff * result_den < result_diff * best_den) {
  804. result_num = best_num;
  805. result_den = best_den;
  806. }
  807. if (nump)
  808. *nump = result_num;
  809. if (denp)
  810. *denp = result_den;
  811. }
  812. return err;
  813. }
  814. EXPORT_SYMBOL(snd_interval_ratnum);
  815. /**
  816. * snd_interval_ratden - refine the interval value
  817. * @i: interval to refine
  818. * @rats_count: number of struct ratden
  819. * @rats: struct ratden array
  820. * @nump: pointer to store the resultant numerator
  821. * @denp: pointer to store the resultant denominator
  822. *
  823. * Returns non-zero if the value is changed, zero if not changed.
  824. */
  825. static int snd_interval_ratden(struct snd_interval *i,
  826. unsigned int rats_count, struct snd_ratden *rats,
  827. unsigned int *nump, unsigned int *denp)
  828. {
  829. unsigned int best_num, best_diff, best_den;
  830. unsigned int k;
  831. struct snd_interval t;
  832. int err;
  833. best_num = best_den = best_diff = 0;
  834. for (k = 0; k < rats_count; ++k) {
  835. unsigned int num;
  836. unsigned int den = rats[k].den;
  837. unsigned int q = i->min;
  838. int diff;
  839. num = mul(q, den);
  840. if (num > rats[k].num_max)
  841. continue;
  842. if (num < rats[k].num_min)
  843. num = rats[k].num_max;
  844. else {
  845. unsigned int r;
  846. r = (num - rats[k].num_min) % rats[k].num_step;
  847. if (r != 0)
  848. num += rats[k].num_step - r;
  849. }
  850. diff = num - q * den;
  851. if (best_num == 0 ||
  852. diff * best_den < best_diff * den) {
  853. best_diff = diff;
  854. best_den = den;
  855. best_num = num;
  856. }
  857. }
  858. if (best_den == 0) {
  859. i->empty = 1;
  860. return -EINVAL;
  861. }
  862. t.min = div_down(best_num, best_den);
  863. t.openmin = !!(best_num % best_den);
  864. best_num = best_den = best_diff = 0;
  865. for (k = 0; k < rats_count; ++k) {
  866. unsigned int num;
  867. unsigned int den = rats[k].den;
  868. unsigned int q = i->max;
  869. int diff;
  870. num = mul(q, den);
  871. if (num < rats[k].num_min)
  872. continue;
  873. if (num > rats[k].num_max)
  874. num = rats[k].num_max;
  875. else {
  876. unsigned int r;
  877. r = (num - rats[k].num_min) % rats[k].num_step;
  878. if (r != 0)
  879. num -= r;
  880. }
  881. diff = q * den - num;
  882. if (best_num == 0 ||
  883. diff * best_den < best_diff * den) {
  884. best_diff = diff;
  885. best_den = den;
  886. best_num = num;
  887. }
  888. }
  889. if (best_den == 0) {
  890. i->empty = 1;
  891. return -EINVAL;
  892. }
  893. t.max = div_up(best_num, best_den);
  894. t.openmax = !!(best_num % best_den);
  895. t.integer = 0;
  896. err = snd_interval_refine(i, &t);
  897. if (err < 0)
  898. return err;
  899. if (snd_interval_single(i)) {
  900. if (nump)
  901. *nump = best_num;
  902. if (denp)
  903. *denp = best_den;
  904. }
  905. return err;
  906. }
  907. /**
  908. * snd_interval_list - refine the interval value from the list
  909. * @i: the interval value to refine
  910. * @count: the number of elements in the list
  911. * @list: the value list
  912. * @mask: the bit-mask to evaluate
  913. *
  914. * Refines the interval value from the list.
  915. * When mask is non-zero, only the elements corresponding to bit 1 are
  916. * evaluated.
  917. *
  918. * Returns non-zero if the value is changed, zero if not changed.
  919. */
  920. int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
  921. {
  922. unsigned int k;
  923. struct snd_interval list_range;
  924. if (!count) {
  925. i->empty = 1;
  926. return -EINVAL;
  927. }
  928. snd_interval_any(&list_range);
  929. list_range.min = UINT_MAX;
  930. list_range.max = 0;
  931. for (k = 0; k < count; k++) {
  932. if (mask && !(mask & (1 << k)))
  933. continue;
  934. if (!snd_interval_test(i, list[k]))
  935. continue;
  936. list_range.min = min(list_range.min, list[k]);
  937. list_range.max = max(list_range.max, list[k]);
  938. }
  939. return snd_interval_refine(i, &list_range);
  940. }
  941. EXPORT_SYMBOL(snd_interval_list);
  942. static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
  943. {
  944. unsigned int n;
  945. int changed = 0;
  946. n = (i->min - min) % step;
  947. if (n != 0 || i->openmin) {
  948. i->min += step - n;
  949. changed = 1;
  950. }
  951. n = (i->max - min) % step;
  952. if (n != 0 || i->openmax) {
  953. i->max -= n;
  954. changed = 1;
  955. }
  956. if (snd_interval_checkempty(i)) {
  957. i->empty = 1;
  958. return -EINVAL;
  959. }
  960. return changed;
  961. }
  962. /* Info constraints helpers */
  963. /**
  964. * snd_pcm_hw_rule_add - add the hw-constraint rule
  965. * @runtime: the pcm runtime instance
  966. * @cond: condition bits
  967. * @var: the variable to evaluate
  968. * @func: the evaluation function
  969. * @private: the private data pointer passed to function
  970. * @dep: the dependent variables
  971. *
  972. * Returns zero if successful, or a negative error code on failure.
  973. */
  974. int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
  975. int var,
  976. snd_pcm_hw_rule_func_t func, void *private,
  977. int dep, ...)
  978. {
  979. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  980. struct snd_pcm_hw_rule *c;
  981. unsigned int k;
  982. va_list args;
  983. va_start(args, dep);
  984. if (constrs->rules_num >= constrs->rules_all) {
  985. struct snd_pcm_hw_rule *new;
  986. unsigned int new_rules = constrs->rules_all + 16;
  987. new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
  988. if (!new)
  989. return -ENOMEM;
  990. if (constrs->rules) {
  991. memcpy(new, constrs->rules,
  992. constrs->rules_num * sizeof(*c));
  993. kfree(constrs->rules);
  994. }
  995. constrs->rules = new;
  996. constrs->rules_all = new_rules;
  997. }
  998. c = &constrs->rules[constrs->rules_num];
  999. c->cond = cond;
  1000. c->func = func;
  1001. c->var = var;
  1002. c->private = private;
  1003. k = 0;
  1004. while (1) {
  1005. if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
  1006. return -EINVAL;
  1007. c->deps[k++] = dep;
  1008. if (dep < 0)
  1009. break;
  1010. dep = va_arg(args, int);
  1011. }
  1012. constrs->rules_num++;
  1013. va_end(args);
  1014. return 0;
  1015. }
  1016. EXPORT_SYMBOL(snd_pcm_hw_rule_add);
  1017. /**
  1018. * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
  1019. * @runtime: PCM runtime instance
  1020. * @var: hw_params variable to apply the mask
  1021. * @mask: the bitmap mask
  1022. *
  1023. * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
  1024. */
  1025. int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1026. u_int32_t mask)
  1027. {
  1028. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1029. struct snd_mask *maskp = constrs_mask(constrs, var);
  1030. *maskp->bits &= mask;
  1031. memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
  1032. if (*maskp->bits == 0)
  1033. return -EINVAL;
  1034. return 0;
  1035. }
  1036. /**
  1037. * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
  1038. * @runtime: PCM runtime instance
  1039. * @var: hw_params variable to apply the mask
  1040. * @mask: the 64bit bitmap mask
  1041. *
  1042. * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
  1043. */
  1044. int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1045. u_int64_t mask)
  1046. {
  1047. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1048. struct snd_mask *maskp = constrs_mask(constrs, var);
  1049. maskp->bits[0] &= (u_int32_t)mask;
  1050. maskp->bits[1] &= (u_int32_t)(mask >> 32);
  1051. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
  1052. if (! maskp->bits[0] && ! maskp->bits[1])
  1053. return -EINVAL;
  1054. return 0;
  1055. }
  1056. /**
  1057. * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
  1058. * @runtime: PCM runtime instance
  1059. * @var: hw_params variable to apply the integer constraint
  1060. *
  1061. * Apply the constraint of integer to an interval parameter.
  1062. */
  1063. int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
  1064. {
  1065. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1066. return snd_interval_setinteger(constrs_interval(constrs, var));
  1067. }
  1068. EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
  1069. /**
  1070. * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
  1071. * @runtime: PCM runtime instance
  1072. * @var: hw_params variable to apply the range
  1073. * @min: the minimal value
  1074. * @max: the maximal value
  1075. *
  1076. * Apply the min/max range constraint to an interval parameter.
  1077. */
  1078. int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1079. unsigned int min, unsigned int max)
  1080. {
  1081. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1082. struct snd_interval t;
  1083. t.min = min;
  1084. t.max = max;
  1085. t.openmin = t.openmax = 0;
  1086. t.integer = 0;
  1087. return snd_interval_refine(constrs_interval(constrs, var), &t);
  1088. }
  1089. EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
  1090. static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
  1091. struct snd_pcm_hw_rule *rule)
  1092. {
  1093. struct snd_pcm_hw_constraint_list *list = rule->private;
  1094. return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
  1095. }
  1096. /**
  1097. * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
  1098. * @runtime: PCM runtime instance
  1099. * @cond: condition bits
  1100. * @var: hw_params variable to apply the list constraint
  1101. * @l: list
  1102. *
  1103. * Apply the list of constraints to an interval parameter.
  1104. */
  1105. int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
  1106. unsigned int cond,
  1107. snd_pcm_hw_param_t var,
  1108. struct snd_pcm_hw_constraint_list *l)
  1109. {
  1110. return snd_pcm_hw_rule_add(runtime, cond, var,
  1111. snd_pcm_hw_rule_list, l,
  1112. var, -1);
  1113. }
  1114. EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
  1115. static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
  1116. struct snd_pcm_hw_rule *rule)
  1117. {
  1118. struct snd_pcm_hw_constraint_ratnums *r = rule->private;
  1119. unsigned int num = 0, den = 0;
  1120. int err;
  1121. err = snd_interval_ratnum(hw_param_interval(params, rule->var),
  1122. r->nrats, r->rats, &num, &den);
  1123. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1124. params->rate_num = num;
  1125. params->rate_den = den;
  1126. }
  1127. return err;
  1128. }
  1129. /**
  1130. * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
  1131. * @runtime: PCM runtime instance
  1132. * @cond: condition bits
  1133. * @var: hw_params variable to apply the ratnums constraint
  1134. * @r: struct snd_ratnums constriants
  1135. */
  1136. int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
  1137. unsigned int cond,
  1138. snd_pcm_hw_param_t var,
  1139. struct snd_pcm_hw_constraint_ratnums *r)
  1140. {
  1141. return snd_pcm_hw_rule_add(runtime, cond, var,
  1142. snd_pcm_hw_rule_ratnums, r,
  1143. var, -1);
  1144. }
  1145. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
  1146. static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
  1147. struct snd_pcm_hw_rule *rule)
  1148. {
  1149. struct snd_pcm_hw_constraint_ratdens *r = rule->private;
  1150. unsigned int num = 0, den = 0;
  1151. int err = snd_interval_ratden(hw_param_interval(params, rule->var),
  1152. r->nrats, r->rats, &num, &den);
  1153. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1154. params->rate_num = num;
  1155. params->rate_den = den;
  1156. }
  1157. return err;
  1158. }
  1159. /**
  1160. * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
  1161. * @runtime: PCM runtime instance
  1162. * @cond: condition bits
  1163. * @var: hw_params variable to apply the ratdens constraint
  1164. * @r: struct snd_ratdens constriants
  1165. */
  1166. int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
  1167. unsigned int cond,
  1168. snd_pcm_hw_param_t var,
  1169. struct snd_pcm_hw_constraint_ratdens *r)
  1170. {
  1171. return snd_pcm_hw_rule_add(runtime, cond, var,
  1172. snd_pcm_hw_rule_ratdens, r,
  1173. var, -1);
  1174. }
  1175. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
  1176. static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
  1177. struct snd_pcm_hw_rule *rule)
  1178. {
  1179. unsigned int l = (unsigned long) rule->private;
  1180. int width = l & 0xffff;
  1181. unsigned int msbits = l >> 16;
  1182. struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
  1183. if (snd_interval_single(i) && snd_interval_value(i) == width)
  1184. params->msbits = msbits;
  1185. return 0;
  1186. }
  1187. /**
  1188. * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
  1189. * @runtime: PCM runtime instance
  1190. * @cond: condition bits
  1191. * @width: sample bits width
  1192. * @msbits: msbits width
  1193. */
  1194. int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
  1195. unsigned int cond,
  1196. unsigned int width,
  1197. unsigned int msbits)
  1198. {
  1199. unsigned long l = (msbits << 16) | width;
  1200. return snd_pcm_hw_rule_add(runtime, cond, -1,
  1201. snd_pcm_hw_rule_msbits,
  1202. (void*) l,
  1203. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1204. }
  1205. EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
  1206. static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
  1207. struct snd_pcm_hw_rule *rule)
  1208. {
  1209. unsigned long step = (unsigned long) rule->private;
  1210. return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
  1211. }
  1212. /**
  1213. * snd_pcm_hw_constraint_step - add a hw constraint step rule
  1214. * @runtime: PCM runtime instance
  1215. * @cond: condition bits
  1216. * @var: hw_params variable to apply the step constraint
  1217. * @step: step size
  1218. */
  1219. int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
  1220. unsigned int cond,
  1221. snd_pcm_hw_param_t var,
  1222. unsigned long step)
  1223. {
  1224. return snd_pcm_hw_rule_add(runtime, cond, var,
  1225. snd_pcm_hw_rule_step, (void *) step,
  1226. var, -1);
  1227. }
  1228. EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
  1229. static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
  1230. {
  1231. static unsigned int pow2_sizes[] = {
  1232. 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
  1233. 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
  1234. 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
  1235. 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
  1236. };
  1237. return snd_interval_list(hw_param_interval(params, rule->var),
  1238. ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
  1239. }
  1240. /**
  1241. * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
  1242. * @runtime: PCM runtime instance
  1243. * @cond: condition bits
  1244. * @var: hw_params variable to apply the power-of-2 constraint
  1245. */
  1246. int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
  1247. unsigned int cond,
  1248. snd_pcm_hw_param_t var)
  1249. {
  1250. return snd_pcm_hw_rule_add(runtime, cond, var,
  1251. snd_pcm_hw_rule_pow2, NULL,
  1252. var, -1);
  1253. }
  1254. EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
  1255. static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
  1256. snd_pcm_hw_param_t var)
  1257. {
  1258. if (hw_is_mask(var)) {
  1259. snd_mask_any(hw_param_mask(params, var));
  1260. params->cmask |= 1 << var;
  1261. params->rmask |= 1 << var;
  1262. return;
  1263. }
  1264. if (hw_is_interval(var)) {
  1265. snd_interval_any(hw_param_interval(params, var));
  1266. params->cmask |= 1 << var;
  1267. params->rmask |= 1 << var;
  1268. return;
  1269. }
  1270. snd_BUG();
  1271. }
  1272. void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
  1273. {
  1274. unsigned int k;
  1275. memset(params, 0, sizeof(*params));
  1276. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
  1277. _snd_pcm_hw_param_any(params, k);
  1278. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
  1279. _snd_pcm_hw_param_any(params, k);
  1280. params->info = ~0U;
  1281. }
  1282. EXPORT_SYMBOL(_snd_pcm_hw_params_any);
  1283. /**
  1284. * snd_pcm_hw_param_value - return @params field @var value
  1285. * @params: the hw_params instance
  1286. * @var: parameter to retrieve
  1287. * @dir: pointer to the direction (-1,0,1) or %NULL
  1288. *
  1289. * Return the value for field @var if it's fixed in configuration space
  1290. * defined by @params. Return -%EINVAL otherwise.
  1291. */
  1292. int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
  1293. snd_pcm_hw_param_t var, int *dir)
  1294. {
  1295. if (hw_is_mask(var)) {
  1296. const struct snd_mask *mask = hw_param_mask_c(params, var);
  1297. if (!snd_mask_single(mask))
  1298. return -EINVAL;
  1299. if (dir)
  1300. *dir = 0;
  1301. return snd_mask_value(mask);
  1302. }
  1303. if (hw_is_interval(var)) {
  1304. const struct snd_interval *i = hw_param_interval_c(params, var);
  1305. if (!snd_interval_single(i))
  1306. return -EINVAL;
  1307. if (dir)
  1308. *dir = i->openmin;
  1309. return snd_interval_value(i);
  1310. }
  1311. return -EINVAL;
  1312. }
  1313. EXPORT_SYMBOL(snd_pcm_hw_param_value);
  1314. void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
  1315. snd_pcm_hw_param_t var)
  1316. {
  1317. if (hw_is_mask(var)) {
  1318. snd_mask_none(hw_param_mask(params, var));
  1319. params->cmask |= 1 << var;
  1320. params->rmask |= 1 << var;
  1321. } else if (hw_is_interval(var)) {
  1322. snd_interval_none(hw_param_interval(params, var));
  1323. params->cmask |= 1 << var;
  1324. params->rmask |= 1 << var;
  1325. } else {
  1326. snd_BUG();
  1327. }
  1328. }
  1329. EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
  1330. static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
  1331. snd_pcm_hw_param_t var)
  1332. {
  1333. int changed;
  1334. if (hw_is_mask(var))
  1335. changed = snd_mask_refine_first(hw_param_mask(params, var));
  1336. else if (hw_is_interval(var))
  1337. changed = snd_interval_refine_first(hw_param_interval(params, var));
  1338. else
  1339. return -EINVAL;
  1340. if (changed) {
  1341. params->cmask |= 1 << var;
  1342. params->rmask |= 1 << var;
  1343. }
  1344. return changed;
  1345. }
  1346. /**
  1347. * snd_pcm_hw_param_first - refine config space and return minimum value
  1348. * @pcm: PCM instance
  1349. * @params: the hw_params instance
  1350. * @var: parameter to retrieve
  1351. * @dir: pointer to the direction (-1,0,1) or %NULL
  1352. *
  1353. * Inside configuration space defined by @params remove from @var all
  1354. * values > minimum. Reduce configuration space accordingly.
  1355. * Return the minimum.
  1356. */
  1357. int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
  1358. struct snd_pcm_hw_params *params,
  1359. snd_pcm_hw_param_t var, int *dir)
  1360. {
  1361. int changed = _snd_pcm_hw_param_first(params, var);
  1362. if (changed < 0)
  1363. return changed;
  1364. if (params->rmask) {
  1365. int err = snd_pcm_hw_refine(pcm, params);
  1366. if (snd_BUG_ON(err < 0))
  1367. return err;
  1368. }
  1369. return snd_pcm_hw_param_value(params, var, dir);
  1370. }
  1371. EXPORT_SYMBOL(snd_pcm_hw_param_first);
  1372. static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
  1373. snd_pcm_hw_param_t var)
  1374. {
  1375. int changed;
  1376. if (hw_is_mask(var))
  1377. changed = snd_mask_refine_last(hw_param_mask(params, var));
  1378. else if (hw_is_interval(var))
  1379. changed = snd_interval_refine_last(hw_param_interval(params, var));
  1380. else
  1381. return -EINVAL;
  1382. if (changed) {
  1383. params->cmask |= 1 << var;
  1384. params->rmask |= 1 << var;
  1385. }
  1386. return changed;
  1387. }
  1388. /**
  1389. * snd_pcm_hw_param_last - refine config space and return maximum value
  1390. * @pcm: PCM instance
  1391. * @params: the hw_params instance
  1392. * @var: parameter to retrieve
  1393. * @dir: pointer to the direction (-1,0,1) or %NULL
  1394. *
  1395. * Inside configuration space defined by @params remove from @var all
  1396. * values < maximum. Reduce configuration space accordingly.
  1397. * Return the maximum.
  1398. */
  1399. int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
  1400. struct snd_pcm_hw_params *params,
  1401. snd_pcm_hw_param_t var, int *dir)
  1402. {
  1403. int changed = _snd_pcm_hw_param_last(params, var);
  1404. if (changed < 0)
  1405. return changed;
  1406. if (params->rmask) {
  1407. int err = snd_pcm_hw_refine(pcm, params);
  1408. if (snd_BUG_ON(err < 0))
  1409. return err;
  1410. }
  1411. return snd_pcm_hw_param_value(params, var, dir);
  1412. }
  1413. EXPORT_SYMBOL(snd_pcm_hw_param_last);
  1414. /**
  1415. * snd_pcm_hw_param_choose - choose a configuration defined by @params
  1416. * @pcm: PCM instance
  1417. * @params: the hw_params instance
  1418. *
  1419. * Choose one configuration from configuration space defined by @params.
  1420. * The configuration chosen is that obtained fixing in this order:
  1421. * first access, first format, first subformat, min channels,
  1422. * min rate, min period time, max buffer size, min tick time
  1423. */
  1424. int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
  1425. struct snd_pcm_hw_params *params)
  1426. {
  1427. static int vars[] = {
  1428. SNDRV_PCM_HW_PARAM_ACCESS,
  1429. SNDRV_PCM_HW_PARAM_FORMAT,
  1430. SNDRV_PCM_HW_PARAM_SUBFORMAT,
  1431. SNDRV_PCM_HW_PARAM_CHANNELS,
  1432. SNDRV_PCM_HW_PARAM_RATE,
  1433. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1434. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1435. SNDRV_PCM_HW_PARAM_TICK_TIME,
  1436. -1
  1437. };
  1438. int err, *v;
  1439. for (v = vars; *v != -1; v++) {
  1440. if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
  1441. err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
  1442. else
  1443. err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
  1444. if (snd_BUG_ON(err < 0))
  1445. return err;
  1446. }
  1447. return 0;
  1448. }
  1449. static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
  1450. void *arg)
  1451. {
  1452. struct snd_pcm_runtime *runtime = substream->runtime;
  1453. unsigned long flags;
  1454. snd_pcm_stream_lock_irqsave(substream, flags);
  1455. if (snd_pcm_running(substream) &&
  1456. snd_pcm_update_hw_ptr(substream) >= 0)
  1457. runtime->status->hw_ptr %= runtime->buffer_size;
  1458. else
  1459. runtime->status->hw_ptr = 0;
  1460. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1461. return 0;
  1462. }
  1463. static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
  1464. void *arg)
  1465. {
  1466. struct snd_pcm_channel_info *info = arg;
  1467. struct snd_pcm_runtime *runtime = substream->runtime;
  1468. int width;
  1469. if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
  1470. info->offset = -1;
  1471. return 0;
  1472. }
  1473. width = snd_pcm_format_physical_width(runtime->format);
  1474. if (width < 0)
  1475. return width;
  1476. info->offset = 0;
  1477. switch (runtime->access) {
  1478. case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
  1479. case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
  1480. info->first = info->channel * width;
  1481. info->step = runtime->channels * width;
  1482. break;
  1483. case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
  1484. case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
  1485. {
  1486. size_t size = runtime->dma_bytes / runtime->channels;
  1487. info->first = info->channel * size * 8;
  1488. info->step = width;
  1489. break;
  1490. }
  1491. default:
  1492. snd_BUG();
  1493. break;
  1494. }
  1495. return 0;
  1496. }
  1497. static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
  1498. void *arg)
  1499. {
  1500. struct snd_pcm_hw_params *params = arg;
  1501. snd_pcm_format_t format;
  1502. int channels, width;
  1503. params->fifo_size = substream->runtime->hw.fifo_size;
  1504. if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
  1505. format = params_format(params);
  1506. channels = params_channels(params);
  1507. width = snd_pcm_format_physical_width(format);
  1508. params->fifo_size /= width * channels;
  1509. }
  1510. return 0;
  1511. }
  1512. /**
  1513. * snd_pcm_lib_ioctl - a generic PCM ioctl callback
  1514. * @substream: the pcm substream instance
  1515. * @cmd: ioctl command
  1516. * @arg: ioctl argument
  1517. *
  1518. * Processes the generic ioctl commands for PCM.
  1519. * Can be passed as the ioctl callback for PCM ops.
  1520. *
  1521. * Returns zero if successful, or a negative error code on failure.
  1522. */
  1523. int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  1524. unsigned int cmd, void *arg)
  1525. {
  1526. switch (cmd) {
  1527. case SNDRV_PCM_IOCTL1_INFO:
  1528. return 0;
  1529. case SNDRV_PCM_IOCTL1_RESET:
  1530. return snd_pcm_lib_ioctl_reset(substream, arg);
  1531. case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
  1532. return snd_pcm_lib_ioctl_channel_info(substream, arg);
  1533. case SNDRV_PCM_IOCTL1_FIFO_SIZE:
  1534. return snd_pcm_lib_ioctl_fifo_size(substream, arg);
  1535. }
  1536. return -ENXIO;
  1537. }
  1538. EXPORT_SYMBOL(snd_pcm_lib_ioctl);
  1539. /**
  1540. * snd_pcm_period_elapsed - update the pcm status for the next period
  1541. * @substream: the pcm substream instance
  1542. *
  1543. * This function is called from the interrupt handler when the
  1544. * PCM has processed the period size. It will update the current
  1545. * pointer, wake up sleepers, etc.
  1546. *
  1547. * Even if more than one periods have elapsed since the last call, you
  1548. * have to call this only once.
  1549. */
  1550. void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
  1551. {
  1552. struct snd_pcm_runtime *runtime;
  1553. unsigned long flags;
  1554. if (PCM_RUNTIME_CHECK(substream))
  1555. return;
  1556. runtime = substream->runtime;
  1557. if (runtime->transfer_ack_begin)
  1558. runtime->transfer_ack_begin(substream);
  1559. snd_pcm_stream_lock_irqsave(substream, flags);
  1560. if (!snd_pcm_running(substream) ||
  1561. snd_pcm_update_hw_ptr0(substream, 1) < 0)
  1562. goto _end;
  1563. if (substream->timer_running)
  1564. snd_timer_interrupt(substream->timer, 1);
  1565. _end:
  1566. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1567. if (runtime->transfer_ack_end)
  1568. runtime->transfer_ack_end(substream);
  1569. kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
  1570. }
  1571. EXPORT_SYMBOL(snd_pcm_period_elapsed);
  1572. /*
  1573. * Wait until avail_min data becomes available
  1574. * Returns a negative error code if any error occurs during operation.
  1575. * The available space is stored on availp. When err = 0 and avail = 0
  1576. * on the capture stream, it indicates the stream is in DRAINING state.
  1577. */
  1578. static int wait_for_avail_min(struct snd_pcm_substream *substream,
  1579. snd_pcm_uframes_t *availp)
  1580. {
  1581. struct snd_pcm_runtime *runtime = substream->runtime;
  1582. int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  1583. wait_queue_t wait;
  1584. int err = 0;
  1585. snd_pcm_uframes_t avail = 0;
  1586. long tout;
  1587. init_waitqueue_entry(&wait, current);
  1588. add_wait_queue(&runtime->tsleep, &wait);
  1589. for (;;) {
  1590. if (signal_pending(current)) {
  1591. err = -ERESTARTSYS;
  1592. break;
  1593. }
  1594. set_current_state(TASK_INTERRUPTIBLE);
  1595. snd_pcm_stream_unlock_irq(substream);
  1596. tout = schedule_timeout(msecs_to_jiffies(10000));
  1597. snd_pcm_stream_lock_irq(substream);
  1598. switch (runtime->status->state) {
  1599. case SNDRV_PCM_STATE_SUSPENDED:
  1600. err = -ESTRPIPE;
  1601. goto _endloop;
  1602. case SNDRV_PCM_STATE_XRUN:
  1603. err = -EPIPE;
  1604. goto _endloop;
  1605. case SNDRV_PCM_STATE_DRAINING:
  1606. if (is_playback)
  1607. err = -EPIPE;
  1608. else
  1609. avail = 0; /* indicate draining */
  1610. goto _endloop;
  1611. case SNDRV_PCM_STATE_OPEN:
  1612. case SNDRV_PCM_STATE_SETUP:
  1613. case SNDRV_PCM_STATE_DISCONNECTED:
  1614. err = -EBADFD;
  1615. goto _endloop;
  1616. }
  1617. if (!tout) {
  1618. snd_printd("%s write error (DMA or IRQ trouble?)\n",
  1619. is_playback ? "playback" : "capture");
  1620. err = -EIO;
  1621. break;
  1622. }
  1623. if (is_playback)
  1624. avail = snd_pcm_playback_avail(runtime);
  1625. else
  1626. avail = snd_pcm_capture_avail(runtime);
  1627. if (avail >= runtime->control->avail_min)
  1628. break;
  1629. }
  1630. _endloop:
  1631. remove_wait_queue(&runtime->tsleep, &wait);
  1632. *availp = avail;
  1633. return err;
  1634. }
  1635. static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
  1636. unsigned int hwoff,
  1637. unsigned long data, unsigned int off,
  1638. snd_pcm_uframes_t frames)
  1639. {
  1640. struct snd_pcm_runtime *runtime = substream->runtime;
  1641. int err;
  1642. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  1643. if (substream->ops->copy) {
  1644. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  1645. return err;
  1646. } else {
  1647. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  1648. if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
  1649. return -EFAULT;
  1650. }
  1651. return 0;
  1652. }
  1653. typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
  1654. unsigned long data, unsigned int off,
  1655. snd_pcm_uframes_t size);
  1656. static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
  1657. unsigned long data,
  1658. snd_pcm_uframes_t size,
  1659. int nonblock,
  1660. transfer_f transfer)
  1661. {
  1662. struct snd_pcm_runtime *runtime = substream->runtime;
  1663. snd_pcm_uframes_t xfer = 0;
  1664. snd_pcm_uframes_t offset = 0;
  1665. int err = 0;
  1666. if (size == 0)
  1667. return 0;
  1668. snd_pcm_stream_lock_irq(substream);
  1669. switch (runtime->status->state) {
  1670. case SNDRV_PCM_STATE_PREPARED:
  1671. case SNDRV_PCM_STATE_RUNNING:
  1672. case SNDRV_PCM_STATE_PAUSED:
  1673. break;
  1674. case SNDRV_PCM_STATE_XRUN:
  1675. err = -EPIPE;
  1676. goto _end_unlock;
  1677. case SNDRV_PCM_STATE_SUSPENDED:
  1678. err = -ESTRPIPE;
  1679. goto _end_unlock;
  1680. default:
  1681. err = -EBADFD;
  1682. goto _end_unlock;
  1683. }
  1684. runtime->twake = 1;
  1685. while (size > 0) {
  1686. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  1687. snd_pcm_uframes_t avail;
  1688. snd_pcm_uframes_t cont;
  1689. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1690. snd_pcm_update_hw_ptr(substream);
  1691. avail = snd_pcm_playback_avail(runtime);
  1692. if (!avail) {
  1693. if (nonblock) {
  1694. err = -EAGAIN;
  1695. goto _end_unlock;
  1696. }
  1697. err = wait_for_avail_min(substream, &avail);
  1698. if (err < 0)
  1699. goto _end_unlock;
  1700. }
  1701. frames = size > avail ? avail : size;
  1702. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  1703. if (frames > cont)
  1704. frames = cont;
  1705. if (snd_BUG_ON(!frames)) {
  1706. runtime->twake = 0;
  1707. snd_pcm_stream_unlock_irq(substream);
  1708. return -EINVAL;
  1709. }
  1710. appl_ptr = runtime->control->appl_ptr;
  1711. appl_ofs = appl_ptr % runtime->buffer_size;
  1712. snd_pcm_stream_unlock_irq(substream);
  1713. err = transfer(substream, appl_ofs, data, offset, frames);
  1714. snd_pcm_stream_lock_irq(substream);
  1715. if (err < 0)
  1716. goto _end_unlock;
  1717. switch (runtime->status->state) {
  1718. case SNDRV_PCM_STATE_XRUN:
  1719. err = -EPIPE;
  1720. goto _end_unlock;
  1721. case SNDRV_PCM_STATE_SUSPENDED:
  1722. err = -ESTRPIPE;
  1723. goto _end_unlock;
  1724. default:
  1725. break;
  1726. }
  1727. appl_ptr += frames;
  1728. if (appl_ptr >= runtime->boundary)
  1729. appl_ptr -= runtime->boundary;
  1730. runtime->control->appl_ptr = appl_ptr;
  1731. if (substream->ops->ack)
  1732. substream->ops->ack(substream);
  1733. offset += frames;
  1734. size -= frames;
  1735. xfer += frames;
  1736. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
  1737. snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
  1738. err = snd_pcm_start(substream);
  1739. if (err < 0)
  1740. goto _end_unlock;
  1741. }
  1742. }
  1743. _end_unlock:
  1744. runtime->twake = 0;
  1745. if (xfer > 0 && err >= 0)
  1746. snd_pcm_update_state(substream, runtime);
  1747. snd_pcm_stream_unlock_irq(substream);
  1748. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  1749. }
  1750. /* sanity-check for read/write methods */
  1751. static int pcm_sanity_check(struct snd_pcm_substream *substream)
  1752. {
  1753. struct snd_pcm_runtime *runtime;
  1754. if (PCM_RUNTIME_CHECK(substream))
  1755. return -ENXIO;
  1756. runtime = substream->runtime;
  1757. if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
  1758. return -EINVAL;
  1759. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1760. return -EBADFD;
  1761. return 0;
  1762. }
  1763. snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
  1764. {
  1765. struct snd_pcm_runtime *runtime;
  1766. int nonblock;
  1767. int err;
  1768. err = pcm_sanity_check(substream);
  1769. if (err < 0)
  1770. return err;
  1771. runtime = substream->runtime;
  1772. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1773. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
  1774. runtime->channels > 1)
  1775. return -EINVAL;
  1776. return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
  1777. snd_pcm_lib_write_transfer);
  1778. }
  1779. EXPORT_SYMBOL(snd_pcm_lib_write);
  1780. static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
  1781. unsigned int hwoff,
  1782. unsigned long data, unsigned int off,
  1783. snd_pcm_uframes_t frames)
  1784. {
  1785. struct snd_pcm_runtime *runtime = substream->runtime;
  1786. int err;
  1787. void __user **bufs = (void __user **)data;
  1788. int channels = runtime->channels;
  1789. int c;
  1790. if (substream->ops->copy) {
  1791. if (snd_BUG_ON(!substream->ops->silence))
  1792. return -EINVAL;
  1793. for (c = 0; c < channels; ++c, ++bufs) {
  1794. if (*bufs == NULL) {
  1795. if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
  1796. return err;
  1797. } else {
  1798. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  1799. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  1800. return err;
  1801. }
  1802. }
  1803. } else {
  1804. /* default transfer behaviour */
  1805. size_t dma_csize = runtime->dma_bytes / channels;
  1806. for (c = 0; c < channels; ++c, ++bufs) {
  1807. char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  1808. if (*bufs == NULL) {
  1809. snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
  1810. } else {
  1811. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  1812. if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
  1813. return -EFAULT;
  1814. }
  1815. }
  1816. }
  1817. return 0;
  1818. }
  1819. snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
  1820. void __user **bufs,
  1821. snd_pcm_uframes_t frames)
  1822. {
  1823. struct snd_pcm_runtime *runtime;
  1824. int nonblock;
  1825. int err;
  1826. err = pcm_sanity_check(substream);
  1827. if (err < 0)
  1828. return err;
  1829. runtime = substream->runtime;
  1830. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1831. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  1832. return -EINVAL;
  1833. return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
  1834. nonblock, snd_pcm_lib_writev_transfer);
  1835. }
  1836. EXPORT_SYMBOL(snd_pcm_lib_writev);
  1837. static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
  1838. unsigned int hwoff,
  1839. unsigned long data, unsigned int off,
  1840. snd_pcm_uframes_t frames)
  1841. {
  1842. struct snd_pcm_runtime *runtime = substream->runtime;
  1843. int err;
  1844. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  1845. if (substream->ops->copy) {
  1846. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  1847. return err;
  1848. } else {
  1849. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  1850. if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
  1851. return -EFAULT;
  1852. }
  1853. return 0;
  1854. }
  1855. static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
  1856. unsigned long data,
  1857. snd_pcm_uframes_t size,
  1858. int nonblock,
  1859. transfer_f transfer)
  1860. {
  1861. struct snd_pcm_runtime *runtime = substream->runtime;
  1862. snd_pcm_uframes_t xfer = 0;
  1863. snd_pcm_uframes_t offset = 0;
  1864. int err = 0;
  1865. if (size == 0)
  1866. return 0;
  1867. snd_pcm_stream_lock_irq(substream);
  1868. switch (runtime->status->state) {
  1869. case SNDRV_PCM_STATE_PREPARED:
  1870. if (size >= runtime->start_threshold) {
  1871. err = snd_pcm_start(substream);
  1872. if (err < 0)
  1873. goto _end_unlock;
  1874. }
  1875. break;
  1876. case SNDRV_PCM_STATE_DRAINING:
  1877. case SNDRV_PCM_STATE_RUNNING:
  1878. case SNDRV_PCM_STATE_PAUSED:
  1879. break;
  1880. case SNDRV_PCM_STATE_XRUN:
  1881. err = -EPIPE;
  1882. goto _end_unlock;
  1883. case SNDRV_PCM_STATE_SUSPENDED:
  1884. err = -ESTRPIPE;
  1885. goto _end_unlock;
  1886. default:
  1887. err = -EBADFD;
  1888. goto _end_unlock;
  1889. }
  1890. runtime->twake = 1;
  1891. while (size > 0) {
  1892. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  1893. snd_pcm_uframes_t avail;
  1894. snd_pcm_uframes_t cont;
  1895. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1896. snd_pcm_update_hw_ptr(substream);
  1897. avail = snd_pcm_capture_avail(runtime);
  1898. if (!avail) {
  1899. if (runtime->status->state ==
  1900. SNDRV_PCM_STATE_DRAINING) {
  1901. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1902. goto _end_unlock;
  1903. }
  1904. if (nonblock) {
  1905. err = -EAGAIN;
  1906. goto _end_unlock;
  1907. }
  1908. err = wait_for_avail_min(substream, &avail);
  1909. if (err < 0)
  1910. goto _end_unlock;
  1911. if (!avail)
  1912. continue; /* draining */
  1913. }
  1914. frames = size > avail ? avail : size;
  1915. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  1916. if (frames > cont)
  1917. frames = cont;
  1918. if (snd_BUG_ON(!frames)) {
  1919. runtime->twake = 0;
  1920. snd_pcm_stream_unlock_irq(substream);
  1921. return -EINVAL;
  1922. }
  1923. appl_ptr = runtime->control->appl_ptr;
  1924. appl_ofs = appl_ptr % runtime->buffer_size;
  1925. snd_pcm_stream_unlock_irq(substream);
  1926. err = transfer(substream, appl_ofs, data, offset, frames);
  1927. snd_pcm_stream_lock_irq(substream);
  1928. if (err < 0)
  1929. goto _end_unlock;
  1930. switch (runtime->status->state) {
  1931. case SNDRV_PCM_STATE_XRUN:
  1932. err = -EPIPE;
  1933. goto _end_unlock;
  1934. case SNDRV_PCM_STATE_SUSPENDED:
  1935. err = -ESTRPIPE;
  1936. goto _end_unlock;
  1937. default:
  1938. break;
  1939. }
  1940. appl_ptr += frames;
  1941. if (appl_ptr >= runtime->boundary)
  1942. appl_ptr -= runtime->boundary;
  1943. runtime->control->appl_ptr = appl_ptr;
  1944. if (substream->ops->ack)
  1945. substream->ops->ack(substream);
  1946. offset += frames;
  1947. size -= frames;
  1948. xfer += frames;
  1949. }
  1950. _end_unlock:
  1951. runtime->twake = 0;
  1952. if (xfer > 0 && err >= 0)
  1953. snd_pcm_update_state(substream, runtime);
  1954. snd_pcm_stream_unlock_irq(substream);
  1955. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  1956. }
  1957. snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
  1958. {
  1959. struct snd_pcm_runtime *runtime;
  1960. int nonblock;
  1961. int err;
  1962. err = pcm_sanity_check(substream);
  1963. if (err < 0)
  1964. return err;
  1965. runtime = substream->runtime;
  1966. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1967. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
  1968. return -EINVAL;
  1969. return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
  1970. }
  1971. EXPORT_SYMBOL(snd_pcm_lib_read);
  1972. static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
  1973. unsigned int hwoff,
  1974. unsigned long data, unsigned int off,
  1975. snd_pcm_uframes_t frames)
  1976. {
  1977. struct snd_pcm_runtime *runtime = substream->runtime;
  1978. int err;
  1979. void __user **bufs = (void __user **)data;
  1980. int channels = runtime->channels;
  1981. int c;
  1982. if (substream->ops->copy) {
  1983. for (c = 0; c < channels; ++c, ++bufs) {
  1984. char __user *buf;
  1985. if (*bufs == NULL)
  1986. continue;
  1987. buf = *bufs + samples_to_bytes(runtime, off);
  1988. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  1989. return err;
  1990. }
  1991. } else {
  1992. snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
  1993. for (c = 0; c < channels; ++c, ++bufs) {
  1994. char *hwbuf;
  1995. char __user *buf;
  1996. if (*bufs == NULL)
  1997. continue;
  1998. hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  1999. buf = *bufs + samples_to_bytes(runtime, off);
  2000. if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
  2001. return -EFAULT;
  2002. }
  2003. }
  2004. return 0;
  2005. }
  2006. snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
  2007. void __user **bufs,
  2008. snd_pcm_uframes_t frames)
  2009. {
  2010. struct snd_pcm_runtime *runtime;
  2011. int nonblock;
  2012. int err;
  2013. err = pcm_sanity_check(substream);
  2014. if (err < 0)
  2015. return err;
  2016. runtime = substream->runtime;
  2017. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2018. return -EBADFD;
  2019. nonblock = !!(substream->f_flags & O_NONBLOCK);
  2020. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  2021. return -EINVAL;
  2022. return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
  2023. }
  2024. EXPORT_SYMBOL(snd_pcm_lib_readv);