pcm_lib.c 59 KB

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