pcm_lib.c 68 KB

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