pcm_lib.c 68 KB

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