pcm_lib.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686
  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 = (ofs + frames) - runtime->buffer_size;
  80. } else {
  81. runtime->silence_start = ofs - runtime->silence_filled;
  82. }
  83. if ((snd_pcm_sframes_t)runtime->silence_start < 0)
  84. runtime->silence_start += runtime->boundary;
  85. }
  86. frames = runtime->buffer_size - runtime->silence_filled;
  87. }
  88. snd_assert(frames <= runtime->buffer_size, return);
  89. if (frames == 0)
  90. return;
  91. ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size;
  92. while (frames > 0) {
  93. transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
  94. if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
  95. runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
  96. if (substream->ops->silence) {
  97. int err;
  98. err = substream->ops->silence(substream, -1, ofs, transfer);
  99. snd_assert(err >= 0, );
  100. } else {
  101. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
  102. snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
  103. }
  104. } else {
  105. unsigned int c;
  106. unsigned int channels = runtime->channels;
  107. if (substream->ops->silence) {
  108. for (c = 0; c < channels; ++c) {
  109. int err;
  110. err = substream->ops->silence(substream, c, ofs, transfer);
  111. snd_assert(err >= 0, );
  112. }
  113. } else {
  114. size_t dma_csize = runtime->dma_bytes / channels;
  115. for (c = 0; c < channels; ++c) {
  116. char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
  117. snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
  118. }
  119. }
  120. }
  121. runtime->silence_filled += transfer;
  122. frames -= transfer;
  123. ofs = 0;
  124. }
  125. }
  126. static void xrun(struct snd_pcm_substream *substream)
  127. {
  128. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  129. #ifdef CONFIG_SND_DEBUG
  130. if (substream->pstr->xrun_debug) {
  131. snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
  132. substream->pcm->card->number,
  133. substream->pcm->device,
  134. substream->stream ? 'c' : 'p');
  135. if (substream->pstr->xrun_debug > 1)
  136. dump_stack();
  137. }
  138. #endif
  139. }
  140. static inline snd_pcm_uframes_t snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
  141. struct snd_pcm_runtime *runtime)
  142. {
  143. snd_pcm_uframes_t pos;
  144. pos = substream->ops->pointer(substream);
  145. if (pos == SNDRV_PCM_POS_XRUN)
  146. return pos; /* XRUN */
  147. if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
  148. getnstimeofday((struct timespec *)&runtime->status->tstamp);
  149. #ifdef CONFIG_SND_DEBUG
  150. if (pos >= runtime->buffer_size) {
  151. 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);
  152. }
  153. #endif
  154. pos -= pos % runtime->min_align;
  155. return pos;
  156. }
  157. static inline int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
  158. struct snd_pcm_runtime *runtime)
  159. {
  160. snd_pcm_uframes_t avail;
  161. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  162. avail = snd_pcm_playback_avail(runtime);
  163. else
  164. avail = snd_pcm_capture_avail(runtime);
  165. if (avail > runtime->avail_max)
  166. runtime->avail_max = avail;
  167. if (avail >= runtime->stop_threshold) {
  168. if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
  169. snd_pcm_drain_done(substream);
  170. else
  171. xrun(substream);
  172. return -EPIPE;
  173. }
  174. if (avail >= runtime->control->avail_min)
  175. wake_up(&runtime->sleep);
  176. return 0;
  177. }
  178. static inline int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
  179. {
  180. struct snd_pcm_runtime *runtime = substream->runtime;
  181. snd_pcm_uframes_t pos;
  182. snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt;
  183. snd_pcm_sframes_t delta;
  184. pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
  185. if (pos == SNDRV_PCM_POS_XRUN) {
  186. xrun(substream);
  187. return -EPIPE;
  188. }
  189. if (runtime->period_size == runtime->buffer_size)
  190. goto __next_buf;
  191. new_hw_ptr = runtime->hw_ptr_base + pos;
  192. hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
  193. delta = hw_ptr_interrupt - new_hw_ptr;
  194. if (delta > 0) {
  195. if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
  196. #ifdef CONFIG_SND_DEBUG
  197. if (runtime->periods > 1 && substream->pstr->xrun_debug) {
  198. 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);
  199. if (substream->pstr->xrun_debug > 1)
  200. dump_stack();
  201. }
  202. #endif
  203. return 0;
  204. }
  205. __next_buf:
  206. runtime->hw_ptr_base += runtime->buffer_size;
  207. if (runtime->hw_ptr_base == runtime->boundary)
  208. runtime->hw_ptr_base = 0;
  209. new_hw_ptr = runtime->hw_ptr_base + pos;
  210. }
  211. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  212. runtime->silence_size > 0)
  213. snd_pcm_playback_silence(substream, new_hw_ptr);
  214. runtime->status->hw_ptr = new_hw_ptr;
  215. runtime->hw_ptr_interrupt = new_hw_ptr - new_hw_ptr % runtime->period_size;
  216. return snd_pcm_update_hw_ptr_post(substream, runtime);
  217. }
  218. /* CAUTION: call it with irq disabled */
  219. int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
  220. {
  221. struct snd_pcm_runtime *runtime = substream->runtime;
  222. snd_pcm_uframes_t pos;
  223. snd_pcm_uframes_t old_hw_ptr, new_hw_ptr;
  224. snd_pcm_sframes_t delta;
  225. old_hw_ptr = runtime->status->hw_ptr;
  226. pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
  227. if (pos == SNDRV_PCM_POS_XRUN) {
  228. xrun(substream);
  229. return -EPIPE;
  230. }
  231. new_hw_ptr = runtime->hw_ptr_base + pos;
  232. delta = old_hw_ptr - new_hw_ptr;
  233. if (delta > 0) {
  234. if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
  235. #ifdef CONFIG_SND_DEBUG
  236. if (runtime->periods > 2 && substream->pstr->xrun_debug) {
  237. 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);
  238. if (substream->pstr->xrun_debug > 1)
  239. dump_stack();
  240. }
  241. #endif
  242. return 0;
  243. }
  244. runtime->hw_ptr_base += runtime->buffer_size;
  245. if (runtime->hw_ptr_base == runtime->boundary)
  246. runtime->hw_ptr_base = 0;
  247. new_hw_ptr = runtime->hw_ptr_base + pos;
  248. }
  249. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  250. runtime->silence_size > 0)
  251. snd_pcm_playback_silence(substream, new_hw_ptr);
  252. runtime->status->hw_ptr = new_hw_ptr;
  253. return snd_pcm_update_hw_ptr_post(substream, runtime);
  254. }
  255. /**
  256. * snd_pcm_set_ops - set the PCM operators
  257. * @pcm: the pcm instance
  258. * @direction: stream direction, SNDRV_PCM_STREAM_XXX
  259. * @ops: the operator table
  260. *
  261. * Sets the given PCM operators to the pcm instance.
  262. */
  263. void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
  264. {
  265. struct snd_pcm_str *stream = &pcm->streams[direction];
  266. struct snd_pcm_substream *substream;
  267. for (substream = stream->substream; substream != NULL; substream = substream->next)
  268. substream->ops = ops;
  269. }
  270. /**
  271. * snd_pcm_sync - set the PCM sync id
  272. * @substream: the pcm substream
  273. *
  274. * Sets the PCM sync identifier for the card.
  275. */
  276. void snd_pcm_set_sync(struct snd_pcm_substream *substream)
  277. {
  278. struct snd_pcm_runtime *runtime = substream->runtime;
  279. runtime->sync.id32[0] = substream->pcm->card->number;
  280. runtime->sync.id32[1] = -1;
  281. runtime->sync.id32[2] = -1;
  282. runtime->sync.id32[3] = -1;
  283. }
  284. /*
  285. * Standard ioctl routine
  286. */
  287. /* Code taken from alsa-lib */
  288. #define assert(a) snd_assert((a), return -EINVAL)
  289. static inline unsigned int div32(unsigned int a, unsigned int b,
  290. unsigned int *r)
  291. {
  292. if (b == 0) {
  293. *r = 0;
  294. return UINT_MAX;
  295. }
  296. *r = a % b;
  297. return a / b;
  298. }
  299. static inline unsigned int div_down(unsigned int a, unsigned int b)
  300. {
  301. if (b == 0)
  302. return UINT_MAX;
  303. return a / b;
  304. }
  305. static inline unsigned int div_up(unsigned int a, unsigned int b)
  306. {
  307. unsigned int r;
  308. unsigned int q;
  309. if (b == 0)
  310. return UINT_MAX;
  311. q = div32(a, b, &r);
  312. if (r)
  313. ++q;
  314. return q;
  315. }
  316. static inline unsigned int mul(unsigned int a, unsigned int b)
  317. {
  318. if (a == 0)
  319. return 0;
  320. if (div_down(UINT_MAX, a) < b)
  321. return UINT_MAX;
  322. return a * b;
  323. }
  324. static inline unsigned int muldiv32(unsigned int a, unsigned int b,
  325. unsigned int c, unsigned int *r)
  326. {
  327. u_int64_t n = (u_int64_t) a * b;
  328. if (c == 0) {
  329. snd_assert(n > 0, );
  330. *r = 0;
  331. return UINT_MAX;
  332. }
  333. div64_32(&n, c, r);
  334. if (n >= UINT_MAX) {
  335. *r = 0;
  336. return UINT_MAX;
  337. }
  338. return n;
  339. }
  340. static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin)
  341. {
  342. int changed = 0;
  343. assert(!snd_interval_empty(i));
  344. if (i->min < min) {
  345. i->min = min;
  346. i->openmin = openmin;
  347. changed = 1;
  348. } else if (i->min == min && !i->openmin && openmin) {
  349. i->openmin = 1;
  350. changed = 1;
  351. }
  352. if (i->integer) {
  353. if (i->openmin) {
  354. i->min++;
  355. i->openmin = 0;
  356. }
  357. }
  358. if (snd_interval_checkempty(i)) {
  359. snd_interval_none(i);
  360. return -EINVAL;
  361. }
  362. return changed;
  363. }
  364. static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax)
  365. {
  366. int changed = 0;
  367. assert(!snd_interval_empty(i));
  368. if (i->max > max) {
  369. i->max = max;
  370. i->openmax = openmax;
  371. changed = 1;
  372. } else if (i->max == max && !i->openmax && openmax) {
  373. i->openmax = 1;
  374. changed = 1;
  375. }
  376. if (i->integer) {
  377. if (i->openmax) {
  378. i->max--;
  379. i->openmax = 0;
  380. }
  381. }
  382. if (snd_interval_checkempty(i)) {
  383. snd_interval_none(i);
  384. return -EINVAL;
  385. }
  386. return changed;
  387. }
  388. /**
  389. * snd_interval_refine - refine the interval value of configurator
  390. * @i: the interval value to refine
  391. * @v: the interval value to refer to
  392. *
  393. * Refines the interval value with the reference value.
  394. * The interval is changed to the range satisfying both intervals.
  395. * The interval status (min, max, integer, etc.) are evaluated.
  396. *
  397. * Returns non-zero if the value is changed, zero if not changed.
  398. */
  399. int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
  400. {
  401. int changed = 0;
  402. assert(!snd_interval_empty(i));
  403. if (i->min < v->min) {
  404. i->min = v->min;
  405. i->openmin = v->openmin;
  406. changed = 1;
  407. } else if (i->min == v->min && !i->openmin && v->openmin) {
  408. i->openmin = 1;
  409. changed = 1;
  410. }
  411. if (i->max > v->max) {
  412. i->max = v->max;
  413. i->openmax = v->openmax;
  414. changed = 1;
  415. } else if (i->max == v->max && !i->openmax && v->openmax) {
  416. i->openmax = 1;
  417. changed = 1;
  418. }
  419. if (!i->integer && v->integer) {
  420. i->integer = 1;
  421. changed = 1;
  422. }
  423. if (i->integer) {
  424. if (i->openmin) {
  425. i->min++;
  426. i->openmin = 0;
  427. }
  428. if (i->openmax) {
  429. i->max--;
  430. i->openmax = 0;
  431. }
  432. } else if (!i->openmin && !i->openmax && i->min == i->max)
  433. i->integer = 1;
  434. if (snd_interval_checkempty(i)) {
  435. snd_interval_none(i);
  436. return -EINVAL;
  437. }
  438. return changed;
  439. }
  440. static int snd_interval_refine_first(struct snd_interval *i)
  441. {
  442. assert(!snd_interval_empty(i));
  443. if (snd_interval_single(i))
  444. return 0;
  445. i->max = i->min;
  446. i->openmax = i->openmin;
  447. if (i->openmax)
  448. i->max++;
  449. return 1;
  450. }
  451. static int snd_interval_refine_last(struct snd_interval *i)
  452. {
  453. assert(!snd_interval_empty(i));
  454. if (snd_interval_single(i))
  455. return 0;
  456. i->min = i->max;
  457. i->openmin = i->openmax;
  458. if (i->openmin)
  459. i->min--;
  460. return 1;
  461. }
  462. static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
  463. {
  464. struct snd_interval t;
  465. t.empty = 0;
  466. t.min = t.max = val;
  467. t.openmin = t.openmax = 0;
  468. t.integer = 1;
  469. return snd_interval_refine(i, &t);
  470. }
  471. void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
  472. {
  473. if (a->empty || b->empty) {
  474. snd_interval_none(c);
  475. return;
  476. }
  477. c->empty = 0;
  478. c->min = mul(a->min, b->min);
  479. c->openmin = (a->openmin || b->openmin);
  480. c->max = mul(a->max, b->max);
  481. c->openmax = (a->openmax || b->openmax);
  482. c->integer = (a->integer && b->integer);
  483. }
  484. /**
  485. * snd_interval_div - refine the interval value with division
  486. * @a: dividend
  487. * @b: divisor
  488. * @c: quotient
  489. *
  490. * c = a / b
  491. *
  492. * Returns non-zero if the value is changed, zero if not changed.
  493. */
  494. void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
  495. {
  496. unsigned int r;
  497. if (a->empty || b->empty) {
  498. snd_interval_none(c);
  499. return;
  500. }
  501. c->empty = 0;
  502. c->min = div32(a->min, b->max, &r);
  503. c->openmin = (r || a->openmin || b->openmax);
  504. if (b->min > 0) {
  505. c->max = div32(a->max, b->min, &r);
  506. if (r) {
  507. c->max++;
  508. c->openmax = 1;
  509. } else
  510. c->openmax = (a->openmax || b->openmin);
  511. } else {
  512. c->max = UINT_MAX;
  513. c->openmax = 0;
  514. }
  515. c->integer = 0;
  516. }
  517. /**
  518. * snd_interval_muldivk - refine the interval value
  519. * @a: dividend 1
  520. * @b: dividend 2
  521. * @k: divisor (as integer)
  522. * @c: result
  523. *
  524. * c = a * b / k
  525. *
  526. * Returns non-zero if the value is changed, zero if not changed.
  527. */
  528. void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
  529. unsigned int k, struct snd_interval *c)
  530. {
  531. unsigned int r;
  532. if (a->empty || b->empty) {
  533. snd_interval_none(c);
  534. return;
  535. }
  536. c->empty = 0;
  537. c->min = muldiv32(a->min, b->min, k, &r);
  538. c->openmin = (r || a->openmin || b->openmin);
  539. c->max = muldiv32(a->max, b->max, k, &r);
  540. if (r) {
  541. c->max++;
  542. c->openmax = 1;
  543. } else
  544. c->openmax = (a->openmax || b->openmax);
  545. c->integer = 0;
  546. }
  547. /**
  548. * snd_interval_mulkdiv - refine the interval value
  549. * @a: dividend 1
  550. * @k: dividend 2 (as integer)
  551. * @b: divisor
  552. * @c: result
  553. *
  554. * c = a * k / b
  555. *
  556. * Returns non-zero if the value is changed, zero if not changed.
  557. */
  558. void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
  559. const struct snd_interval *b, struct snd_interval *c)
  560. {
  561. unsigned int r;
  562. if (a->empty || b->empty) {
  563. snd_interval_none(c);
  564. return;
  565. }
  566. c->empty = 0;
  567. c->min = muldiv32(a->min, k, b->max, &r);
  568. c->openmin = (r || a->openmin || b->openmax);
  569. if (b->min > 0) {
  570. c->max = muldiv32(a->max, k, b->min, &r);
  571. if (r) {
  572. c->max++;
  573. c->openmax = 1;
  574. } else
  575. c->openmax = (a->openmax || b->openmin);
  576. } else {
  577. c->max = UINT_MAX;
  578. c->openmax = 0;
  579. }
  580. c->integer = 0;
  581. }
  582. #undef assert
  583. /* ---- */
  584. /**
  585. * snd_interval_ratnum - refine the interval value
  586. * @i: interval to refine
  587. * @rats_count: number of ratnum_t
  588. * @rats: ratnum_t array
  589. * @nump: pointer to store the resultant numerator
  590. * @denp: pointer to store the resultant denominator
  591. *
  592. * Returns non-zero if the value is changed, zero if not changed.
  593. */
  594. int snd_interval_ratnum(struct snd_interval *i,
  595. unsigned int rats_count, struct snd_ratnum *rats,
  596. unsigned int *nump, unsigned int *denp)
  597. {
  598. unsigned int best_num, best_diff, best_den;
  599. unsigned int k;
  600. struct snd_interval t;
  601. int err;
  602. best_num = best_den = best_diff = 0;
  603. for (k = 0; k < rats_count; ++k) {
  604. unsigned int num = rats[k].num;
  605. unsigned int den;
  606. unsigned int q = i->min;
  607. int diff;
  608. if (q == 0)
  609. q = 1;
  610. den = div_down(num, q);
  611. if (den < rats[k].den_min)
  612. continue;
  613. if (den > rats[k].den_max)
  614. den = rats[k].den_max;
  615. else {
  616. unsigned int r;
  617. r = (den - rats[k].den_min) % rats[k].den_step;
  618. if (r != 0)
  619. den -= r;
  620. }
  621. diff = num - q * den;
  622. if (best_num == 0 ||
  623. diff * best_den < best_diff * den) {
  624. best_diff = diff;
  625. best_den = den;
  626. best_num = num;
  627. }
  628. }
  629. if (best_den == 0) {
  630. i->empty = 1;
  631. return -EINVAL;
  632. }
  633. t.min = div_down(best_num, best_den);
  634. t.openmin = !!(best_num % best_den);
  635. best_num = best_den = best_diff = 0;
  636. for (k = 0; k < rats_count; ++k) {
  637. unsigned int num = rats[k].num;
  638. unsigned int den;
  639. unsigned int q = i->max;
  640. int diff;
  641. if (q == 0) {
  642. i->empty = 1;
  643. return -EINVAL;
  644. }
  645. den = div_up(num, q);
  646. if (den > rats[k].den_max)
  647. continue;
  648. if (den < rats[k].den_min)
  649. den = rats[k].den_min;
  650. else {
  651. unsigned int r;
  652. r = (den - rats[k].den_min) % rats[k].den_step;
  653. if (r != 0)
  654. den += rats[k].den_step - r;
  655. }
  656. diff = q * den - num;
  657. if (best_num == 0 ||
  658. diff * best_den < best_diff * den) {
  659. best_diff = diff;
  660. best_den = den;
  661. best_num = num;
  662. }
  663. }
  664. if (best_den == 0) {
  665. i->empty = 1;
  666. return -EINVAL;
  667. }
  668. t.max = div_up(best_num, best_den);
  669. t.openmax = !!(best_num % best_den);
  670. t.integer = 0;
  671. err = snd_interval_refine(i, &t);
  672. if (err < 0)
  673. return err;
  674. if (snd_interval_single(i)) {
  675. if (nump)
  676. *nump = best_num;
  677. if (denp)
  678. *denp = best_den;
  679. }
  680. return err;
  681. }
  682. /**
  683. * snd_interval_ratden - refine the interval value
  684. * @i: interval to refine
  685. * @rats_count: number of struct ratden
  686. * @rats: struct ratden array
  687. * @nump: pointer to store the resultant numerator
  688. * @denp: pointer to store the resultant denominator
  689. *
  690. * Returns non-zero if the value is changed, zero if not changed.
  691. */
  692. static int snd_interval_ratden(struct snd_interval *i,
  693. unsigned int rats_count, struct snd_ratden *rats,
  694. unsigned int *nump, unsigned int *denp)
  695. {
  696. unsigned int best_num, best_diff, best_den;
  697. unsigned int k;
  698. struct snd_interval t;
  699. int err;
  700. best_num = best_den = best_diff = 0;
  701. for (k = 0; k < rats_count; ++k) {
  702. unsigned int num;
  703. unsigned int den = rats[k].den;
  704. unsigned int q = i->min;
  705. int diff;
  706. num = mul(q, den);
  707. if (num > rats[k].num_max)
  708. continue;
  709. if (num < rats[k].num_min)
  710. num = rats[k].num_max;
  711. else {
  712. unsigned int r;
  713. r = (num - rats[k].num_min) % rats[k].num_step;
  714. if (r != 0)
  715. num += rats[k].num_step - r;
  716. }
  717. diff = num - q * den;
  718. if (best_num == 0 ||
  719. diff * best_den < best_diff * den) {
  720. best_diff = diff;
  721. best_den = den;
  722. best_num = num;
  723. }
  724. }
  725. if (best_den == 0) {
  726. i->empty = 1;
  727. return -EINVAL;
  728. }
  729. t.min = div_down(best_num, best_den);
  730. t.openmin = !!(best_num % best_den);
  731. best_num = best_den = best_diff = 0;
  732. for (k = 0; k < rats_count; ++k) {
  733. unsigned int num;
  734. unsigned int den = rats[k].den;
  735. unsigned int q = i->max;
  736. int diff;
  737. num = mul(q, den);
  738. if (num < rats[k].num_min)
  739. continue;
  740. if (num > rats[k].num_max)
  741. num = rats[k].num_max;
  742. else {
  743. unsigned int r;
  744. r = (num - rats[k].num_min) % rats[k].num_step;
  745. if (r != 0)
  746. num -= r;
  747. }
  748. diff = q * den - num;
  749. if (best_num == 0 ||
  750. diff * best_den < best_diff * den) {
  751. best_diff = diff;
  752. best_den = den;
  753. best_num = num;
  754. }
  755. }
  756. if (best_den == 0) {
  757. i->empty = 1;
  758. return -EINVAL;
  759. }
  760. t.max = div_up(best_num, best_den);
  761. t.openmax = !!(best_num % best_den);
  762. t.integer = 0;
  763. err = snd_interval_refine(i, &t);
  764. if (err < 0)
  765. return err;
  766. if (snd_interval_single(i)) {
  767. if (nump)
  768. *nump = best_num;
  769. if (denp)
  770. *denp = best_den;
  771. }
  772. return err;
  773. }
  774. /**
  775. * snd_interval_list - refine the interval value from the list
  776. * @i: the interval value to refine
  777. * @count: the number of elements in the list
  778. * @list: the value list
  779. * @mask: the bit-mask to evaluate
  780. *
  781. * Refines the interval value from the list.
  782. * When mask is non-zero, only the elements corresponding to bit 1 are
  783. * evaluated.
  784. *
  785. * Returns non-zero if the value is changed, zero if not changed.
  786. */
  787. int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
  788. {
  789. unsigned int k;
  790. int changed = 0;
  791. for (k = 0; k < count; k++) {
  792. if (mask && !(mask & (1 << k)))
  793. continue;
  794. if (i->min == list[k] && !i->openmin)
  795. goto _l1;
  796. if (i->min < list[k]) {
  797. i->min = list[k];
  798. i->openmin = 0;
  799. changed = 1;
  800. goto _l1;
  801. }
  802. }
  803. i->empty = 1;
  804. return -EINVAL;
  805. _l1:
  806. for (k = count; k-- > 0;) {
  807. if (mask && !(mask & (1 << k)))
  808. continue;
  809. if (i->max == list[k] && !i->openmax)
  810. goto _l2;
  811. if (i->max > list[k]) {
  812. i->max = list[k];
  813. i->openmax = 0;
  814. changed = 1;
  815. goto _l2;
  816. }
  817. }
  818. i->empty = 1;
  819. return -EINVAL;
  820. _l2:
  821. if (snd_interval_checkempty(i)) {
  822. i->empty = 1;
  823. return -EINVAL;
  824. }
  825. return changed;
  826. }
  827. static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
  828. {
  829. unsigned int n;
  830. int changed = 0;
  831. n = (i->min - min) % step;
  832. if (n != 0 || i->openmin) {
  833. i->min += step - n;
  834. changed = 1;
  835. }
  836. n = (i->max - min) % step;
  837. if (n != 0 || i->openmax) {
  838. i->max -= n;
  839. changed = 1;
  840. }
  841. if (snd_interval_checkempty(i)) {
  842. i->empty = 1;
  843. return -EINVAL;
  844. }
  845. return changed;
  846. }
  847. /* Info constraints helpers */
  848. /**
  849. * snd_pcm_hw_rule_add - add the hw-constraint rule
  850. * @runtime: the pcm runtime instance
  851. * @cond: condition bits
  852. * @var: the variable to evaluate
  853. * @func: the evaluation function
  854. * @private: the private data pointer passed to function
  855. * @dep: the dependent variables
  856. *
  857. * Returns zero if successful, or a negative error code on failure.
  858. */
  859. int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
  860. int var,
  861. snd_pcm_hw_rule_func_t func, void *private,
  862. int dep, ...)
  863. {
  864. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  865. struct snd_pcm_hw_rule *c;
  866. unsigned int k;
  867. va_list args;
  868. va_start(args, dep);
  869. if (constrs->rules_num >= constrs->rules_all) {
  870. struct snd_pcm_hw_rule *new;
  871. unsigned int new_rules = constrs->rules_all + 16;
  872. new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
  873. if (!new)
  874. return -ENOMEM;
  875. if (constrs->rules) {
  876. memcpy(new, constrs->rules,
  877. constrs->rules_num * sizeof(*c));
  878. kfree(constrs->rules);
  879. }
  880. constrs->rules = new;
  881. constrs->rules_all = new_rules;
  882. }
  883. c = &constrs->rules[constrs->rules_num];
  884. c->cond = cond;
  885. c->func = func;
  886. c->var = var;
  887. c->private = private;
  888. k = 0;
  889. while (1) {
  890. snd_assert(k < ARRAY_SIZE(c->deps), return -EINVAL);
  891. c->deps[k++] = dep;
  892. if (dep < 0)
  893. break;
  894. dep = va_arg(args, int);
  895. }
  896. constrs->rules_num++;
  897. va_end(args);
  898. return 0;
  899. }
  900. /**
  901. * snd_pcm_hw_constraint_mask
  902. * @runtime: PCM runtime instance
  903. * @var: hw_params variable to apply the mask
  904. * @mask: the bitmap mask
  905. *
  906. * Apply the constraint of the given bitmap mask to a mask parameter.
  907. */
  908. int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  909. u_int32_t mask)
  910. {
  911. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  912. struct snd_mask *maskp = constrs_mask(constrs, var);
  913. *maskp->bits &= mask;
  914. memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
  915. if (*maskp->bits == 0)
  916. return -EINVAL;
  917. return 0;
  918. }
  919. /**
  920. * snd_pcm_hw_constraint_mask64
  921. * @runtime: PCM runtime instance
  922. * @var: hw_params variable to apply the mask
  923. * @mask: the 64bit bitmap mask
  924. *
  925. * Apply the constraint of the given bitmap mask to a mask parameter.
  926. */
  927. int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  928. u_int64_t mask)
  929. {
  930. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  931. struct snd_mask *maskp = constrs_mask(constrs, var);
  932. maskp->bits[0] &= (u_int32_t)mask;
  933. maskp->bits[1] &= (u_int32_t)(mask >> 32);
  934. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
  935. if (! maskp->bits[0] && ! maskp->bits[1])
  936. return -EINVAL;
  937. return 0;
  938. }
  939. /**
  940. * snd_pcm_hw_constraint_integer
  941. * @runtime: PCM runtime instance
  942. * @var: hw_params variable to apply the integer constraint
  943. *
  944. * Apply the constraint of integer to an interval parameter.
  945. */
  946. int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
  947. {
  948. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  949. return snd_interval_setinteger(constrs_interval(constrs, var));
  950. }
  951. /**
  952. * snd_pcm_hw_constraint_minmax
  953. * @runtime: PCM runtime instance
  954. * @var: hw_params variable to apply the range
  955. * @min: the minimal value
  956. * @max: the maximal value
  957. *
  958. * Apply the min/max range constraint to an interval parameter.
  959. */
  960. int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  961. unsigned int min, unsigned int max)
  962. {
  963. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  964. struct snd_interval t;
  965. t.min = min;
  966. t.max = max;
  967. t.openmin = t.openmax = 0;
  968. t.integer = 0;
  969. return snd_interval_refine(constrs_interval(constrs, var), &t);
  970. }
  971. static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
  972. struct snd_pcm_hw_rule *rule)
  973. {
  974. struct snd_pcm_hw_constraint_list *list = rule->private;
  975. return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
  976. }
  977. /**
  978. * snd_pcm_hw_constraint_list
  979. * @runtime: PCM runtime instance
  980. * @cond: condition bits
  981. * @var: hw_params variable to apply the list constraint
  982. * @l: list
  983. *
  984. * Apply the list of constraints to an interval parameter.
  985. */
  986. int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
  987. unsigned int cond,
  988. snd_pcm_hw_param_t var,
  989. struct snd_pcm_hw_constraint_list *l)
  990. {
  991. return snd_pcm_hw_rule_add(runtime, cond, var,
  992. snd_pcm_hw_rule_list, l,
  993. var, -1);
  994. }
  995. static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
  996. struct snd_pcm_hw_rule *rule)
  997. {
  998. struct snd_pcm_hw_constraint_ratnums *r = rule->private;
  999. unsigned int num = 0, den = 0;
  1000. int err;
  1001. err = snd_interval_ratnum(hw_param_interval(params, rule->var),
  1002. r->nrats, r->rats, &num, &den);
  1003. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1004. params->rate_num = num;
  1005. params->rate_den = den;
  1006. }
  1007. return err;
  1008. }
  1009. /**
  1010. * snd_pcm_hw_constraint_ratnums
  1011. * @runtime: PCM runtime instance
  1012. * @cond: condition bits
  1013. * @var: hw_params variable to apply the ratnums constraint
  1014. * @r: struct snd_ratnums constriants
  1015. */
  1016. int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
  1017. unsigned int cond,
  1018. snd_pcm_hw_param_t var,
  1019. struct snd_pcm_hw_constraint_ratnums *r)
  1020. {
  1021. return snd_pcm_hw_rule_add(runtime, cond, var,
  1022. snd_pcm_hw_rule_ratnums, r,
  1023. var, -1);
  1024. }
  1025. static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
  1026. struct snd_pcm_hw_rule *rule)
  1027. {
  1028. struct snd_pcm_hw_constraint_ratdens *r = rule->private;
  1029. unsigned int num = 0, den = 0;
  1030. int err = snd_interval_ratden(hw_param_interval(params, rule->var),
  1031. r->nrats, r->rats, &num, &den);
  1032. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1033. params->rate_num = num;
  1034. params->rate_den = den;
  1035. }
  1036. return err;
  1037. }
  1038. /**
  1039. * snd_pcm_hw_constraint_ratdens
  1040. * @runtime: PCM runtime instance
  1041. * @cond: condition bits
  1042. * @var: hw_params variable to apply the ratdens constraint
  1043. * @r: struct snd_ratdens constriants
  1044. */
  1045. int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
  1046. unsigned int cond,
  1047. snd_pcm_hw_param_t var,
  1048. struct snd_pcm_hw_constraint_ratdens *r)
  1049. {
  1050. return snd_pcm_hw_rule_add(runtime, cond, var,
  1051. snd_pcm_hw_rule_ratdens, r,
  1052. var, -1);
  1053. }
  1054. static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
  1055. struct snd_pcm_hw_rule *rule)
  1056. {
  1057. unsigned int l = (unsigned long) rule->private;
  1058. int width = l & 0xffff;
  1059. unsigned int msbits = l >> 16;
  1060. struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
  1061. if (snd_interval_single(i) && snd_interval_value(i) == width)
  1062. params->msbits = msbits;
  1063. return 0;
  1064. }
  1065. /**
  1066. * snd_pcm_hw_constraint_msbits
  1067. * @runtime: PCM runtime instance
  1068. * @cond: condition bits
  1069. * @width: sample bits width
  1070. * @msbits: msbits width
  1071. */
  1072. int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
  1073. unsigned int cond,
  1074. unsigned int width,
  1075. unsigned int msbits)
  1076. {
  1077. unsigned long l = (msbits << 16) | width;
  1078. return snd_pcm_hw_rule_add(runtime, cond, -1,
  1079. snd_pcm_hw_rule_msbits,
  1080. (void*) l,
  1081. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1082. }
  1083. static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
  1084. struct snd_pcm_hw_rule *rule)
  1085. {
  1086. unsigned long step = (unsigned long) rule->private;
  1087. return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
  1088. }
  1089. /**
  1090. * snd_pcm_hw_constraint_step
  1091. * @runtime: PCM runtime instance
  1092. * @cond: condition bits
  1093. * @var: hw_params variable to apply the step constraint
  1094. * @step: step size
  1095. */
  1096. int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
  1097. unsigned int cond,
  1098. snd_pcm_hw_param_t var,
  1099. unsigned long step)
  1100. {
  1101. return snd_pcm_hw_rule_add(runtime, cond, var,
  1102. snd_pcm_hw_rule_step, (void *) step,
  1103. var, -1);
  1104. }
  1105. static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
  1106. {
  1107. static int pow2_sizes[] = {
  1108. 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
  1109. 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
  1110. 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
  1111. 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
  1112. };
  1113. return snd_interval_list(hw_param_interval(params, rule->var),
  1114. ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
  1115. }
  1116. /**
  1117. * snd_pcm_hw_constraint_pow2
  1118. * @runtime: PCM runtime instance
  1119. * @cond: condition bits
  1120. * @var: hw_params variable to apply the power-of-2 constraint
  1121. */
  1122. int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
  1123. unsigned int cond,
  1124. snd_pcm_hw_param_t var)
  1125. {
  1126. return snd_pcm_hw_rule_add(runtime, cond, var,
  1127. snd_pcm_hw_rule_pow2, NULL,
  1128. var, -1);
  1129. }
  1130. /* To use the same code we have in alsa-lib */
  1131. #define assert(i) snd_assert((i), return -EINVAL)
  1132. #ifndef INT_MIN
  1133. #define INT_MIN ((int)((unsigned int)INT_MAX+1))
  1134. #endif
  1135. static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
  1136. snd_pcm_hw_param_t var)
  1137. {
  1138. if (hw_is_mask(var)) {
  1139. snd_mask_any(hw_param_mask(params, var));
  1140. params->cmask |= 1 << var;
  1141. params->rmask |= 1 << var;
  1142. return;
  1143. }
  1144. if (hw_is_interval(var)) {
  1145. snd_interval_any(hw_param_interval(params, var));
  1146. params->cmask |= 1 << var;
  1147. params->rmask |= 1 << var;
  1148. return;
  1149. }
  1150. snd_BUG();
  1151. }
  1152. #if 0
  1153. /*
  1154. * snd_pcm_hw_param_any
  1155. */
  1156. int snd_pcm_hw_param_any(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
  1157. snd_pcm_hw_param_t var)
  1158. {
  1159. _snd_pcm_hw_param_any(params, var);
  1160. return snd_pcm_hw_refine(pcm, params);
  1161. }
  1162. #endif /* 0 */
  1163. void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
  1164. {
  1165. unsigned int k;
  1166. memset(params, 0, sizeof(*params));
  1167. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
  1168. _snd_pcm_hw_param_any(params, k);
  1169. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
  1170. _snd_pcm_hw_param_any(params, k);
  1171. params->info = ~0U;
  1172. }
  1173. #if 0
  1174. /*
  1175. * snd_pcm_hw_params_any
  1176. *
  1177. * Fill PARAMS with full configuration space boundaries
  1178. */
  1179. int snd_pcm_hw_params_any(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params)
  1180. {
  1181. _snd_pcm_hw_params_any(params);
  1182. return snd_pcm_hw_refine(pcm, params);
  1183. }
  1184. #endif /* 0 */
  1185. /**
  1186. * snd_pcm_hw_param_value
  1187. * @params: the hw_params instance
  1188. * @var: parameter to retrieve
  1189. * @dir: pointer to the direction (-1,0,1) or NULL
  1190. *
  1191. * Return the value for field PAR if it's fixed in configuration space
  1192. * defined by PARAMS. Return -EINVAL otherwise
  1193. */
  1194. static int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
  1195. snd_pcm_hw_param_t var, int *dir)
  1196. {
  1197. if (hw_is_mask(var)) {
  1198. const struct snd_mask *mask = hw_param_mask_c(params, var);
  1199. if (!snd_mask_single(mask))
  1200. return -EINVAL;
  1201. if (dir)
  1202. *dir = 0;
  1203. return snd_mask_value(mask);
  1204. }
  1205. if (hw_is_interval(var)) {
  1206. const struct snd_interval *i = hw_param_interval_c(params, var);
  1207. if (!snd_interval_single(i))
  1208. return -EINVAL;
  1209. if (dir)
  1210. *dir = i->openmin;
  1211. return snd_interval_value(i);
  1212. }
  1213. assert(0);
  1214. return -EINVAL;
  1215. }
  1216. /**
  1217. * snd_pcm_hw_param_value_min
  1218. * @params: the hw_params instance
  1219. * @var: parameter to retrieve
  1220. * @dir: pointer to the direction (-1,0,1) or NULL
  1221. *
  1222. * Return the minimum value for field PAR.
  1223. */
  1224. unsigned int snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
  1225. snd_pcm_hw_param_t var, int *dir)
  1226. {
  1227. if (hw_is_mask(var)) {
  1228. if (dir)
  1229. *dir = 0;
  1230. return snd_mask_min(hw_param_mask_c(params, var));
  1231. }
  1232. if (hw_is_interval(var)) {
  1233. const struct snd_interval *i = hw_param_interval_c(params, var);
  1234. if (dir)
  1235. *dir = i->openmin;
  1236. return snd_interval_min(i);
  1237. }
  1238. assert(0);
  1239. return -EINVAL;
  1240. }
  1241. /**
  1242. * snd_pcm_hw_param_value_max
  1243. * @params: the hw_params instance
  1244. * @var: parameter to retrieve
  1245. * @dir: pointer to the direction (-1,0,1) or NULL
  1246. *
  1247. * Return the maximum value for field PAR.
  1248. */
  1249. unsigned int snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
  1250. snd_pcm_hw_param_t var, int *dir)
  1251. {
  1252. if (hw_is_mask(var)) {
  1253. if (dir)
  1254. *dir = 0;
  1255. return snd_mask_max(hw_param_mask_c(params, var));
  1256. }
  1257. if (hw_is_interval(var)) {
  1258. const struct snd_interval *i = hw_param_interval_c(params, var);
  1259. if (dir)
  1260. *dir = - (int) i->openmax;
  1261. return snd_interval_max(i);
  1262. }
  1263. assert(0);
  1264. return -EINVAL;
  1265. }
  1266. void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
  1267. snd_pcm_hw_param_t var)
  1268. {
  1269. if (hw_is_mask(var)) {
  1270. snd_mask_none(hw_param_mask(params, var));
  1271. params->cmask |= 1 << var;
  1272. params->rmask |= 1 << var;
  1273. } else if (hw_is_interval(var)) {
  1274. snd_interval_none(hw_param_interval(params, var));
  1275. params->cmask |= 1 << var;
  1276. params->rmask |= 1 << var;
  1277. } else {
  1278. snd_BUG();
  1279. }
  1280. }
  1281. int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
  1282. snd_pcm_hw_param_t var)
  1283. {
  1284. int changed;
  1285. assert(hw_is_interval(var));
  1286. changed = snd_interval_setinteger(hw_param_interval(params, var));
  1287. if (changed) {
  1288. params->cmask |= 1 << var;
  1289. params->rmask |= 1 << var;
  1290. }
  1291. return changed;
  1292. }
  1293. #if 0
  1294. /*
  1295. * snd_pcm_hw_param_setinteger
  1296. *
  1297. * Inside configuration space defined by PARAMS remove from PAR all
  1298. * non integer values. Reduce configuration space accordingly.
  1299. * Return -EINVAL if the configuration space is empty
  1300. */
  1301. int snd_pcm_hw_param_setinteger(struct snd_pcm_substream *pcm,
  1302. struct snd_pcm_hw_params *params,
  1303. snd_pcm_hw_param_t var)
  1304. {
  1305. int changed = _snd_pcm_hw_param_setinteger(params, var);
  1306. if (changed < 0)
  1307. return changed;
  1308. if (params->rmask) {
  1309. int err = snd_pcm_hw_refine(pcm, params);
  1310. if (err < 0)
  1311. return err;
  1312. }
  1313. return 0;
  1314. }
  1315. #endif /* 0 */
  1316. static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
  1317. snd_pcm_hw_param_t var)
  1318. {
  1319. int changed;
  1320. if (hw_is_mask(var))
  1321. changed = snd_mask_refine_first(hw_param_mask(params, var));
  1322. else if (hw_is_interval(var))
  1323. changed = snd_interval_refine_first(hw_param_interval(params, var));
  1324. else {
  1325. assert(0);
  1326. return -EINVAL;
  1327. }
  1328. if (changed) {
  1329. params->cmask |= 1 << var;
  1330. params->rmask |= 1 << var;
  1331. }
  1332. return changed;
  1333. }
  1334. /**
  1335. * snd_pcm_hw_param_first
  1336. * @pcm: PCM instance
  1337. * @params: the hw_params instance
  1338. * @var: parameter to retrieve
  1339. * @dir: pointer to the direction (-1,0,1) or NULL
  1340. *
  1341. * Inside configuration space defined by PARAMS remove from PAR all
  1342. * values > minimum. Reduce configuration space accordingly.
  1343. * Return the minimum.
  1344. */
  1345. static int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
  1346. struct snd_pcm_hw_params *params,
  1347. snd_pcm_hw_param_t var, int *dir)
  1348. {
  1349. int changed = _snd_pcm_hw_param_first(params, var);
  1350. if (changed < 0)
  1351. return changed;
  1352. if (params->rmask) {
  1353. int err = snd_pcm_hw_refine(pcm, params);
  1354. assert(err >= 0);
  1355. }
  1356. return snd_pcm_hw_param_value(params, var, dir);
  1357. }
  1358. static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
  1359. snd_pcm_hw_param_t var)
  1360. {
  1361. int changed;
  1362. if (hw_is_mask(var))
  1363. changed = snd_mask_refine_last(hw_param_mask(params, var));
  1364. else if (hw_is_interval(var))
  1365. changed = snd_interval_refine_last(hw_param_interval(params, var));
  1366. else {
  1367. assert(0);
  1368. return -EINVAL;
  1369. }
  1370. if (changed) {
  1371. params->cmask |= 1 << var;
  1372. params->rmask |= 1 << var;
  1373. }
  1374. return changed;
  1375. }
  1376. /**
  1377. * snd_pcm_hw_param_last
  1378. * @pcm: PCM instance
  1379. * @params: the hw_params instance
  1380. * @var: parameter to retrieve
  1381. * @dir: pointer to the direction (-1,0,1) or NULL
  1382. *
  1383. * Inside configuration space defined by PARAMS remove from PAR all
  1384. * values < maximum. Reduce configuration space accordingly.
  1385. * Return the maximum.
  1386. */
  1387. static int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
  1388. struct snd_pcm_hw_params *params,
  1389. snd_pcm_hw_param_t var, int *dir)
  1390. {
  1391. int changed = _snd_pcm_hw_param_last(params, var);
  1392. if (changed < 0)
  1393. return changed;
  1394. if (params->rmask) {
  1395. int err = snd_pcm_hw_refine(pcm, params);
  1396. assert(err >= 0);
  1397. }
  1398. return snd_pcm_hw_param_value(params, var, dir);
  1399. }
  1400. int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
  1401. snd_pcm_hw_param_t var, unsigned int val, int dir)
  1402. {
  1403. int changed;
  1404. int open = 0;
  1405. if (dir) {
  1406. if (dir > 0) {
  1407. open = 1;
  1408. } else if (dir < 0) {
  1409. if (val > 0) {
  1410. open = 1;
  1411. val--;
  1412. }
  1413. }
  1414. }
  1415. if (hw_is_mask(var))
  1416. changed = snd_mask_refine_min(hw_param_mask(params, var), val + !!open);
  1417. else if (hw_is_interval(var))
  1418. changed = snd_interval_refine_min(hw_param_interval(params, var), val, open);
  1419. else {
  1420. assert(0);
  1421. return -EINVAL;
  1422. }
  1423. if (changed) {
  1424. params->cmask |= 1 << var;
  1425. params->rmask |= 1 << var;
  1426. }
  1427. return changed;
  1428. }
  1429. /**
  1430. * snd_pcm_hw_param_min
  1431. * @pcm: PCM instance
  1432. * @params: the hw_params instance
  1433. * @var: parameter to retrieve
  1434. * @val: minimal value
  1435. * @dir: pointer to the direction (-1,0,1) or NULL
  1436. *
  1437. * Inside configuration space defined by PARAMS remove from PAR all
  1438. * values < VAL. Reduce configuration space accordingly.
  1439. * Return new minimum or -EINVAL if the configuration space is empty
  1440. */
  1441. static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
  1442. snd_pcm_hw_param_t var, unsigned int val,
  1443. int *dir)
  1444. {
  1445. int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
  1446. if (changed < 0)
  1447. return changed;
  1448. if (params->rmask) {
  1449. int err = snd_pcm_hw_refine(pcm, params);
  1450. if (err < 0)
  1451. return err;
  1452. }
  1453. return snd_pcm_hw_param_value_min(params, var, dir);
  1454. }
  1455. static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params,
  1456. snd_pcm_hw_param_t var, unsigned int val,
  1457. int dir)
  1458. {
  1459. int changed;
  1460. int open = 0;
  1461. if (dir) {
  1462. if (dir < 0) {
  1463. open = 1;
  1464. } else if (dir > 0) {
  1465. open = 1;
  1466. val++;
  1467. }
  1468. }
  1469. if (hw_is_mask(var)) {
  1470. if (val == 0 && open) {
  1471. snd_mask_none(hw_param_mask(params, var));
  1472. changed = -EINVAL;
  1473. } else
  1474. changed = snd_mask_refine_max(hw_param_mask(params, var), val - !!open);
  1475. } else if (hw_is_interval(var))
  1476. changed = snd_interval_refine_max(hw_param_interval(params, var), val, open);
  1477. else {
  1478. assert(0);
  1479. return -EINVAL;
  1480. }
  1481. if (changed) {
  1482. params->cmask |= 1 << var;
  1483. params->rmask |= 1 << var;
  1484. }
  1485. return changed;
  1486. }
  1487. /**
  1488. * snd_pcm_hw_param_max
  1489. * @pcm: PCM instance
  1490. * @params: the hw_params instance
  1491. * @var: parameter to retrieve
  1492. * @val: maximal value
  1493. * @dir: pointer to the direction (-1,0,1) or NULL
  1494. *
  1495. * Inside configuration space defined by PARAMS remove from PAR all
  1496. * values >= VAL + 1. Reduce configuration space accordingly.
  1497. * Return new maximum or -EINVAL if the configuration space is empty
  1498. */
  1499. static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
  1500. snd_pcm_hw_param_t var, unsigned int val,
  1501. int *dir)
  1502. {
  1503. int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
  1504. if (changed < 0)
  1505. return changed;
  1506. if (params->rmask) {
  1507. int err = snd_pcm_hw_refine(pcm, params);
  1508. if (err < 0)
  1509. return err;
  1510. }
  1511. return snd_pcm_hw_param_value_max(params, var, dir);
  1512. }
  1513. int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
  1514. snd_pcm_hw_param_t var, unsigned int val, int dir)
  1515. {
  1516. int changed;
  1517. if (hw_is_mask(var)) {
  1518. struct snd_mask *m = hw_param_mask(params, var);
  1519. if (val == 0 && dir < 0) {
  1520. changed = -EINVAL;
  1521. snd_mask_none(m);
  1522. } else {
  1523. if (dir > 0)
  1524. val++;
  1525. else if (dir < 0)
  1526. val--;
  1527. changed = snd_mask_refine_set(hw_param_mask(params, var), val);
  1528. }
  1529. } else if (hw_is_interval(var)) {
  1530. struct snd_interval *i = hw_param_interval(params, var);
  1531. if (val == 0 && dir < 0) {
  1532. changed = -EINVAL;
  1533. snd_interval_none(i);
  1534. } else if (dir == 0)
  1535. changed = snd_interval_refine_set(i, val);
  1536. else {
  1537. struct snd_interval t;
  1538. t.openmin = 1;
  1539. t.openmax = 1;
  1540. t.empty = 0;
  1541. t.integer = 0;
  1542. if (dir < 0) {
  1543. t.min = val - 1;
  1544. t.max = val;
  1545. } else {
  1546. t.min = val;
  1547. t.max = val+1;
  1548. }
  1549. changed = snd_interval_refine(i, &t);
  1550. }
  1551. } else {
  1552. assert(0);
  1553. return -EINVAL;
  1554. }
  1555. if (changed) {
  1556. params->cmask |= 1 << var;
  1557. params->rmask |= 1 << var;
  1558. }
  1559. return changed;
  1560. }
  1561. /**
  1562. * snd_pcm_hw_param_set
  1563. * @pcm: PCM instance
  1564. * @params: the hw_params instance
  1565. * @var: parameter to retrieve
  1566. * @val: value to set
  1567. * @dir: pointer to the direction (-1,0,1) or NULL
  1568. *
  1569. * Inside configuration space defined by PARAMS remove from PAR all
  1570. * values != VAL. Reduce configuration space accordingly.
  1571. * Return VAL or -EINVAL if the configuration space is empty
  1572. */
  1573. int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
  1574. snd_pcm_hw_param_t var, unsigned int val, int dir)
  1575. {
  1576. int changed = _snd_pcm_hw_param_set(params, var, val, dir);
  1577. if (changed < 0)
  1578. return changed;
  1579. if (params->rmask) {
  1580. int err = snd_pcm_hw_refine(pcm, params);
  1581. if (err < 0)
  1582. return err;
  1583. }
  1584. return snd_pcm_hw_param_value(params, var, NULL);
  1585. }
  1586. static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params,
  1587. snd_pcm_hw_param_t var, const struct snd_mask *val)
  1588. {
  1589. int changed;
  1590. assert(hw_is_mask(var));
  1591. changed = snd_mask_refine(hw_param_mask(params, var), val);
  1592. if (changed) {
  1593. params->cmask |= 1 << var;
  1594. params->rmask |= 1 << var;
  1595. }
  1596. return changed;
  1597. }
  1598. /**
  1599. * snd_pcm_hw_param_mask
  1600. * @pcm: PCM instance
  1601. * @params: the hw_params instance
  1602. * @var: parameter to retrieve
  1603. * @val: mask to apply
  1604. *
  1605. * Inside configuration space defined by PARAMS remove from PAR all values
  1606. * not contained in MASK. Reduce configuration space accordingly.
  1607. * This function can be called only for SNDRV_PCM_HW_PARAM_ACCESS,
  1608. * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
  1609. * Return 0 on success or -EINVAL
  1610. * if the configuration space is empty
  1611. */
  1612. int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
  1613. snd_pcm_hw_param_t var, const struct snd_mask *val)
  1614. {
  1615. int changed = _snd_pcm_hw_param_mask(params, var, val);
  1616. if (changed < 0)
  1617. return changed;
  1618. if (params->rmask) {
  1619. int err = snd_pcm_hw_refine(pcm, params);
  1620. if (err < 0)
  1621. return err;
  1622. }
  1623. return 0;
  1624. }
  1625. static int boundary_sub(int a, int adir,
  1626. int b, int bdir,
  1627. int *c, int *cdir)
  1628. {
  1629. adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
  1630. bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
  1631. *c = a - b;
  1632. *cdir = adir - bdir;
  1633. if (*cdir == -2) {
  1634. assert(*c > INT_MIN);
  1635. (*c)--;
  1636. } else if (*cdir == 2) {
  1637. assert(*c < INT_MAX);
  1638. (*c)++;
  1639. }
  1640. return 0;
  1641. }
  1642. static int boundary_lt(unsigned int a, int adir,
  1643. unsigned int b, int bdir)
  1644. {
  1645. assert(a > 0 || adir >= 0);
  1646. assert(b > 0 || bdir >= 0);
  1647. if (adir < 0) {
  1648. a--;
  1649. adir = 1;
  1650. } else if (adir > 0)
  1651. adir = 1;
  1652. if (bdir < 0) {
  1653. b--;
  1654. bdir = 1;
  1655. } else if (bdir > 0)
  1656. bdir = 1;
  1657. return a < b || (a == b && adir < bdir);
  1658. }
  1659. /* Return 1 if min is nearer to best than max */
  1660. static int boundary_nearer(int min, int mindir,
  1661. int best, int bestdir,
  1662. int max, int maxdir)
  1663. {
  1664. int dmin, dmindir;
  1665. int dmax, dmaxdir;
  1666. boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
  1667. boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
  1668. return boundary_lt(dmin, dmindir, dmax, dmaxdir);
  1669. }
  1670. /**
  1671. * snd_pcm_hw_param_near
  1672. * @pcm: PCM instance
  1673. * @params: the hw_params instance
  1674. * @var: parameter to retrieve
  1675. * @best: value to set
  1676. * @dir: pointer to the direction (-1,0,1) or NULL
  1677. *
  1678. * Inside configuration space defined by PARAMS set PAR to the available value
  1679. * nearest to VAL. Reduce configuration space accordingly.
  1680. * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
  1681. * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
  1682. * Return the value found.
  1683. */
  1684. int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params,
  1685. snd_pcm_hw_param_t var, unsigned int best, int *dir)
  1686. {
  1687. struct snd_pcm_hw_params *save = NULL;
  1688. int v;
  1689. unsigned int saved_min;
  1690. int last = 0;
  1691. int min, max;
  1692. int mindir, maxdir;
  1693. int valdir = dir ? *dir : 0;
  1694. /* FIXME */
  1695. if (best > INT_MAX)
  1696. best = INT_MAX;
  1697. min = max = best;
  1698. mindir = maxdir = valdir;
  1699. if (maxdir > 0)
  1700. maxdir = 0;
  1701. else if (maxdir == 0)
  1702. maxdir = -1;
  1703. else {
  1704. maxdir = 1;
  1705. max--;
  1706. }
  1707. save = kmalloc(sizeof(*save), GFP_KERNEL);
  1708. if (save == NULL)
  1709. return -ENOMEM;
  1710. *save = *params;
  1711. saved_min = min;
  1712. min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
  1713. if (min >= 0) {
  1714. struct snd_pcm_hw_params *params1;
  1715. if (max < 0)
  1716. goto _end;
  1717. if ((unsigned int)min == saved_min && mindir == valdir)
  1718. goto _end;
  1719. params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
  1720. if (params1 == NULL) {
  1721. kfree(save);
  1722. return -ENOMEM;
  1723. }
  1724. *params1 = *save;
  1725. max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
  1726. if (max < 0) {
  1727. kfree(params1);
  1728. goto _end;
  1729. }
  1730. if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
  1731. *params = *params1;
  1732. last = 1;
  1733. }
  1734. kfree(params1);
  1735. } else {
  1736. *params = *save;
  1737. max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
  1738. assert(max >= 0);
  1739. last = 1;
  1740. }
  1741. _end:
  1742. kfree(save);
  1743. if (last)
  1744. v = snd_pcm_hw_param_last(pcm, params, var, dir);
  1745. else
  1746. v = snd_pcm_hw_param_first(pcm, params, var, dir);
  1747. assert(v >= 0);
  1748. return v;
  1749. }
  1750. /**
  1751. * snd_pcm_hw_param_choose
  1752. * @pcm: PCM instance
  1753. * @params: the hw_params instance
  1754. *
  1755. * Choose one configuration from configuration space defined by PARAMS
  1756. * The configuration chosen is that obtained fixing in this order:
  1757. * first access, first format, first subformat, min channels,
  1758. * min rate, min period time, max buffer size, min tick time
  1759. */
  1760. int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm, struct snd_pcm_hw_params *params)
  1761. {
  1762. int err;
  1763. err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_ACCESS, NULL);
  1764. assert(err >= 0);
  1765. err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_FORMAT, NULL);
  1766. assert(err >= 0);
  1767. err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_SUBFORMAT, NULL);
  1768. assert(err >= 0);
  1769. err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_CHANNELS, NULL);
  1770. assert(err >= 0);
  1771. err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_RATE, NULL);
  1772. assert(err >= 0);
  1773. err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_PERIOD_TIME, NULL);
  1774. assert(err >= 0);
  1775. err = snd_pcm_hw_param_last(pcm, params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL);
  1776. assert(err >= 0);
  1777. err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_TICK_TIME, NULL);
  1778. assert(err >= 0);
  1779. return 0;
  1780. }
  1781. #undef assert
  1782. static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
  1783. void *arg)
  1784. {
  1785. struct snd_pcm_runtime *runtime = substream->runtime;
  1786. unsigned long flags;
  1787. snd_pcm_stream_lock_irqsave(substream, flags);
  1788. if (snd_pcm_running(substream) &&
  1789. snd_pcm_update_hw_ptr(substream) >= 0)
  1790. runtime->status->hw_ptr %= runtime->buffer_size;
  1791. else
  1792. runtime->status->hw_ptr = 0;
  1793. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1794. return 0;
  1795. }
  1796. static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
  1797. void *arg)
  1798. {
  1799. struct snd_pcm_channel_info *info = arg;
  1800. struct snd_pcm_runtime *runtime = substream->runtime;
  1801. int width;
  1802. if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
  1803. info->offset = -1;
  1804. return 0;
  1805. }
  1806. width = snd_pcm_format_physical_width(runtime->format);
  1807. if (width < 0)
  1808. return width;
  1809. info->offset = 0;
  1810. switch (runtime->access) {
  1811. case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
  1812. case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
  1813. info->first = info->channel * width;
  1814. info->step = runtime->channels * width;
  1815. break;
  1816. case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
  1817. case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
  1818. {
  1819. size_t size = runtime->dma_bytes / runtime->channels;
  1820. info->first = info->channel * size * 8;
  1821. info->step = width;
  1822. break;
  1823. }
  1824. default:
  1825. snd_BUG();
  1826. break;
  1827. }
  1828. return 0;
  1829. }
  1830. /**
  1831. * snd_pcm_lib_ioctl - a generic PCM ioctl callback
  1832. * @substream: the pcm substream instance
  1833. * @cmd: ioctl command
  1834. * @arg: ioctl argument
  1835. *
  1836. * Processes the generic ioctl commands for PCM.
  1837. * Can be passed as the ioctl callback for PCM ops.
  1838. *
  1839. * Returns zero if successful, or a negative error code on failure.
  1840. */
  1841. int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  1842. unsigned int cmd, void *arg)
  1843. {
  1844. switch (cmd) {
  1845. case SNDRV_PCM_IOCTL1_INFO:
  1846. return 0;
  1847. case SNDRV_PCM_IOCTL1_RESET:
  1848. return snd_pcm_lib_ioctl_reset(substream, arg);
  1849. case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
  1850. return snd_pcm_lib_ioctl_channel_info(substream, arg);
  1851. }
  1852. return -ENXIO;
  1853. }
  1854. /*
  1855. * Conditions
  1856. */
  1857. static void snd_pcm_system_tick_set(struct snd_pcm_substream *substream,
  1858. unsigned long ticks)
  1859. {
  1860. struct snd_pcm_runtime *runtime = substream->runtime;
  1861. if (ticks == 0)
  1862. del_timer(&runtime->tick_timer);
  1863. else {
  1864. ticks += (1000000 / HZ) - 1;
  1865. ticks /= (1000000 / HZ);
  1866. mod_timer(&runtime->tick_timer, jiffies + ticks);
  1867. }
  1868. }
  1869. /* Temporary alias */
  1870. void snd_pcm_tick_set(struct snd_pcm_substream *substream, unsigned long ticks)
  1871. {
  1872. snd_pcm_system_tick_set(substream, ticks);
  1873. }
  1874. void snd_pcm_tick_prepare(struct snd_pcm_substream *substream)
  1875. {
  1876. struct snd_pcm_runtime *runtime = substream->runtime;
  1877. snd_pcm_uframes_t frames = ULONG_MAX;
  1878. snd_pcm_uframes_t avail, dist;
  1879. unsigned int ticks;
  1880. u_int64_t n;
  1881. u_int32_t r;
  1882. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1883. if (runtime->silence_size >= runtime->boundary) {
  1884. frames = 1;
  1885. } else if (runtime->silence_size > 0 &&
  1886. runtime->silence_filled < runtime->buffer_size) {
  1887. snd_pcm_sframes_t noise_dist;
  1888. noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
  1889. if (noise_dist > (snd_pcm_sframes_t)runtime->silence_threshold)
  1890. frames = noise_dist - runtime->silence_threshold;
  1891. }
  1892. avail = snd_pcm_playback_avail(runtime);
  1893. } else {
  1894. avail = snd_pcm_capture_avail(runtime);
  1895. }
  1896. if (avail < runtime->control->avail_min) {
  1897. snd_pcm_sframes_t n = runtime->control->avail_min - avail;
  1898. if (n > 0 && frames > (snd_pcm_uframes_t)n)
  1899. frames = n;
  1900. }
  1901. if (avail < runtime->buffer_size) {
  1902. snd_pcm_sframes_t n = runtime->buffer_size - avail;
  1903. if (n > 0 && frames > (snd_pcm_uframes_t)n)
  1904. frames = n;
  1905. }
  1906. if (frames == ULONG_MAX) {
  1907. snd_pcm_tick_set(substream, 0);
  1908. return;
  1909. }
  1910. dist = runtime->status->hw_ptr - runtime->hw_ptr_base;
  1911. /* Distance to next interrupt */
  1912. dist = runtime->period_size - dist % runtime->period_size;
  1913. if (dist <= frames) {
  1914. snd_pcm_tick_set(substream, 0);
  1915. return;
  1916. }
  1917. /* the base time is us */
  1918. n = frames;
  1919. n *= 1000000;
  1920. div64_32(&n, runtime->tick_time * runtime->rate, &r);
  1921. ticks = n + (r > 0 ? 1 : 0);
  1922. if (ticks < runtime->sleep_min)
  1923. ticks = runtime->sleep_min;
  1924. snd_pcm_tick_set(substream, (unsigned long) ticks);
  1925. }
  1926. void snd_pcm_tick_elapsed(struct snd_pcm_substream *substream)
  1927. {
  1928. struct snd_pcm_runtime *runtime;
  1929. unsigned long flags;
  1930. snd_assert(substream != NULL, return);
  1931. runtime = substream->runtime;
  1932. snd_assert(runtime != NULL, return);
  1933. snd_pcm_stream_lock_irqsave(substream, flags);
  1934. if (!snd_pcm_running(substream) ||
  1935. snd_pcm_update_hw_ptr(substream) < 0)
  1936. goto _end;
  1937. if (runtime->sleep_min)
  1938. snd_pcm_tick_prepare(substream);
  1939. _end:
  1940. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1941. }
  1942. /**
  1943. * snd_pcm_period_elapsed - update the pcm status for the next period
  1944. * @substream: the pcm substream instance
  1945. *
  1946. * This function is called from the interrupt handler when the
  1947. * PCM has processed the period size. It will update the current
  1948. * pointer, set up the tick, wake up sleepers, etc.
  1949. *
  1950. * Even if more than one periods have elapsed since the last call, you
  1951. * have to call this only once.
  1952. */
  1953. void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
  1954. {
  1955. struct snd_pcm_runtime *runtime;
  1956. unsigned long flags;
  1957. snd_assert(substream != NULL, return);
  1958. runtime = substream->runtime;
  1959. snd_assert(runtime != NULL, return);
  1960. if (runtime->transfer_ack_begin)
  1961. runtime->transfer_ack_begin(substream);
  1962. snd_pcm_stream_lock_irqsave(substream, flags);
  1963. if (!snd_pcm_running(substream) ||
  1964. snd_pcm_update_hw_ptr_interrupt(substream) < 0)
  1965. goto _end;
  1966. if (substream->timer_running)
  1967. snd_timer_interrupt(substream->timer, 1);
  1968. if (runtime->sleep_min)
  1969. snd_pcm_tick_prepare(substream);
  1970. _end:
  1971. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1972. if (runtime->transfer_ack_end)
  1973. runtime->transfer_ack_end(substream);
  1974. kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
  1975. }
  1976. static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
  1977. unsigned int hwoff,
  1978. unsigned long data, unsigned int off,
  1979. snd_pcm_uframes_t frames)
  1980. {
  1981. struct snd_pcm_runtime *runtime = substream->runtime;
  1982. int err;
  1983. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  1984. if (substream->ops->copy) {
  1985. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  1986. return err;
  1987. } else {
  1988. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  1989. snd_assert(runtime->dma_area, return -EFAULT);
  1990. if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
  1991. return -EFAULT;
  1992. }
  1993. return 0;
  1994. }
  1995. typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
  1996. unsigned long data, unsigned int off,
  1997. snd_pcm_uframes_t size);
  1998. static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
  1999. unsigned long data,
  2000. snd_pcm_uframes_t size,
  2001. int nonblock,
  2002. transfer_f transfer)
  2003. {
  2004. struct snd_pcm_runtime *runtime = substream->runtime;
  2005. snd_pcm_uframes_t xfer = 0;
  2006. snd_pcm_uframes_t offset = 0;
  2007. int err = 0;
  2008. if (size == 0)
  2009. return 0;
  2010. if (size > runtime->xfer_align)
  2011. size -= size % runtime->xfer_align;
  2012. snd_pcm_stream_lock_irq(substream);
  2013. switch (runtime->status->state) {
  2014. case SNDRV_PCM_STATE_PREPARED:
  2015. case SNDRV_PCM_STATE_RUNNING:
  2016. case SNDRV_PCM_STATE_PAUSED:
  2017. break;
  2018. case SNDRV_PCM_STATE_XRUN:
  2019. err = -EPIPE;
  2020. goto _end_unlock;
  2021. case SNDRV_PCM_STATE_SUSPENDED:
  2022. err = -ESTRPIPE;
  2023. goto _end_unlock;
  2024. default:
  2025. err = -EBADFD;
  2026. goto _end_unlock;
  2027. }
  2028. while (size > 0) {
  2029. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  2030. snd_pcm_uframes_t avail;
  2031. snd_pcm_uframes_t cont;
  2032. if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  2033. snd_pcm_update_hw_ptr(substream);
  2034. avail = snd_pcm_playback_avail(runtime);
  2035. if (((avail < runtime->control->avail_min && size > avail) ||
  2036. (size >= runtime->xfer_align && avail < runtime->xfer_align))) {
  2037. wait_queue_t wait;
  2038. enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
  2039. long tout;
  2040. if (nonblock) {
  2041. err = -EAGAIN;
  2042. goto _end_unlock;
  2043. }
  2044. init_waitqueue_entry(&wait, current);
  2045. add_wait_queue(&runtime->sleep, &wait);
  2046. while (1) {
  2047. if (signal_pending(current)) {
  2048. state = SIGNALED;
  2049. break;
  2050. }
  2051. set_current_state(TASK_INTERRUPTIBLE);
  2052. snd_pcm_stream_unlock_irq(substream);
  2053. tout = schedule_timeout(10 * HZ);
  2054. snd_pcm_stream_lock_irq(substream);
  2055. if (tout == 0) {
  2056. if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
  2057. runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
  2058. state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
  2059. break;
  2060. }
  2061. }
  2062. switch (runtime->status->state) {
  2063. case SNDRV_PCM_STATE_XRUN:
  2064. case SNDRV_PCM_STATE_DRAINING:
  2065. state = ERROR;
  2066. goto _end_loop;
  2067. case SNDRV_PCM_STATE_SUSPENDED:
  2068. state = SUSPENDED;
  2069. goto _end_loop;
  2070. case SNDRV_PCM_STATE_SETUP:
  2071. state = DROPPED;
  2072. goto _end_loop;
  2073. default:
  2074. break;
  2075. }
  2076. avail = snd_pcm_playback_avail(runtime);
  2077. if (avail >= runtime->control->avail_min) {
  2078. state = READY;
  2079. break;
  2080. }
  2081. }
  2082. _end_loop:
  2083. remove_wait_queue(&runtime->sleep, &wait);
  2084. switch (state) {
  2085. case ERROR:
  2086. err = -EPIPE;
  2087. goto _end_unlock;
  2088. case SUSPENDED:
  2089. err = -ESTRPIPE;
  2090. goto _end_unlock;
  2091. case SIGNALED:
  2092. err = -ERESTARTSYS;
  2093. goto _end_unlock;
  2094. case EXPIRED:
  2095. snd_printd("playback write error (DMA or IRQ trouble?)\n");
  2096. err = -EIO;
  2097. goto _end_unlock;
  2098. case DROPPED:
  2099. err = -EBADFD;
  2100. goto _end_unlock;
  2101. default:
  2102. break;
  2103. }
  2104. }
  2105. if (avail > runtime->xfer_align)
  2106. avail -= avail % runtime->xfer_align;
  2107. frames = size > avail ? avail : size;
  2108. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  2109. if (frames > cont)
  2110. frames = cont;
  2111. snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
  2112. appl_ptr = runtime->control->appl_ptr;
  2113. appl_ofs = appl_ptr % runtime->buffer_size;
  2114. snd_pcm_stream_unlock_irq(substream);
  2115. if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
  2116. goto _end;
  2117. snd_pcm_stream_lock_irq(substream);
  2118. switch (runtime->status->state) {
  2119. case SNDRV_PCM_STATE_XRUN:
  2120. err = -EPIPE;
  2121. goto _end_unlock;
  2122. case SNDRV_PCM_STATE_SUSPENDED:
  2123. err = -ESTRPIPE;
  2124. goto _end_unlock;
  2125. default:
  2126. break;
  2127. }
  2128. appl_ptr += frames;
  2129. if (appl_ptr >= runtime->boundary)
  2130. appl_ptr -= runtime->boundary;
  2131. runtime->control->appl_ptr = appl_ptr;
  2132. if (substream->ops->ack)
  2133. substream->ops->ack(substream);
  2134. offset += frames;
  2135. size -= frames;
  2136. xfer += frames;
  2137. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
  2138. snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
  2139. err = snd_pcm_start(substream);
  2140. if (err < 0)
  2141. goto _end_unlock;
  2142. }
  2143. if (runtime->sleep_min &&
  2144. runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  2145. snd_pcm_tick_prepare(substream);
  2146. }
  2147. _end_unlock:
  2148. snd_pcm_stream_unlock_irq(substream);
  2149. _end:
  2150. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  2151. }
  2152. snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
  2153. {
  2154. struct snd_pcm_runtime *runtime;
  2155. int nonblock;
  2156. snd_assert(substream != NULL, return -ENXIO);
  2157. runtime = substream->runtime;
  2158. snd_assert(runtime != NULL, return -ENXIO);
  2159. snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
  2160. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2161. return -EBADFD;
  2162. nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
  2163. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
  2164. runtime->channels > 1)
  2165. return -EINVAL;
  2166. return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
  2167. snd_pcm_lib_write_transfer);
  2168. }
  2169. static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
  2170. unsigned int hwoff,
  2171. unsigned long data, unsigned int off,
  2172. snd_pcm_uframes_t frames)
  2173. {
  2174. struct snd_pcm_runtime *runtime = substream->runtime;
  2175. int err;
  2176. void __user **bufs = (void __user **)data;
  2177. int channels = runtime->channels;
  2178. int c;
  2179. if (substream->ops->copy) {
  2180. snd_assert(substream->ops->silence != NULL, return -EINVAL);
  2181. for (c = 0; c < channels; ++c, ++bufs) {
  2182. if (*bufs == NULL) {
  2183. if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
  2184. return err;
  2185. } else {
  2186. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  2187. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  2188. return err;
  2189. }
  2190. }
  2191. } else {
  2192. /* default transfer behaviour */
  2193. size_t dma_csize = runtime->dma_bytes / channels;
  2194. snd_assert(runtime->dma_area, return -EFAULT);
  2195. for (c = 0; c < channels; ++c, ++bufs) {
  2196. char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  2197. if (*bufs == NULL) {
  2198. snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
  2199. } else {
  2200. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  2201. if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
  2202. return -EFAULT;
  2203. }
  2204. }
  2205. }
  2206. return 0;
  2207. }
  2208. snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
  2209. void __user **bufs,
  2210. snd_pcm_uframes_t frames)
  2211. {
  2212. struct snd_pcm_runtime *runtime;
  2213. int nonblock;
  2214. snd_assert(substream != NULL, return -ENXIO);
  2215. runtime = substream->runtime;
  2216. snd_assert(runtime != NULL, return -ENXIO);
  2217. snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
  2218. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2219. return -EBADFD;
  2220. nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
  2221. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  2222. return -EINVAL;
  2223. return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
  2224. nonblock, snd_pcm_lib_writev_transfer);
  2225. }
  2226. static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
  2227. unsigned int hwoff,
  2228. unsigned long data, unsigned int off,
  2229. snd_pcm_uframes_t frames)
  2230. {
  2231. struct snd_pcm_runtime *runtime = substream->runtime;
  2232. int err;
  2233. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  2234. if (substream->ops->copy) {
  2235. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  2236. return err;
  2237. } else {
  2238. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  2239. snd_assert(runtime->dma_area, return -EFAULT);
  2240. if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
  2241. return -EFAULT;
  2242. }
  2243. return 0;
  2244. }
  2245. static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
  2246. unsigned long data,
  2247. snd_pcm_uframes_t size,
  2248. int nonblock,
  2249. transfer_f transfer)
  2250. {
  2251. struct snd_pcm_runtime *runtime = substream->runtime;
  2252. snd_pcm_uframes_t xfer = 0;
  2253. snd_pcm_uframes_t offset = 0;
  2254. int err = 0;
  2255. if (size == 0)
  2256. return 0;
  2257. if (size > runtime->xfer_align)
  2258. size -= size % runtime->xfer_align;
  2259. snd_pcm_stream_lock_irq(substream);
  2260. switch (runtime->status->state) {
  2261. case SNDRV_PCM_STATE_PREPARED:
  2262. if (size >= runtime->start_threshold) {
  2263. err = snd_pcm_start(substream);
  2264. if (err < 0)
  2265. goto _end_unlock;
  2266. }
  2267. break;
  2268. case SNDRV_PCM_STATE_DRAINING:
  2269. case SNDRV_PCM_STATE_RUNNING:
  2270. case SNDRV_PCM_STATE_PAUSED:
  2271. break;
  2272. case SNDRV_PCM_STATE_XRUN:
  2273. err = -EPIPE;
  2274. goto _end_unlock;
  2275. case SNDRV_PCM_STATE_SUSPENDED:
  2276. err = -ESTRPIPE;
  2277. goto _end_unlock;
  2278. default:
  2279. err = -EBADFD;
  2280. goto _end_unlock;
  2281. }
  2282. while (size > 0) {
  2283. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  2284. snd_pcm_uframes_t avail;
  2285. snd_pcm_uframes_t cont;
  2286. if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  2287. snd_pcm_update_hw_ptr(substream);
  2288. __draining:
  2289. avail = snd_pcm_capture_avail(runtime);
  2290. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  2291. if (avail < runtime->xfer_align) {
  2292. err = -EPIPE;
  2293. goto _end_unlock;
  2294. }
  2295. } else if ((avail < runtime->control->avail_min && size > avail) ||
  2296. (size >= runtime->xfer_align && avail < runtime->xfer_align)) {
  2297. wait_queue_t wait;
  2298. enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
  2299. long tout;
  2300. if (nonblock) {
  2301. err = -EAGAIN;
  2302. goto _end_unlock;
  2303. }
  2304. init_waitqueue_entry(&wait, current);
  2305. add_wait_queue(&runtime->sleep, &wait);
  2306. while (1) {
  2307. if (signal_pending(current)) {
  2308. state = SIGNALED;
  2309. break;
  2310. }
  2311. set_current_state(TASK_INTERRUPTIBLE);
  2312. snd_pcm_stream_unlock_irq(substream);
  2313. tout = schedule_timeout(10 * HZ);
  2314. snd_pcm_stream_lock_irq(substream);
  2315. if (tout == 0) {
  2316. if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
  2317. runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
  2318. state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
  2319. break;
  2320. }
  2321. }
  2322. switch (runtime->status->state) {
  2323. case SNDRV_PCM_STATE_XRUN:
  2324. state = ERROR;
  2325. goto _end_loop;
  2326. case SNDRV_PCM_STATE_SUSPENDED:
  2327. state = SUSPENDED;
  2328. goto _end_loop;
  2329. case SNDRV_PCM_STATE_DRAINING:
  2330. goto __draining;
  2331. case SNDRV_PCM_STATE_SETUP:
  2332. state = DROPPED;
  2333. goto _end_loop;
  2334. default:
  2335. break;
  2336. }
  2337. avail = snd_pcm_capture_avail(runtime);
  2338. if (avail >= runtime->control->avail_min) {
  2339. state = READY;
  2340. break;
  2341. }
  2342. }
  2343. _end_loop:
  2344. remove_wait_queue(&runtime->sleep, &wait);
  2345. switch (state) {
  2346. case ERROR:
  2347. err = -EPIPE;
  2348. goto _end_unlock;
  2349. case SUSPENDED:
  2350. err = -ESTRPIPE;
  2351. goto _end_unlock;
  2352. case SIGNALED:
  2353. err = -ERESTARTSYS;
  2354. goto _end_unlock;
  2355. case EXPIRED:
  2356. snd_printd("capture read error (DMA or IRQ trouble?)\n");
  2357. err = -EIO;
  2358. goto _end_unlock;
  2359. case DROPPED:
  2360. err = -EBADFD;
  2361. goto _end_unlock;
  2362. default:
  2363. break;
  2364. }
  2365. }
  2366. if (avail > runtime->xfer_align)
  2367. avail -= avail % runtime->xfer_align;
  2368. frames = size > avail ? avail : size;
  2369. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  2370. if (frames > cont)
  2371. frames = cont;
  2372. snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
  2373. appl_ptr = runtime->control->appl_ptr;
  2374. appl_ofs = appl_ptr % runtime->buffer_size;
  2375. snd_pcm_stream_unlock_irq(substream);
  2376. if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
  2377. goto _end;
  2378. snd_pcm_stream_lock_irq(substream);
  2379. switch (runtime->status->state) {
  2380. case SNDRV_PCM_STATE_XRUN:
  2381. err = -EPIPE;
  2382. goto _end_unlock;
  2383. case SNDRV_PCM_STATE_SUSPENDED:
  2384. err = -ESTRPIPE;
  2385. goto _end_unlock;
  2386. default:
  2387. break;
  2388. }
  2389. appl_ptr += frames;
  2390. if (appl_ptr >= runtime->boundary)
  2391. appl_ptr -= runtime->boundary;
  2392. runtime->control->appl_ptr = appl_ptr;
  2393. if (substream->ops->ack)
  2394. substream->ops->ack(substream);
  2395. offset += frames;
  2396. size -= frames;
  2397. xfer += frames;
  2398. if (runtime->sleep_min &&
  2399. runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  2400. snd_pcm_tick_prepare(substream);
  2401. }
  2402. _end_unlock:
  2403. snd_pcm_stream_unlock_irq(substream);
  2404. _end:
  2405. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  2406. }
  2407. snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
  2408. {
  2409. struct snd_pcm_runtime *runtime;
  2410. int nonblock;
  2411. snd_assert(substream != NULL, return -ENXIO);
  2412. runtime = substream->runtime;
  2413. snd_assert(runtime != NULL, return -ENXIO);
  2414. snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
  2415. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2416. return -EBADFD;
  2417. nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
  2418. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
  2419. return -EINVAL;
  2420. return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
  2421. }
  2422. static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
  2423. unsigned int hwoff,
  2424. unsigned long data, unsigned int off,
  2425. snd_pcm_uframes_t frames)
  2426. {
  2427. struct snd_pcm_runtime *runtime = substream->runtime;
  2428. int err;
  2429. void __user **bufs = (void __user **)data;
  2430. int channels = runtime->channels;
  2431. int c;
  2432. if (substream->ops->copy) {
  2433. for (c = 0; c < channels; ++c, ++bufs) {
  2434. char __user *buf;
  2435. if (*bufs == NULL)
  2436. continue;
  2437. buf = *bufs + samples_to_bytes(runtime, off);
  2438. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  2439. return err;
  2440. }
  2441. } else {
  2442. snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
  2443. snd_assert(runtime->dma_area, return -EFAULT);
  2444. for (c = 0; c < channels; ++c, ++bufs) {
  2445. char *hwbuf;
  2446. char __user *buf;
  2447. if (*bufs == NULL)
  2448. continue;
  2449. hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  2450. buf = *bufs + samples_to_bytes(runtime, off);
  2451. if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
  2452. return -EFAULT;
  2453. }
  2454. }
  2455. return 0;
  2456. }
  2457. snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
  2458. void __user **bufs,
  2459. snd_pcm_uframes_t frames)
  2460. {
  2461. struct snd_pcm_runtime *runtime;
  2462. int nonblock;
  2463. snd_assert(substream != NULL, return -ENXIO);
  2464. runtime = substream->runtime;
  2465. snd_assert(runtime != NULL, return -ENXIO);
  2466. snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
  2467. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2468. return -EBADFD;
  2469. nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
  2470. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  2471. return -EINVAL;
  2472. return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
  2473. }
  2474. /*
  2475. * Exported symbols
  2476. */
  2477. EXPORT_SYMBOL(snd_interval_refine);
  2478. EXPORT_SYMBOL(snd_interval_list);
  2479. EXPORT_SYMBOL(snd_interval_ratnum);
  2480. EXPORT_SYMBOL(_snd_pcm_hw_params_any);
  2481. EXPORT_SYMBOL(_snd_pcm_hw_param_min);
  2482. EXPORT_SYMBOL(_snd_pcm_hw_param_set);
  2483. EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
  2484. EXPORT_SYMBOL(_snd_pcm_hw_param_setinteger);
  2485. EXPORT_SYMBOL(snd_pcm_hw_param_value_min);
  2486. EXPORT_SYMBOL(snd_pcm_hw_param_value_max);
  2487. EXPORT_SYMBOL(snd_pcm_hw_param_mask);
  2488. EXPORT_SYMBOL(snd_pcm_hw_param_first);
  2489. EXPORT_SYMBOL(snd_pcm_hw_param_last);
  2490. EXPORT_SYMBOL(snd_pcm_hw_param_near);
  2491. EXPORT_SYMBOL(snd_pcm_hw_param_set);
  2492. EXPORT_SYMBOL(snd_pcm_hw_refine);
  2493. EXPORT_SYMBOL(snd_pcm_hw_constraints_init);
  2494. EXPORT_SYMBOL(snd_pcm_hw_constraints_complete);
  2495. EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
  2496. EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
  2497. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
  2498. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
  2499. EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
  2500. EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
  2501. EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
  2502. EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
  2503. EXPORT_SYMBOL(snd_pcm_hw_rule_add);
  2504. EXPORT_SYMBOL(snd_pcm_set_ops);
  2505. EXPORT_SYMBOL(snd_pcm_set_sync);
  2506. EXPORT_SYMBOL(snd_pcm_lib_ioctl);
  2507. EXPORT_SYMBOL(snd_pcm_stop);
  2508. EXPORT_SYMBOL(snd_pcm_period_elapsed);
  2509. EXPORT_SYMBOL(snd_pcm_lib_write);
  2510. EXPORT_SYMBOL(snd_pcm_lib_read);
  2511. EXPORT_SYMBOL(snd_pcm_lib_writev);
  2512. EXPORT_SYMBOL(snd_pcm_lib_readv);
  2513. EXPORT_SYMBOL(snd_pcm_lib_buffer_bytes);
  2514. EXPORT_SYMBOL(snd_pcm_lib_period_bytes);
  2515. /* pcm_memory.c */
  2516. EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
  2517. EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
  2518. EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
  2519. EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page);
  2520. EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
  2521. EXPORT_SYMBOL(snd_pcm_lib_free_pages);