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