pcm_lib.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  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. struct snd_interval list_range;
  887. if (!count) {
  888. i->empty = 1;
  889. return -EINVAL;
  890. }
  891. snd_interval_any(&list_range);
  892. list_range.min = UINT_MAX;
  893. list_range.max = 0;
  894. for (k = 0; k < count; k++) {
  895. if (mask && !(mask & (1 << k)))
  896. continue;
  897. if (!snd_interval_test(i, list[k]))
  898. continue;
  899. list_range.min = min(list_range.min, list[k]);
  900. list_range.max = max(list_range.max, list[k]);
  901. }
  902. return snd_interval_refine(i, &list_range);
  903. }
  904. EXPORT_SYMBOL(snd_interval_list);
  905. static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
  906. {
  907. unsigned int n;
  908. int changed = 0;
  909. n = (i->min - min) % step;
  910. if (n != 0 || i->openmin) {
  911. i->min += step - n;
  912. changed = 1;
  913. }
  914. n = (i->max - min) % step;
  915. if (n != 0 || i->openmax) {
  916. i->max -= n;
  917. changed = 1;
  918. }
  919. if (snd_interval_checkempty(i)) {
  920. i->empty = 1;
  921. return -EINVAL;
  922. }
  923. return changed;
  924. }
  925. /* Info constraints helpers */
  926. /**
  927. * snd_pcm_hw_rule_add - add the hw-constraint rule
  928. * @runtime: the pcm runtime instance
  929. * @cond: condition bits
  930. * @var: the variable to evaluate
  931. * @func: the evaluation function
  932. * @private: the private data pointer passed to function
  933. * @dep: the dependent variables
  934. *
  935. * Returns zero if successful, or a negative error code on failure.
  936. */
  937. int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
  938. int var,
  939. snd_pcm_hw_rule_func_t func, void *private,
  940. int dep, ...)
  941. {
  942. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  943. struct snd_pcm_hw_rule *c;
  944. unsigned int k;
  945. va_list args;
  946. va_start(args, dep);
  947. if (constrs->rules_num >= constrs->rules_all) {
  948. struct snd_pcm_hw_rule *new;
  949. unsigned int new_rules = constrs->rules_all + 16;
  950. new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
  951. if (!new)
  952. return -ENOMEM;
  953. if (constrs->rules) {
  954. memcpy(new, constrs->rules,
  955. constrs->rules_num * sizeof(*c));
  956. kfree(constrs->rules);
  957. }
  958. constrs->rules = new;
  959. constrs->rules_all = new_rules;
  960. }
  961. c = &constrs->rules[constrs->rules_num];
  962. c->cond = cond;
  963. c->func = func;
  964. c->var = var;
  965. c->private = private;
  966. k = 0;
  967. while (1) {
  968. if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
  969. return -EINVAL;
  970. c->deps[k++] = dep;
  971. if (dep < 0)
  972. break;
  973. dep = va_arg(args, int);
  974. }
  975. constrs->rules_num++;
  976. va_end(args);
  977. return 0;
  978. }
  979. EXPORT_SYMBOL(snd_pcm_hw_rule_add);
  980. /**
  981. * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
  982. * @runtime: PCM runtime instance
  983. * @var: hw_params variable to apply the mask
  984. * @mask: the bitmap mask
  985. *
  986. * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
  987. */
  988. int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  989. u_int32_t mask)
  990. {
  991. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  992. struct snd_mask *maskp = constrs_mask(constrs, var);
  993. *maskp->bits &= mask;
  994. memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
  995. if (*maskp->bits == 0)
  996. return -EINVAL;
  997. return 0;
  998. }
  999. /**
  1000. * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
  1001. * @runtime: PCM runtime instance
  1002. * @var: hw_params variable to apply the mask
  1003. * @mask: the 64bit bitmap mask
  1004. *
  1005. * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
  1006. */
  1007. int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1008. u_int64_t mask)
  1009. {
  1010. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1011. struct snd_mask *maskp = constrs_mask(constrs, var);
  1012. maskp->bits[0] &= (u_int32_t)mask;
  1013. maskp->bits[1] &= (u_int32_t)(mask >> 32);
  1014. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
  1015. if (! maskp->bits[0] && ! maskp->bits[1])
  1016. return -EINVAL;
  1017. return 0;
  1018. }
  1019. /**
  1020. * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
  1021. * @runtime: PCM runtime instance
  1022. * @var: hw_params variable to apply the integer constraint
  1023. *
  1024. * Apply the constraint of integer to an interval parameter.
  1025. */
  1026. int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
  1027. {
  1028. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1029. return snd_interval_setinteger(constrs_interval(constrs, var));
  1030. }
  1031. EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
  1032. /**
  1033. * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
  1034. * @runtime: PCM runtime instance
  1035. * @var: hw_params variable to apply the range
  1036. * @min: the minimal value
  1037. * @max: the maximal value
  1038. *
  1039. * Apply the min/max range constraint to an interval parameter.
  1040. */
  1041. int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1042. unsigned int min, unsigned int max)
  1043. {
  1044. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1045. struct snd_interval t;
  1046. t.min = min;
  1047. t.max = max;
  1048. t.openmin = t.openmax = 0;
  1049. t.integer = 0;
  1050. return snd_interval_refine(constrs_interval(constrs, var), &t);
  1051. }
  1052. EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
  1053. static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
  1054. struct snd_pcm_hw_rule *rule)
  1055. {
  1056. struct snd_pcm_hw_constraint_list *list = rule->private;
  1057. return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
  1058. }
  1059. /**
  1060. * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
  1061. * @runtime: PCM runtime instance
  1062. * @cond: condition bits
  1063. * @var: hw_params variable to apply the list constraint
  1064. * @l: list
  1065. *
  1066. * Apply the list of constraints to an interval parameter.
  1067. */
  1068. int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
  1069. unsigned int cond,
  1070. snd_pcm_hw_param_t var,
  1071. struct snd_pcm_hw_constraint_list *l)
  1072. {
  1073. return snd_pcm_hw_rule_add(runtime, cond, var,
  1074. snd_pcm_hw_rule_list, l,
  1075. var, -1);
  1076. }
  1077. EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
  1078. static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
  1079. struct snd_pcm_hw_rule *rule)
  1080. {
  1081. struct snd_pcm_hw_constraint_ratnums *r = rule->private;
  1082. unsigned int num = 0, den = 0;
  1083. int err;
  1084. err = snd_interval_ratnum(hw_param_interval(params, rule->var),
  1085. r->nrats, r->rats, &num, &den);
  1086. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1087. params->rate_num = num;
  1088. params->rate_den = den;
  1089. }
  1090. return err;
  1091. }
  1092. /**
  1093. * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
  1094. * @runtime: PCM runtime instance
  1095. * @cond: condition bits
  1096. * @var: hw_params variable to apply the ratnums constraint
  1097. * @r: struct snd_ratnums constriants
  1098. */
  1099. int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
  1100. unsigned int cond,
  1101. snd_pcm_hw_param_t var,
  1102. struct snd_pcm_hw_constraint_ratnums *r)
  1103. {
  1104. return snd_pcm_hw_rule_add(runtime, cond, var,
  1105. snd_pcm_hw_rule_ratnums, r,
  1106. var, -1);
  1107. }
  1108. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
  1109. static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
  1110. struct snd_pcm_hw_rule *rule)
  1111. {
  1112. struct snd_pcm_hw_constraint_ratdens *r = rule->private;
  1113. unsigned int num = 0, den = 0;
  1114. int err = snd_interval_ratden(hw_param_interval(params, rule->var),
  1115. r->nrats, r->rats, &num, &den);
  1116. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1117. params->rate_num = num;
  1118. params->rate_den = den;
  1119. }
  1120. return err;
  1121. }
  1122. /**
  1123. * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
  1124. * @runtime: PCM runtime instance
  1125. * @cond: condition bits
  1126. * @var: hw_params variable to apply the ratdens constraint
  1127. * @r: struct snd_ratdens constriants
  1128. */
  1129. int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
  1130. unsigned int cond,
  1131. snd_pcm_hw_param_t var,
  1132. struct snd_pcm_hw_constraint_ratdens *r)
  1133. {
  1134. return snd_pcm_hw_rule_add(runtime, cond, var,
  1135. snd_pcm_hw_rule_ratdens, r,
  1136. var, -1);
  1137. }
  1138. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
  1139. static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
  1140. struct snd_pcm_hw_rule *rule)
  1141. {
  1142. unsigned int l = (unsigned long) rule->private;
  1143. int width = l & 0xffff;
  1144. unsigned int msbits = l >> 16;
  1145. struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
  1146. if (snd_interval_single(i) && snd_interval_value(i) == width)
  1147. params->msbits = msbits;
  1148. return 0;
  1149. }
  1150. /**
  1151. * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
  1152. * @runtime: PCM runtime instance
  1153. * @cond: condition bits
  1154. * @width: sample bits width
  1155. * @msbits: msbits width
  1156. */
  1157. int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
  1158. unsigned int cond,
  1159. unsigned int width,
  1160. unsigned int msbits)
  1161. {
  1162. unsigned long l = (msbits << 16) | width;
  1163. return snd_pcm_hw_rule_add(runtime, cond, -1,
  1164. snd_pcm_hw_rule_msbits,
  1165. (void*) l,
  1166. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1167. }
  1168. EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
  1169. static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
  1170. struct snd_pcm_hw_rule *rule)
  1171. {
  1172. unsigned long step = (unsigned long) rule->private;
  1173. return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
  1174. }
  1175. /**
  1176. * snd_pcm_hw_constraint_step - add a hw constraint step rule
  1177. * @runtime: PCM runtime instance
  1178. * @cond: condition bits
  1179. * @var: hw_params variable to apply the step constraint
  1180. * @step: step size
  1181. */
  1182. int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
  1183. unsigned int cond,
  1184. snd_pcm_hw_param_t var,
  1185. unsigned long step)
  1186. {
  1187. return snd_pcm_hw_rule_add(runtime, cond, var,
  1188. snd_pcm_hw_rule_step, (void *) step,
  1189. var, -1);
  1190. }
  1191. EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
  1192. static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
  1193. {
  1194. static unsigned int pow2_sizes[] = {
  1195. 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
  1196. 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
  1197. 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
  1198. 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
  1199. };
  1200. return snd_interval_list(hw_param_interval(params, rule->var),
  1201. ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
  1202. }
  1203. /**
  1204. * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
  1205. * @runtime: PCM runtime instance
  1206. * @cond: condition bits
  1207. * @var: hw_params variable to apply the power-of-2 constraint
  1208. */
  1209. int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
  1210. unsigned int cond,
  1211. snd_pcm_hw_param_t var)
  1212. {
  1213. return snd_pcm_hw_rule_add(runtime, cond, var,
  1214. snd_pcm_hw_rule_pow2, NULL,
  1215. var, -1);
  1216. }
  1217. EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
  1218. static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
  1219. snd_pcm_hw_param_t var)
  1220. {
  1221. if (hw_is_mask(var)) {
  1222. snd_mask_any(hw_param_mask(params, var));
  1223. params->cmask |= 1 << var;
  1224. params->rmask |= 1 << var;
  1225. return;
  1226. }
  1227. if (hw_is_interval(var)) {
  1228. snd_interval_any(hw_param_interval(params, var));
  1229. params->cmask |= 1 << var;
  1230. params->rmask |= 1 << var;
  1231. return;
  1232. }
  1233. snd_BUG();
  1234. }
  1235. void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
  1236. {
  1237. unsigned int k;
  1238. memset(params, 0, sizeof(*params));
  1239. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
  1240. _snd_pcm_hw_param_any(params, k);
  1241. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
  1242. _snd_pcm_hw_param_any(params, k);
  1243. params->info = ~0U;
  1244. }
  1245. EXPORT_SYMBOL(_snd_pcm_hw_params_any);
  1246. /**
  1247. * snd_pcm_hw_param_value - return @params field @var value
  1248. * @params: the hw_params instance
  1249. * @var: parameter to retrieve
  1250. * @dir: pointer to the direction (-1,0,1) or %NULL
  1251. *
  1252. * Return the value for field @var if it's fixed in configuration space
  1253. * defined by @params. Return -%EINVAL otherwise.
  1254. */
  1255. int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
  1256. snd_pcm_hw_param_t var, int *dir)
  1257. {
  1258. if (hw_is_mask(var)) {
  1259. const struct snd_mask *mask = hw_param_mask_c(params, var);
  1260. if (!snd_mask_single(mask))
  1261. return -EINVAL;
  1262. if (dir)
  1263. *dir = 0;
  1264. return snd_mask_value(mask);
  1265. }
  1266. if (hw_is_interval(var)) {
  1267. const struct snd_interval *i = hw_param_interval_c(params, var);
  1268. if (!snd_interval_single(i))
  1269. return -EINVAL;
  1270. if (dir)
  1271. *dir = i->openmin;
  1272. return snd_interval_value(i);
  1273. }
  1274. return -EINVAL;
  1275. }
  1276. EXPORT_SYMBOL(snd_pcm_hw_param_value);
  1277. void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
  1278. snd_pcm_hw_param_t var)
  1279. {
  1280. if (hw_is_mask(var)) {
  1281. snd_mask_none(hw_param_mask(params, var));
  1282. params->cmask |= 1 << var;
  1283. params->rmask |= 1 << var;
  1284. } else if (hw_is_interval(var)) {
  1285. snd_interval_none(hw_param_interval(params, var));
  1286. params->cmask |= 1 << var;
  1287. params->rmask |= 1 << var;
  1288. } else {
  1289. snd_BUG();
  1290. }
  1291. }
  1292. EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
  1293. static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
  1294. snd_pcm_hw_param_t var)
  1295. {
  1296. int changed;
  1297. if (hw_is_mask(var))
  1298. changed = snd_mask_refine_first(hw_param_mask(params, var));
  1299. else if (hw_is_interval(var))
  1300. changed = snd_interval_refine_first(hw_param_interval(params, var));
  1301. else
  1302. return -EINVAL;
  1303. if (changed) {
  1304. params->cmask |= 1 << var;
  1305. params->rmask |= 1 << var;
  1306. }
  1307. return changed;
  1308. }
  1309. /**
  1310. * snd_pcm_hw_param_first - refine config space and return minimum value
  1311. * @pcm: PCM instance
  1312. * @params: the hw_params instance
  1313. * @var: parameter to retrieve
  1314. * @dir: pointer to the direction (-1,0,1) or %NULL
  1315. *
  1316. * Inside configuration space defined by @params remove from @var all
  1317. * values > minimum. Reduce configuration space accordingly.
  1318. * Return the minimum.
  1319. */
  1320. int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
  1321. struct snd_pcm_hw_params *params,
  1322. snd_pcm_hw_param_t var, int *dir)
  1323. {
  1324. int changed = _snd_pcm_hw_param_first(params, var);
  1325. if (changed < 0)
  1326. return changed;
  1327. if (params->rmask) {
  1328. int err = snd_pcm_hw_refine(pcm, params);
  1329. if (snd_BUG_ON(err < 0))
  1330. return err;
  1331. }
  1332. return snd_pcm_hw_param_value(params, var, dir);
  1333. }
  1334. EXPORT_SYMBOL(snd_pcm_hw_param_first);
  1335. static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
  1336. snd_pcm_hw_param_t var)
  1337. {
  1338. int changed;
  1339. if (hw_is_mask(var))
  1340. changed = snd_mask_refine_last(hw_param_mask(params, var));
  1341. else if (hw_is_interval(var))
  1342. changed = snd_interval_refine_last(hw_param_interval(params, var));
  1343. else
  1344. return -EINVAL;
  1345. if (changed) {
  1346. params->cmask |= 1 << var;
  1347. params->rmask |= 1 << var;
  1348. }
  1349. return changed;
  1350. }
  1351. /**
  1352. * snd_pcm_hw_param_last - refine config space and return maximum value
  1353. * @pcm: PCM instance
  1354. * @params: the hw_params instance
  1355. * @var: parameter to retrieve
  1356. * @dir: pointer to the direction (-1,0,1) or %NULL
  1357. *
  1358. * Inside configuration space defined by @params remove from @var all
  1359. * values < maximum. Reduce configuration space accordingly.
  1360. * Return the maximum.
  1361. */
  1362. int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
  1363. struct snd_pcm_hw_params *params,
  1364. snd_pcm_hw_param_t var, int *dir)
  1365. {
  1366. int changed = _snd_pcm_hw_param_last(params, var);
  1367. if (changed < 0)
  1368. return changed;
  1369. if (params->rmask) {
  1370. int err = snd_pcm_hw_refine(pcm, params);
  1371. if (snd_BUG_ON(err < 0))
  1372. return err;
  1373. }
  1374. return snd_pcm_hw_param_value(params, var, dir);
  1375. }
  1376. EXPORT_SYMBOL(snd_pcm_hw_param_last);
  1377. /**
  1378. * snd_pcm_hw_param_choose - choose a configuration defined by @params
  1379. * @pcm: PCM instance
  1380. * @params: the hw_params instance
  1381. *
  1382. * Choose one configuration from configuration space defined by @params.
  1383. * The configuration chosen is that obtained fixing in this order:
  1384. * first access, first format, first subformat, min channels,
  1385. * min rate, min period time, max buffer size, min tick time
  1386. */
  1387. int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
  1388. struct snd_pcm_hw_params *params)
  1389. {
  1390. static int vars[] = {
  1391. SNDRV_PCM_HW_PARAM_ACCESS,
  1392. SNDRV_PCM_HW_PARAM_FORMAT,
  1393. SNDRV_PCM_HW_PARAM_SUBFORMAT,
  1394. SNDRV_PCM_HW_PARAM_CHANNELS,
  1395. SNDRV_PCM_HW_PARAM_RATE,
  1396. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1397. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1398. SNDRV_PCM_HW_PARAM_TICK_TIME,
  1399. -1
  1400. };
  1401. int err, *v;
  1402. for (v = vars; *v != -1; v++) {
  1403. if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
  1404. err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
  1405. else
  1406. err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
  1407. if (snd_BUG_ON(err < 0))
  1408. return err;
  1409. }
  1410. return 0;
  1411. }
  1412. static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
  1413. void *arg)
  1414. {
  1415. struct snd_pcm_runtime *runtime = substream->runtime;
  1416. unsigned long flags;
  1417. snd_pcm_stream_lock_irqsave(substream, flags);
  1418. if (snd_pcm_running(substream) &&
  1419. snd_pcm_update_hw_ptr(substream) >= 0)
  1420. runtime->status->hw_ptr %= runtime->buffer_size;
  1421. else
  1422. runtime->status->hw_ptr = 0;
  1423. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1424. return 0;
  1425. }
  1426. static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
  1427. void *arg)
  1428. {
  1429. struct snd_pcm_channel_info *info = arg;
  1430. struct snd_pcm_runtime *runtime = substream->runtime;
  1431. int width;
  1432. if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
  1433. info->offset = -1;
  1434. return 0;
  1435. }
  1436. width = snd_pcm_format_physical_width(runtime->format);
  1437. if (width < 0)
  1438. return width;
  1439. info->offset = 0;
  1440. switch (runtime->access) {
  1441. case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
  1442. case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
  1443. info->first = info->channel * width;
  1444. info->step = runtime->channels * width;
  1445. break;
  1446. case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
  1447. case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
  1448. {
  1449. size_t size = runtime->dma_bytes / runtime->channels;
  1450. info->first = info->channel * size * 8;
  1451. info->step = width;
  1452. break;
  1453. }
  1454. default:
  1455. snd_BUG();
  1456. break;
  1457. }
  1458. return 0;
  1459. }
  1460. static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
  1461. void *arg)
  1462. {
  1463. struct snd_pcm_hw_params *params = arg;
  1464. snd_pcm_format_t format;
  1465. int channels, width;
  1466. params->fifo_size = substream->runtime->hw.fifo_size;
  1467. if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
  1468. format = params_format(params);
  1469. channels = params_channels(params);
  1470. width = snd_pcm_format_physical_width(format);
  1471. params->fifo_size /= width * channels;
  1472. }
  1473. return 0;
  1474. }
  1475. /**
  1476. * snd_pcm_lib_ioctl - a generic PCM ioctl callback
  1477. * @substream: the pcm substream instance
  1478. * @cmd: ioctl command
  1479. * @arg: ioctl argument
  1480. *
  1481. * Processes the generic ioctl commands for PCM.
  1482. * Can be passed as the ioctl callback for PCM ops.
  1483. *
  1484. * Returns zero if successful, or a negative error code on failure.
  1485. */
  1486. int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  1487. unsigned int cmd, void *arg)
  1488. {
  1489. switch (cmd) {
  1490. case SNDRV_PCM_IOCTL1_INFO:
  1491. return 0;
  1492. case SNDRV_PCM_IOCTL1_RESET:
  1493. return snd_pcm_lib_ioctl_reset(substream, arg);
  1494. case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
  1495. return snd_pcm_lib_ioctl_channel_info(substream, arg);
  1496. case SNDRV_PCM_IOCTL1_FIFO_SIZE:
  1497. return snd_pcm_lib_ioctl_fifo_size(substream, arg);
  1498. }
  1499. return -ENXIO;
  1500. }
  1501. EXPORT_SYMBOL(snd_pcm_lib_ioctl);
  1502. /**
  1503. * snd_pcm_period_elapsed - update the pcm status for the next period
  1504. * @substream: the pcm substream instance
  1505. *
  1506. * This function is called from the interrupt handler when the
  1507. * PCM has processed the period size. It will update the current
  1508. * pointer, wake up sleepers, etc.
  1509. *
  1510. * Even if more than one periods have elapsed since the last call, you
  1511. * have to call this only once.
  1512. */
  1513. void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
  1514. {
  1515. struct snd_pcm_runtime *runtime;
  1516. unsigned long flags;
  1517. if (PCM_RUNTIME_CHECK(substream))
  1518. return;
  1519. runtime = substream->runtime;
  1520. if (runtime->transfer_ack_begin)
  1521. runtime->transfer_ack_begin(substream);
  1522. snd_pcm_stream_lock_irqsave(substream, flags);
  1523. if (!snd_pcm_running(substream) ||
  1524. snd_pcm_update_hw_ptr_interrupt(substream) < 0)
  1525. goto _end;
  1526. if (substream->timer_running)
  1527. snd_timer_interrupt(substream->timer, 1);
  1528. _end:
  1529. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1530. if (runtime->transfer_ack_end)
  1531. runtime->transfer_ack_end(substream);
  1532. kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
  1533. }
  1534. EXPORT_SYMBOL(snd_pcm_period_elapsed);
  1535. /*
  1536. * Wait until avail_min data becomes available
  1537. * Returns a negative error code if any error occurs during operation.
  1538. * The available space is stored on availp. When err = 0 and avail = 0
  1539. * on the capture stream, it indicates the stream is in DRAINING state.
  1540. */
  1541. static int wait_for_avail_min(struct snd_pcm_substream *substream,
  1542. snd_pcm_uframes_t *availp)
  1543. {
  1544. struct snd_pcm_runtime *runtime = substream->runtime;
  1545. int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  1546. wait_queue_t wait;
  1547. int err = 0;
  1548. snd_pcm_uframes_t avail = 0;
  1549. long tout;
  1550. init_waitqueue_entry(&wait, current);
  1551. add_wait_queue(&runtime->sleep, &wait);
  1552. for (;;) {
  1553. if (signal_pending(current)) {
  1554. err = -ERESTARTSYS;
  1555. break;
  1556. }
  1557. set_current_state(TASK_INTERRUPTIBLE);
  1558. snd_pcm_stream_unlock_irq(substream);
  1559. tout = schedule_timeout(msecs_to_jiffies(10000));
  1560. snd_pcm_stream_lock_irq(substream);
  1561. switch (runtime->status->state) {
  1562. case SNDRV_PCM_STATE_SUSPENDED:
  1563. err = -ESTRPIPE;
  1564. goto _endloop;
  1565. case SNDRV_PCM_STATE_XRUN:
  1566. err = -EPIPE;
  1567. goto _endloop;
  1568. case SNDRV_PCM_STATE_DRAINING:
  1569. if (is_playback)
  1570. err = -EPIPE;
  1571. else
  1572. avail = 0; /* indicate draining */
  1573. goto _endloop;
  1574. case SNDRV_PCM_STATE_OPEN:
  1575. case SNDRV_PCM_STATE_SETUP:
  1576. case SNDRV_PCM_STATE_DISCONNECTED:
  1577. err = -EBADFD;
  1578. goto _endloop;
  1579. }
  1580. if (!tout) {
  1581. snd_printd("%s write error (DMA or IRQ trouble?)\n",
  1582. is_playback ? "playback" : "capture");
  1583. err = -EIO;
  1584. break;
  1585. }
  1586. if (is_playback)
  1587. avail = snd_pcm_playback_avail(runtime);
  1588. else
  1589. avail = snd_pcm_capture_avail(runtime);
  1590. if (avail >= runtime->control->avail_min)
  1591. break;
  1592. }
  1593. _endloop:
  1594. remove_wait_queue(&runtime->sleep, &wait);
  1595. *availp = avail;
  1596. return err;
  1597. }
  1598. static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
  1599. unsigned int hwoff,
  1600. unsigned long data, unsigned int off,
  1601. snd_pcm_uframes_t frames)
  1602. {
  1603. struct snd_pcm_runtime *runtime = substream->runtime;
  1604. int err;
  1605. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  1606. if (substream->ops->copy) {
  1607. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  1608. return err;
  1609. } else {
  1610. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  1611. if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
  1612. return -EFAULT;
  1613. }
  1614. return 0;
  1615. }
  1616. typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
  1617. unsigned long data, unsigned int off,
  1618. snd_pcm_uframes_t size);
  1619. static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
  1620. unsigned long data,
  1621. snd_pcm_uframes_t size,
  1622. int nonblock,
  1623. transfer_f transfer)
  1624. {
  1625. struct snd_pcm_runtime *runtime = substream->runtime;
  1626. snd_pcm_uframes_t xfer = 0;
  1627. snd_pcm_uframes_t offset = 0;
  1628. int err = 0;
  1629. if (size == 0)
  1630. return 0;
  1631. snd_pcm_stream_lock_irq(substream);
  1632. switch (runtime->status->state) {
  1633. case SNDRV_PCM_STATE_PREPARED:
  1634. case SNDRV_PCM_STATE_RUNNING:
  1635. case SNDRV_PCM_STATE_PAUSED:
  1636. break;
  1637. case SNDRV_PCM_STATE_XRUN:
  1638. err = -EPIPE;
  1639. goto _end_unlock;
  1640. case SNDRV_PCM_STATE_SUSPENDED:
  1641. err = -ESTRPIPE;
  1642. goto _end_unlock;
  1643. default:
  1644. err = -EBADFD;
  1645. goto _end_unlock;
  1646. }
  1647. while (size > 0) {
  1648. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  1649. snd_pcm_uframes_t avail;
  1650. snd_pcm_uframes_t cont;
  1651. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1652. snd_pcm_update_hw_ptr(substream);
  1653. avail = snd_pcm_playback_avail(runtime);
  1654. if (!avail) {
  1655. if (nonblock) {
  1656. err = -EAGAIN;
  1657. goto _end_unlock;
  1658. }
  1659. err = wait_for_avail_min(substream, &avail);
  1660. if (err < 0)
  1661. goto _end_unlock;
  1662. }
  1663. frames = size > avail ? avail : size;
  1664. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  1665. if (frames > cont)
  1666. frames = cont;
  1667. if (snd_BUG_ON(!frames)) {
  1668. snd_pcm_stream_unlock_irq(substream);
  1669. return -EINVAL;
  1670. }
  1671. appl_ptr = runtime->control->appl_ptr;
  1672. appl_ofs = appl_ptr % runtime->buffer_size;
  1673. snd_pcm_stream_unlock_irq(substream);
  1674. if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
  1675. goto _end;
  1676. snd_pcm_stream_lock_irq(substream);
  1677. switch (runtime->status->state) {
  1678. case SNDRV_PCM_STATE_XRUN:
  1679. err = -EPIPE;
  1680. goto _end_unlock;
  1681. case SNDRV_PCM_STATE_SUSPENDED:
  1682. err = -ESTRPIPE;
  1683. goto _end_unlock;
  1684. default:
  1685. break;
  1686. }
  1687. appl_ptr += frames;
  1688. if (appl_ptr >= runtime->boundary)
  1689. appl_ptr -= runtime->boundary;
  1690. runtime->control->appl_ptr = appl_ptr;
  1691. if (substream->ops->ack)
  1692. substream->ops->ack(substream);
  1693. offset += frames;
  1694. size -= frames;
  1695. xfer += frames;
  1696. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
  1697. snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
  1698. err = snd_pcm_start(substream);
  1699. if (err < 0)
  1700. goto _end_unlock;
  1701. }
  1702. }
  1703. _end_unlock:
  1704. snd_pcm_stream_unlock_irq(substream);
  1705. _end:
  1706. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  1707. }
  1708. /* sanity-check for read/write methods */
  1709. static int pcm_sanity_check(struct snd_pcm_substream *substream)
  1710. {
  1711. struct snd_pcm_runtime *runtime;
  1712. if (PCM_RUNTIME_CHECK(substream))
  1713. return -ENXIO;
  1714. runtime = substream->runtime;
  1715. if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
  1716. return -EINVAL;
  1717. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1718. return -EBADFD;
  1719. return 0;
  1720. }
  1721. snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
  1722. {
  1723. struct snd_pcm_runtime *runtime;
  1724. int nonblock;
  1725. int err;
  1726. err = pcm_sanity_check(substream);
  1727. if (err < 0)
  1728. return err;
  1729. runtime = substream->runtime;
  1730. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1731. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
  1732. runtime->channels > 1)
  1733. return -EINVAL;
  1734. return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
  1735. snd_pcm_lib_write_transfer);
  1736. }
  1737. EXPORT_SYMBOL(snd_pcm_lib_write);
  1738. static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
  1739. unsigned int hwoff,
  1740. unsigned long data, unsigned int off,
  1741. snd_pcm_uframes_t frames)
  1742. {
  1743. struct snd_pcm_runtime *runtime = substream->runtime;
  1744. int err;
  1745. void __user **bufs = (void __user **)data;
  1746. int channels = runtime->channels;
  1747. int c;
  1748. if (substream->ops->copy) {
  1749. if (snd_BUG_ON(!substream->ops->silence))
  1750. return -EINVAL;
  1751. for (c = 0; c < channels; ++c, ++bufs) {
  1752. if (*bufs == NULL) {
  1753. if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
  1754. return err;
  1755. } else {
  1756. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  1757. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  1758. return err;
  1759. }
  1760. }
  1761. } else {
  1762. /* default transfer behaviour */
  1763. size_t dma_csize = runtime->dma_bytes / channels;
  1764. for (c = 0; c < channels; ++c, ++bufs) {
  1765. char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  1766. if (*bufs == NULL) {
  1767. snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
  1768. } else {
  1769. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  1770. if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
  1771. return -EFAULT;
  1772. }
  1773. }
  1774. }
  1775. return 0;
  1776. }
  1777. snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
  1778. void __user **bufs,
  1779. snd_pcm_uframes_t frames)
  1780. {
  1781. struct snd_pcm_runtime *runtime;
  1782. int nonblock;
  1783. int err;
  1784. err = pcm_sanity_check(substream);
  1785. if (err < 0)
  1786. return err;
  1787. runtime = substream->runtime;
  1788. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1789. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  1790. return -EINVAL;
  1791. return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
  1792. nonblock, snd_pcm_lib_writev_transfer);
  1793. }
  1794. EXPORT_SYMBOL(snd_pcm_lib_writev);
  1795. static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
  1796. unsigned int hwoff,
  1797. unsigned long data, unsigned int off,
  1798. snd_pcm_uframes_t frames)
  1799. {
  1800. struct snd_pcm_runtime *runtime = substream->runtime;
  1801. int err;
  1802. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  1803. if (substream->ops->copy) {
  1804. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  1805. return err;
  1806. } else {
  1807. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  1808. if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
  1809. return -EFAULT;
  1810. }
  1811. return 0;
  1812. }
  1813. static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
  1814. unsigned long data,
  1815. snd_pcm_uframes_t size,
  1816. int nonblock,
  1817. transfer_f transfer)
  1818. {
  1819. struct snd_pcm_runtime *runtime = substream->runtime;
  1820. snd_pcm_uframes_t xfer = 0;
  1821. snd_pcm_uframes_t offset = 0;
  1822. int err = 0;
  1823. if (size == 0)
  1824. return 0;
  1825. snd_pcm_stream_lock_irq(substream);
  1826. switch (runtime->status->state) {
  1827. case SNDRV_PCM_STATE_PREPARED:
  1828. if (size >= runtime->start_threshold) {
  1829. err = snd_pcm_start(substream);
  1830. if (err < 0)
  1831. goto _end_unlock;
  1832. }
  1833. break;
  1834. case SNDRV_PCM_STATE_DRAINING:
  1835. case SNDRV_PCM_STATE_RUNNING:
  1836. case SNDRV_PCM_STATE_PAUSED:
  1837. break;
  1838. case SNDRV_PCM_STATE_XRUN:
  1839. err = -EPIPE;
  1840. goto _end_unlock;
  1841. case SNDRV_PCM_STATE_SUSPENDED:
  1842. err = -ESTRPIPE;
  1843. goto _end_unlock;
  1844. default:
  1845. err = -EBADFD;
  1846. goto _end_unlock;
  1847. }
  1848. while (size > 0) {
  1849. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  1850. snd_pcm_uframes_t avail;
  1851. snd_pcm_uframes_t cont;
  1852. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1853. snd_pcm_update_hw_ptr(substream);
  1854. avail = snd_pcm_capture_avail(runtime);
  1855. if (!avail) {
  1856. if (runtime->status->state ==
  1857. SNDRV_PCM_STATE_DRAINING) {
  1858. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1859. goto _end_unlock;
  1860. }
  1861. if (nonblock) {
  1862. err = -EAGAIN;
  1863. goto _end_unlock;
  1864. }
  1865. err = wait_for_avail_min(substream, &avail);
  1866. if (err < 0)
  1867. goto _end_unlock;
  1868. if (!avail)
  1869. continue; /* draining */
  1870. }
  1871. frames = size > avail ? avail : size;
  1872. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  1873. if (frames > cont)
  1874. frames = cont;
  1875. if (snd_BUG_ON(!frames)) {
  1876. snd_pcm_stream_unlock_irq(substream);
  1877. return -EINVAL;
  1878. }
  1879. appl_ptr = runtime->control->appl_ptr;
  1880. appl_ofs = appl_ptr % runtime->buffer_size;
  1881. snd_pcm_stream_unlock_irq(substream);
  1882. if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
  1883. goto _end;
  1884. snd_pcm_stream_lock_irq(substream);
  1885. switch (runtime->status->state) {
  1886. case SNDRV_PCM_STATE_XRUN:
  1887. err = -EPIPE;
  1888. goto _end_unlock;
  1889. case SNDRV_PCM_STATE_SUSPENDED:
  1890. err = -ESTRPIPE;
  1891. goto _end_unlock;
  1892. default:
  1893. break;
  1894. }
  1895. appl_ptr += frames;
  1896. if (appl_ptr >= runtime->boundary)
  1897. appl_ptr -= runtime->boundary;
  1898. runtime->control->appl_ptr = appl_ptr;
  1899. if (substream->ops->ack)
  1900. substream->ops->ack(substream);
  1901. offset += frames;
  1902. size -= frames;
  1903. xfer += frames;
  1904. }
  1905. _end_unlock:
  1906. snd_pcm_stream_unlock_irq(substream);
  1907. _end:
  1908. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  1909. }
  1910. snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
  1911. {
  1912. struct snd_pcm_runtime *runtime;
  1913. int nonblock;
  1914. int err;
  1915. err = pcm_sanity_check(substream);
  1916. if (err < 0)
  1917. return err;
  1918. runtime = substream->runtime;
  1919. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1920. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
  1921. return -EINVAL;
  1922. return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
  1923. }
  1924. EXPORT_SYMBOL(snd_pcm_lib_read);
  1925. static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
  1926. unsigned int hwoff,
  1927. unsigned long data, unsigned int off,
  1928. snd_pcm_uframes_t frames)
  1929. {
  1930. struct snd_pcm_runtime *runtime = substream->runtime;
  1931. int err;
  1932. void __user **bufs = (void __user **)data;
  1933. int channels = runtime->channels;
  1934. int c;
  1935. if (substream->ops->copy) {
  1936. for (c = 0; c < channels; ++c, ++bufs) {
  1937. char __user *buf;
  1938. if (*bufs == NULL)
  1939. continue;
  1940. buf = *bufs + samples_to_bytes(runtime, off);
  1941. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  1942. return err;
  1943. }
  1944. } else {
  1945. snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
  1946. for (c = 0; c < channels; ++c, ++bufs) {
  1947. char *hwbuf;
  1948. char __user *buf;
  1949. if (*bufs == NULL)
  1950. continue;
  1951. hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  1952. buf = *bufs + samples_to_bytes(runtime, off);
  1953. if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
  1954. return -EFAULT;
  1955. }
  1956. }
  1957. return 0;
  1958. }
  1959. snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
  1960. void __user **bufs,
  1961. snd_pcm_uframes_t frames)
  1962. {
  1963. struct snd_pcm_runtime *runtime;
  1964. int nonblock;
  1965. int err;
  1966. err = pcm_sanity_check(substream);
  1967. if (err < 0)
  1968. return err;
  1969. runtime = substream->runtime;
  1970. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1971. return -EBADFD;
  1972. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1973. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  1974. return -EINVAL;
  1975. return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
  1976. }
  1977. EXPORT_SYMBOL(snd_pcm_lib_readv);