pcm_lib.c 58 KB

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