pcm_lib.c 58 KB

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