pcm_lib.c 57 KB

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