pcm_lib.c 56 KB

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