pcm_lib.c 57 KB

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