pcm_lib.c 55 KB

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