pcm_lib.c 56 KB

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