pcm_lib.c 60 KB

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