pcm_lib.c 54 KB

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