pcm_lib.c 72 KB

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