pcm_lib.c 62 KB

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