pcm_lib.c 58 KB

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