pcm_lib.c 57 KB

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