pcm_lib.c 61 KB

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