pcm_lib.c 55 KB

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