pcm_lib.c 58 KB

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