pcm_lib.c 70 KB

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