pcm_lib.c 71 KB

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