pcm_lib.c 53 KB

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