pcm_lib.c 56 KB

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