pcm_lib.c 57 KB

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