pcm_lib.c 59 KB

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