pcm_lib.c 77 KB

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