pcm_lib.c 52 KB

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