pcm_lib.c 72 KB

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