pcm_lib.c 52 KB

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