pcm_lib.c 67 KB

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