pcm_lib.c 57 KB

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