pcm_lib.c 55 KB

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