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