pcm_lib.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127
  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. static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
  1085. snd_pcm_hw_param_t var)
  1086. {
  1087. if (hw_is_mask(var)) {
  1088. snd_mask_any(hw_param_mask(params, var));
  1089. params->cmask |= 1 << var;
  1090. params->rmask |= 1 << var;
  1091. return;
  1092. }
  1093. if (hw_is_interval(var)) {
  1094. snd_interval_any(hw_param_interval(params, var));
  1095. params->cmask |= 1 << var;
  1096. params->rmask |= 1 << var;
  1097. return;
  1098. }
  1099. snd_BUG();
  1100. }
  1101. void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
  1102. {
  1103. unsigned int k;
  1104. memset(params, 0, sizeof(*params));
  1105. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
  1106. _snd_pcm_hw_param_any(params, k);
  1107. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
  1108. _snd_pcm_hw_param_any(params, k);
  1109. params->info = ~0U;
  1110. }
  1111. EXPORT_SYMBOL(_snd_pcm_hw_params_any);
  1112. /**
  1113. * snd_pcm_hw_param_value
  1114. * @params: the hw_params instance
  1115. * @var: parameter to retrieve
  1116. * @dir: pointer to the direction (-1,0,1) or NULL
  1117. *
  1118. * Return the value for field PAR if it's fixed in configuration space
  1119. * defined by PARAMS. Return -EINVAL otherwise
  1120. */
  1121. int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
  1122. snd_pcm_hw_param_t var, int *dir)
  1123. {
  1124. if (hw_is_mask(var)) {
  1125. const struct snd_mask *mask = hw_param_mask_c(params, var);
  1126. if (!snd_mask_single(mask))
  1127. return -EINVAL;
  1128. if (dir)
  1129. *dir = 0;
  1130. return snd_mask_value(mask);
  1131. }
  1132. if (hw_is_interval(var)) {
  1133. const struct snd_interval *i = hw_param_interval_c(params, var);
  1134. if (!snd_interval_single(i))
  1135. return -EINVAL;
  1136. if (dir)
  1137. *dir = i->openmin;
  1138. return snd_interval_value(i);
  1139. }
  1140. return -EINVAL;
  1141. }
  1142. EXPORT_SYMBOL(snd_pcm_hw_param_value);
  1143. void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
  1144. snd_pcm_hw_param_t var)
  1145. {
  1146. if (hw_is_mask(var)) {
  1147. snd_mask_none(hw_param_mask(params, var));
  1148. params->cmask |= 1 << var;
  1149. params->rmask |= 1 << var;
  1150. } else if (hw_is_interval(var)) {
  1151. snd_interval_none(hw_param_interval(params, var));
  1152. params->cmask |= 1 << var;
  1153. params->rmask |= 1 << var;
  1154. } else {
  1155. snd_BUG();
  1156. }
  1157. }
  1158. EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
  1159. static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
  1160. snd_pcm_hw_param_t var)
  1161. {
  1162. int changed;
  1163. if (hw_is_mask(var))
  1164. changed = snd_mask_refine_first(hw_param_mask(params, var));
  1165. else if (hw_is_interval(var))
  1166. changed = snd_interval_refine_first(hw_param_interval(params, var));
  1167. else
  1168. return -EINVAL;
  1169. if (changed) {
  1170. params->cmask |= 1 << var;
  1171. params->rmask |= 1 << var;
  1172. }
  1173. return changed;
  1174. }
  1175. /**
  1176. * snd_pcm_hw_param_first
  1177. * @pcm: PCM instance
  1178. * @params: the hw_params instance
  1179. * @var: parameter to retrieve
  1180. * @dir: pointer to the direction (-1,0,1) or NULL
  1181. *
  1182. * Inside configuration space defined by PARAMS remove from PAR all
  1183. * values > minimum. Reduce configuration space accordingly.
  1184. * Return the minimum.
  1185. */
  1186. int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
  1187. struct snd_pcm_hw_params *params,
  1188. snd_pcm_hw_param_t var, int *dir)
  1189. {
  1190. int changed = _snd_pcm_hw_param_first(params, var);
  1191. if (changed < 0)
  1192. return changed;
  1193. if (params->rmask) {
  1194. int err = snd_pcm_hw_refine(pcm, params);
  1195. snd_assert(err >= 0, return err);
  1196. }
  1197. return snd_pcm_hw_param_value(params, var, dir);
  1198. }
  1199. EXPORT_SYMBOL(snd_pcm_hw_param_first);
  1200. static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
  1201. snd_pcm_hw_param_t var)
  1202. {
  1203. int changed;
  1204. if (hw_is_mask(var))
  1205. changed = snd_mask_refine_last(hw_param_mask(params, var));
  1206. else if (hw_is_interval(var))
  1207. changed = snd_interval_refine_last(hw_param_interval(params, var));
  1208. else
  1209. return -EINVAL;
  1210. if (changed) {
  1211. params->cmask |= 1 << var;
  1212. params->rmask |= 1 << var;
  1213. }
  1214. return changed;
  1215. }
  1216. /**
  1217. * snd_pcm_hw_param_last
  1218. * @pcm: PCM instance
  1219. * @params: the hw_params instance
  1220. * @var: parameter to retrieve
  1221. * @dir: pointer to the direction (-1,0,1) or NULL
  1222. *
  1223. * Inside configuration space defined by PARAMS remove from PAR all
  1224. * values < maximum. Reduce configuration space accordingly.
  1225. * Return the maximum.
  1226. */
  1227. int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
  1228. struct snd_pcm_hw_params *params,
  1229. snd_pcm_hw_param_t var, int *dir)
  1230. {
  1231. int changed = _snd_pcm_hw_param_last(params, var);
  1232. if (changed < 0)
  1233. return changed;
  1234. if (params->rmask) {
  1235. int err = snd_pcm_hw_refine(pcm, params);
  1236. snd_assert(err >= 0, return err);
  1237. }
  1238. return snd_pcm_hw_param_value(params, var, dir);
  1239. }
  1240. EXPORT_SYMBOL(snd_pcm_hw_param_last);
  1241. /**
  1242. * snd_pcm_hw_param_choose
  1243. * @pcm: PCM instance
  1244. * @params: the hw_params instance
  1245. *
  1246. * Choose one configuration from configuration space defined by PARAMS
  1247. * The configuration chosen is that obtained fixing in this order:
  1248. * first access, first format, first subformat, min channels,
  1249. * min rate, min period time, max buffer size, min tick time
  1250. */
  1251. int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
  1252. struct snd_pcm_hw_params *params)
  1253. {
  1254. static int vars[] = {
  1255. SNDRV_PCM_HW_PARAM_ACCESS,
  1256. SNDRV_PCM_HW_PARAM_FORMAT,
  1257. SNDRV_PCM_HW_PARAM_SUBFORMAT,
  1258. SNDRV_PCM_HW_PARAM_CHANNELS,
  1259. SNDRV_PCM_HW_PARAM_RATE,
  1260. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1261. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1262. SNDRV_PCM_HW_PARAM_TICK_TIME,
  1263. -1
  1264. };
  1265. int err, *v;
  1266. for (v = vars; *v != -1; v++) {
  1267. if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
  1268. err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
  1269. else
  1270. err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
  1271. snd_assert(err >= 0, return err);
  1272. }
  1273. return 0;
  1274. }
  1275. static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
  1276. void *arg)
  1277. {
  1278. struct snd_pcm_runtime *runtime = substream->runtime;
  1279. unsigned long flags;
  1280. snd_pcm_stream_lock_irqsave(substream, flags);
  1281. if (snd_pcm_running(substream) &&
  1282. snd_pcm_update_hw_ptr(substream) >= 0)
  1283. runtime->status->hw_ptr %= runtime->buffer_size;
  1284. else
  1285. runtime->status->hw_ptr = 0;
  1286. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1287. return 0;
  1288. }
  1289. static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
  1290. void *arg)
  1291. {
  1292. struct snd_pcm_channel_info *info = arg;
  1293. struct snd_pcm_runtime *runtime = substream->runtime;
  1294. int width;
  1295. if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
  1296. info->offset = -1;
  1297. return 0;
  1298. }
  1299. width = snd_pcm_format_physical_width(runtime->format);
  1300. if (width < 0)
  1301. return width;
  1302. info->offset = 0;
  1303. switch (runtime->access) {
  1304. case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
  1305. case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
  1306. info->first = info->channel * width;
  1307. info->step = runtime->channels * width;
  1308. break;
  1309. case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
  1310. case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
  1311. {
  1312. size_t size = runtime->dma_bytes / runtime->channels;
  1313. info->first = info->channel * size * 8;
  1314. info->step = width;
  1315. break;
  1316. }
  1317. default:
  1318. snd_BUG();
  1319. break;
  1320. }
  1321. return 0;
  1322. }
  1323. /**
  1324. * snd_pcm_lib_ioctl - a generic PCM ioctl callback
  1325. * @substream: the pcm substream instance
  1326. * @cmd: ioctl command
  1327. * @arg: ioctl argument
  1328. *
  1329. * Processes the generic ioctl commands for PCM.
  1330. * Can be passed as the ioctl callback for PCM ops.
  1331. *
  1332. * Returns zero if successful, or a negative error code on failure.
  1333. */
  1334. int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  1335. unsigned int cmd, void *arg)
  1336. {
  1337. switch (cmd) {
  1338. case SNDRV_PCM_IOCTL1_INFO:
  1339. return 0;
  1340. case SNDRV_PCM_IOCTL1_RESET:
  1341. return snd_pcm_lib_ioctl_reset(substream, arg);
  1342. case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
  1343. return snd_pcm_lib_ioctl_channel_info(substream, arg);
  1344. }
  1345. return -ENXIO;
  1346. }
  1347. EXPORT_SYMBOL(snd_pcm_lib_ioctl);
  1348. /*
  1349. * Conditions
  1350. */
  1351. static void snd_pcm_system_tick_set(struct snd_pcm_substream *substream,
  1352. unsigned long ticks)
  1353. {
  1354. struct snd_pcm_runtime *runtime = substream->runtime;
  1355. if (ticks == 0)
  1356. del_timer(&runtime->tick_timer);
  1357. else {
  1358. ticks += (1000000 / HZ) - 1;
  1359. ticks /= (1000000 / HZ);
  1360. mod_timer(&runtime->tick_timer, jiffies + ticks);
  1361. }
  1362. }
  1363. /* Temporary alias */
  1364. void snd_pcm_tick_set(struct snd_pcm_substream *substream, unsigned long ticks)
  1365. {
  1366. snd_pcm_system_tick_set(substream, ticks);
  1367. }
  1368. void snd_pcm_tick_prepare(struct snd_pcm_substream *substream)
  1369. {
  1370. struct snd_pcm_runtime *runtime = substream->runtime;
  1371. snd_pcm_uframes_t frames = ULONG_MAX;
  1372. snd_pcm_uframes_t avail, dist;
  1373. unsigned int ticks;
  1374. u_int64_t n;
  1375. u_int32_t r;
  1376. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1377. if (runtime->silence_size >= runtime->boundary) {
  1378. frames = 1;
  1379. } else if (runtime->silence_size > 0 &&
  1380. runtime->silence_filled < runtime->buffer_size) {
  1381. snd_pcm_sframes_t noise_dist;
  1382. noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
  1383. if (noise_dist > (snd_pcm_sframes_t)runtime->silence_threshold)
  1384. frames = noise_dist - runtime->silence_threshold;
  1385. }
  1386. avail = snd_pcm_playback_avail(runtime);
  1387. } else {
  1388. avail = snd_pcm_capture_avail(runtime);
  1389. }
  1390. if (avail < runtime->control->avail_min) {
  1391. snd_pcm_sframes_t n = runtime->control->avail_min - avail;
  1392. if (n > 0 && frames > (snd_pcm_uframes_t)n)
  1393. frames = n;
  1394. }
  1395. if (avail < runtime->buffer_size) {
  1396. snd_pcm_sframes_t n = runtime->buffer_size - avail;
  1397. if (n > 0 && frames > (snd_pcm_uframes_t)n)
  1398. frames = n;
  1399. }
  1400. if (frames == ULONG_MAX) {
  1401. snd_pcm_tick_set(substream, 0);
  1402. return;
  1403. }
  1404. dist = runtime->status->hw_ptr - runtime->hw_ptr_base;
  1405. /* Distance to next interrupt */
  1406. dist = runtime->period_size - dist % runtime->period_size;
  1407. if (dist <= frames) {
  1408. snd_pcm_tick_set(substream, 0);
  1409. return;
  1410. }
  1411. /* the base time is us */
  1412. n = frames;
  1413. n *= 1000000;
  1414. div64_32(&n, runtime->tick_time * runtime->rate, &r);
  1415. ticks = n + (r > 0 ? 1 : 0);
  1416. if (ticks < runtime->sleep_min)
  1417. ticks = runtime->sleep_min;
  1418. snd_pcm_tick_set(substream, (unsigned long) ticks);
  1419. }
  1420. void snd_pcm_tick_elapsed(struct snd_pcm_substream *substream)
  1421. {
  1422. struct snd_pcm_runtime *runtime;
  1423. unsigned long flags;
  1424. snd_assert(substream != NULL, return);
  1425. runtime = substream->runtime;
  1426. snd_assert(runtime != NULL, return);
  1427. snd_pcm_stream_lock_irqsave(substream, flags);
  1428. if (!snd_pcm_running(substream) ||
  1429. snd_pcm_update_hw_ptr(substream) < 0)
  1430. goto _end;
  1431. if (runtime->sleep_min)
  1432. snd_pcm_tick_prepare(substream);
  1433. _end:
  1434. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1435. }
  1436. /**
  1437. * snd_pcm_period_elapsed - update the pcm status for the next period
  1438. * @substream: the pcm substream instance
  1439. *
  1440. * This function is called from the interrupt handler when the
  1441. * PCM has processed the period size. It will update the current
  1442. * pointer, set up the tick, wake up sleepers, etc.
  1443. *
  1444. * Even if more than one periods have elapsed since the last call, you
  1445. * have to call this only once.
  1446. */
  1447. void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
  1448. {
  1449. struct snd_pcm_runtime *runtime;
  1450. unsigned long flags;
  1451. snd_assert(substream != NULL, return);
  1452. runtime = substream->runtime;
  1453. snd_assert(runtime != NULL, return);
  1454. if (runtime->transfer_ack_begin)
  1455. runtime->transfer_ack_begin(substream);
  1456. snd_pcm_stream_lock_irqsave(substream, flags);
  1457. if (!snd_pcm_running(substream) ||
  1458. snd_pcm_update_hw_ptr_interrupt(substream) < 0)
  1459. goto _end;
  1460. if (substream->timer_running)
  1461. snd_timer_interrupt(substream->timer, 1);
  1462. if (runtime->sleep_min)
  1463. snd_pcm_tick_prepare(substream);
  1464. _end:
  1465. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1466. if (runtime->transfer_ack_end)
  1467. runtime->transfer_ack_end(substream);
  1468. kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
  1469. }
  1470. EXPORT_SYMBOL(snd_pcm_period_elapsed);
  1471. static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
  1472. unsigned int hwoff,
  1473. unsigned long data, unsigned int off,
  1474. snd_pcm_uframes_t frames)
  1475. {
  1476. struct snd_pcm_runtime *runtime = substream->runtime;
  1477. int err;
  1478. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  1479. if (substream->ops->copy) {
  1480. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  1481. return err;
  1482. } else {
  1483. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  1484. snd_assert(runtime->dma_area, return -EFAULT);
  1485. if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
  1486. return -EFAULT;
  1487. }
  1488. return 0;
  1489. }
  1490. typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
  1491. unsigned long data, unsigned int off,
  1492. snd_pcm_uframes_t size);
  1493. static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
  1494. unsigned long data,
  1495. snd_pcm_uframes_t size,
  1496. int nonblock,
  1497. transfer_f transfer)
  1498. {
  1499. struct snd_pcm_runtime *runtime = substream->runtime;
  1500. snd_pcm_uframes_t xfer = 0;
  1501. snd_pcm_uframes_t offset = 0;
  1502. int err = 0;
  1503. if (size == 0)
  1504. return 0;
  1505. if (size > runtime->xfer_align)
  1506. size -= size % runtime->xfer_align;
  1507. snd_pcm_stream_lock_irq(substream);
  1508. switch (runtime->status->state) {
  1509. case SNDRV_PCM_STATE_PREPARED:
  1510. case SNDRV_PCM_STATE_RUNNING:
  1511. case SNDRV_PCM_STATE_PAUSED:
  1512. break;
  1513. case SNDRV_PCM_STATE_XRUN:
  1514. err = -EPIPE;
  1515. goto _end_unlock;
  1516. case SNDRV_PCM_STATE_SUSPENDED:
  1517. err = -ESTRPIPE;
  1518. goto _end_unlock;
  1519. default:
  1520. err = -EBADFD;
  1521. goto _end_unlock;
  1522. }
  1523. while (size > 0) {
  1524. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  1525. snd_pcm_uframes_t avail;
  1526. snd_pcm_uframes_t cont;
  1527. if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1528. snd_pcm_update_hw_ptr(substream);
  1529. avail = snd_pcm_playback_avail(runtime);
  1530. if (((avail < runtime->control->avail_min && size > avail) ||
  1531. (size >= runtime->xfer_align && avail < runtime->xfer_align))) {
  1532. wait_queue_t wait;
  1533. enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
  1534. long tout;
  1535. if (nonblock) {
  1536. err = -EAGAIN;
  1537. goto _end_unlock;
  1538. }
  1539. init_waitqueue_entry(&wait, current);
  1540. add_wait_queue(&runtime->sleep, &wait);
  1541. while (1) {
  1542. if (signal_pending(current)) {
  1543. state = SIGNALED;
  1544. break;
  1545. }
  1546. set_current_state(TASK_INTERRUPTIBLE);
  1547. snd_pcm_stream_unlock_irq(substream);
  1548. tout = schedule_timeout(10 * HZ);
  1549. snd_pcm_stream_lock_irq(substream);
  1550. if (tout == 0) {
  1551. if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
  1552. runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
  1553. state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
  1554. break;
  1555. }
  1556. }
  1557. switch (runtime->status->state) {
  1558. case SNDRV_PCM_STATE_XRUN:
  1559. case SNDRV_PCM_STATE_DRAINING:
  1560. state = ERROR;
  1561. goto _end_loop;
  1562. case SNDRV_PCM_STATE_SUSPENDED:
  1563. state = SUSPENDED;
  1564. goto _end_loop;
  1565. case SNDRV_PCM_STATE_SETUP:
  1566. state = DROPPED;
  1567. goto _end_loop;
  1568. default:
  1569. break;
  1570. }
  1571. avail = snd_pcm_playback_avail(runtime);
  1572. if (avail >= runtime->control->avail_min) {
  1573. state = READY;
  1574. break;
  1575. }
  1576. }
  1577. _end_loop:
  1578. remove_wait_queue(&runtime->sleep, &wait);
  1579. switch (state) {
  1580. case ERROR:
  1581. err = -EPIPE;
  1582. goto _end_unlock;
  1583. case SUSPENDED:
  1584. err = -ESTRPIPE;
  1585. goto _end_unlock;
  1586. case SIGNALED:
  1587. err = -ERESTARTSYS;
  1588. goto _end_unlock;
  1589. case EXPIRED:
  1590. snd_printd("playback write error (DMA or IRQ trouble?)\n");
  1591. err = -EIO;
  1592. goto _end_unlock;
  1593. case DROPPED:
  1594. err = -EBADFD;
  1595. goto _end_unlock;
  1596. default:
  1597. break;
  1598. }
  1599. }
  1600. if (avail > runtime->xfer_align)
  1601. avail -= avail % runtime->xfer_align;
  1602. frames = size > avail ? avail : size;
  1603. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  1604. if (frames > cont)
  1605. frames = cont;
  1606. snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
  1607. appl_ptr = runtime->control->appl_ptr;
  1608. appl_ofs = appl_ptr % runtime->buffer_size;
  1609. snd_pcm_stream_unlock_irq(substream);
  1610. if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
  1611. goto _end;
  1612. snd_pcm_stream_lock_irq(substream);
  1613. switch (runtime->status->state) {
  1614. case SNDRV_PCM_STATE_XRUN:
  1615. err = -EPIPE;
  1616. goto _end_unlock;
  1617. case SNDRV_PCM_STATE_SUSPENDED:
  1618. err = -ESTRPIPE;
  1619. goto _end_unlock;
  1620. default:
  1621. break;
  1622. }
  1623. appl_ptr += frames;
  1624. if (appl_ptr >= runtime->boundary)
  1625. appl_ptr -= runtime->boundary;
  1626. runtime->control->appl_ptr = appl_ptr;
  1627. if (substream->ops->ack)
  1628. substream->ops->ack(substream);
  1629. offset += frames;
  1630. size -= frames;
  1631. xfer += frames;
  1632. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
  1633. snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
  1634. err = snd_pcm_start(substream);
  1635. if (err < 0)
  1636. goto _end_unlock;
  1637. }
  1638. if (runtime->sleep_min &&
  1639. runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1640. snd_pcm_tick_prepare(substream);
  1641. }
  1642. _end_unlock:
  1643. snd_pcm_stream_unlock_irq(substream);
  1644. _end:
  1645. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  1646. }
  1647. snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
  1648. {
  1649. struct snd_pcm_runtime *runtime;
  1650. int nonblock;
  1651. snd_assert(substream != NULL, return -ENXIO);
  1652. runtime = substream->runtime;
  1653. snd_assert(runtime != NULL, return -ENXIO);
  1654. snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
  1655. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1656. return -EBADFD;
  1657. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1658. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
  1659. runtime->channels > 1)
  1660. return -EINVAL;
  1661. return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
  1662. snd_pcm_lib_write_transfer);
  1663. }
  1664. EXPORT_SYMBOL(snd_pcm_lib_write);
  1665. static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
  1666. unsigned int hwoff,
  1667. unsigned long data, unsigned int off,
  1668. snd_pcm_uframes_t frames)
  1669. {
  1670. struct snd_pcm_runtime *runtime = substream->runtime;
  1671. int err;
  1672. void __user **bufs = (void __user **)data;
  1673. int channels = runtime->channels;
  1674. int c;
  1675. if (substream->ops->copy) {
  1676. snd_assert(substream->ops->silence != NULL, return -EINVAL);
  1677. for (c = 0; c < channels; ++c, ++bufs) {
  1678. if (*bufs == NULL) {
  1679. if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
  1680. return err;
  1681. } else {
  1682. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  1683. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  1684. return err;
  1685. }
  1686. }
  1687. } else {
  1688. /* default transfer behaviour */
  1689. size_t dma_csize = runtime->dma_bytes / channels;
  1690. snd_assert(runtime->dma_area, return -EFAULT);
  1691. for (c = 0; c < channels; ++c, ++bufs) {
  1692. char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  1693. if (*bufs == NULL) {
  1694. snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
  1695. } else {
  1696. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  1697. if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
  1698. return -EFAULT;
  1699. }
  1700. }
  1701. }
  1702. return 0;
  1703. }
  1704. snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
  1705. void __user **bufs,
  1706. snd_pcm_uframes_t frames)
  1707. {
  1708. struct snd_pcm_runtime *runtime;
  1709. int nonblock;
  1710. snd_assert(substream != NULL, return -ENXIO);
  1711. runtime = substream->runtime;
  1712. snd_assert(runtime != NULL, return -ENXIO);
  1713. snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
  1714. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1715. return -EBADFD;
  1716. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1717. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  1718. return -EINVAL;
  1719. return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
  1720. nonblock, snd_pcm_lib_writev_transfer);
  1721. }
  1722. EXPORT_SYMBOL(snd_pcm_lib_writev);
  1723. static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
  1724. unsigned int hwoff,
  1725. unsigned long data, unsigned int off,
  1726. snd_pcm_uframes_t frames)
  1727. {
  1728. struct snd_pcm_runtime *runtime = substream->runtime;
  1729. int err;
  1730. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  1731. if (substream->ops->copy) {
  1732. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  1733. return err;
  1734. } else {
  1735. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  1736. snd_assert(runtime->dma_area, return -EFAULT);
  1737. if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
  1738. return -EFAULT;
  1739. }
  1740. return 0;
  1741. }
  1742. static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
  1743. unsigned long data,
  1744. snd_pcm_uframes_t size,
  1745. int nonblock,
  1746. transfer_f transfer)
  1747. {
  1748. struct snd_pcm_runtime *runtime = substream->runtime;
  1749. snd_pcm_uframes_t xfer = 0;
  1750. snd_pcm_uframes_t offset = 0;
  1751. int err = 0;
  1752. if (size == 0)
  1753. return 0;
  1754. if (size > runtime->xfer_align)
  1755. size -= size % runtime->xfer_align;
  1756. snd_pcm_stream_lock_irq(substream);
  1757. switch (runtime->status->state) {
  1758. case SNDRV_PCM_STATE_PREPARED:
  1759. if (size >= runtime->start_threshold) {
  1760. err = snd_pcm_start(substream);
  1761. if (err < 0)
  1762. goto _end_unlock;
  1763. }
  1764. break;
  1765. case SNDRV_PCM_STATE_DRAINING:
  1766. case SNDRV_PCM_STATE_RUNNING:
  1767. case SNDRV_PCM_STATE_PAUSED:
  1768. break;
  1769. case SNDRV_PCM_STATE_XRUN:
  1770. err = -EPIPE;
  1771. goto _end_unlock;
  1772. case SNDRV_PCM_STATE_SUSPENDED:
  1773. err = -ESTRPIPE;
  1774. goto _end_unlock;
  1775. default:
  1776. err = -EBADFD;
  1777. goto _end_unlock;
  1778. }
  1779. while (size > 0) {
  1780. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  1781. snd_pcm_uframes_t avail;
  1782. snd_pcm_uframes_t cont;
  1783. if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1784. snd_pcm_update_hw_ptr(substream);
  1785. __draining:
  1786. avail = snd_pcm_capture_avail(runtime);
  1787. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  1788. if (avail < runtime->xfer_align) {
  1789. err = -EPIPE;
  1790. goto _end_unlock;
  1791. }
  1792. } else if ((avail < runtime->control->avail_min && size > avail) ||
  1793. (size >= runtime->xfer_align && avail < runtime->xfer_align)) {
  1794. wait_queue_t wait;
  1795. enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
  1796. long tout;
  1797. if (nonblock) {
  1798. err = -EAGAIN;
  1799. goto _end_unlock;
  1800. }
  1801. init_waitqueue_entry(&wait, current);
  1802. add_wait_queue(&runtime->sleep, &wait);
  1803. while (1) {
  1804. if (signal_pending(current)) {
  1805. state = SIGNALED;
  1806. break;
  1807. }
  1808. set_current_state(TASK_INTERRUPTIBLE);
  1809. snd_pcm_stream_unlock_irq(substream);
  1810. tout = schedule_timeout(10 * HZ);
  1811. snd_pcm_stream_lock_irq(substream);
  1812. if (tout == 0) {
  1813. if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
  1814. runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
  1815. state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
  1816. break;
  1817. }
  1818. }
  1819. switch (runtime->status->state) {
  1820. case SNDRV_PCM_STATE_XRUN:
  1821. state = ERROR;
  1822. goto _end_loop;
  1823. case SNDRV_PCM_STATE_SUSPENDED:
  1824. state = SUSPENDED;
  1825. goto _end_loop;
  1826. case SNDRV_PCM_STATE_DRAINING:
  1827. goto __draining;
  1828. case SNDRV_PCM_STATE_SETUP:
  1829. state = DROPPED;
  1830. goto _end_loop;
  1831. default:
  1832. break;
  1833. }
  1834. avail = snd_pcm_capture_avail(runtime);
  1835. if (avail >= runtime->control->avail_min) {
  1836. state = READY;
  1837. break;
  1838. }
  1839. }
  1840. _end_loop:
  1841. remove_wait_queue(&runtime->sleep, &wait);
  1842. switch (state) {
  1843. case ERROR:
  1844. err = -EPIPE;
  1845. goto _end_unlock;
  1846. case SUSPENDED:
  1847. err = -ESTRPIPE;
  1848. goto _end_unlock;
  1849. case SIGNALED:
  1850. err = -ERESTARTSYS;
  1851. goto _end_unlock;
  1852. case EXPIRED:
  1853. snd_printd("capture read error (DMA or IRQ trouble?)\n");
  1854. err = -EIO;
  1855. goto _end_unlock;
  1856. case DROPPED:
  1857. err = -EBADFD;
  1858. goto _end_unlock;
  1859. default:
  1860. break;
  1861. }
  1862. }
  1863. if (avail > runtime->xfer_align)
  1864. avail -= avail % runtime->xfer_align;
  1865. frames = size > avail ? avail : size;
  1866. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  1867. if (frames > cont)
  1868. frames = cont;
  1869. snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
  1870. appl_ptr = runtime->control->appl_ptr;
  1871. appl_ofs = appl_ptr % runtime->buffer_size;
  1872. snd_pcm_stream_unlock_irq(substream);
  1873. if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
  1874. goto _end;
  1875. snd_pcm_stream_lock_irq(substream);
  1876. switch (runtime->status->state) {
  1877. case SNDRV_PCM_STATE_XRUN:
  1878. err = -EPIPE;
  1879. goto _end_unlock;
  1880. case SNDRV_PCM_STATE_SUSPENDED:
  1881. err = -ESTRPIPE;
  1882. goto _end_unlock;
  1883. default:
  1884. break;
  1885. }
  1886. appl_ptr += frames;
  1887. if (appl_ptr >= runtime->boundary)
  1888. appl_ptr -= runtime->boundary;
  1889. runtime->control->appl_ptr = appl_ptr;
  1890. if (substream->ops->ack)
  1891. substream->ops->ack(substream);
  1892. offset += frames;
  1893. size -= frames;
  1894. xfer += frames;
  1895. if (runtime->sleep_min &&
  1896. runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1897. snd_pcm_tick_prepare(substream);
  1898. }
  1899. _end_unlock:
  1900. snd_pcm_stream_unlock_irq(substream);
  1901. _end:
  1902. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  1903. }
  1904. snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
  1905. {
  1906. struct snd_pcm_runtime *runtime;
  1907. int nonblock;
  1908. snd_assert(substream != NULL, return -ENXIO);
  1909. runtime = substream->runtime;
  1910. snd_assert(runtime != NULL, return -ENXIO);
  1911. snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
  1912. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1913. return -EBADFD;
  1914. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1915. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
  1916. return -EINVAL;
  1917. return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
  1918. }
  1919. EXPORT_SYMBOL(snd_pcm_lib_read);
  1920. static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
  1921. unsigned int hwoff,
  1922. unsigned long data, unsigned int off,
  1923. snd_pcm_uframes_t frames)
  1924. {
  1925. struct snd_pcm_runtime *runtime = substream->runtime;
  1926. int err;
  1927. void __user **bufs = (void __user **)data;
  1928. int channels = runtime->channels;
  1929. int c;
  1930. if (substream->ops->copy) {
  1931. for (c = 0; c < channels; ++c, ++bufs) {
  1932. char __user *buf;
  1933. if (*bufs == NULL)
  1934. continue;
  1935. buf = *bufs + samples_to_bytes(runtime, off);
  1936. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  1937. return err;
  1938. }
  1939. } else {
  1940. snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
  1941. snd_assert(runtime->dma_area, return -EFAULT);
  1942. for (c = 0; c < channels; ++c, ++bufs) {
  1943. char *hwbuf;
  1944. char __user *buf;
  1945. if (*bufs == NULL)
  1946. continue;
  1947. hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  1948. buf = *bufs + samples_to_bytes(runtime, off);
  1949. if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
  1950. return -EFAULT;
  1951. }
  1952. }
  1953. return 0;
  1954. }
  1955. snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
  1956. void __user **bufs,
  1957. snd_pcm_uframes_t frames)
  1958. {
  1959. struct snd_pcm_runtime *runtime;
  1960. int nonblock;
  1961. snd_assert(substream != NULL, return -ENXIO);
  1962. runtime = substream->runtime;
  1963. snd_assert(runtime != NULL, return -ENXIO);
  1964. snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
  1965. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1966. return -EBADFD;
  1967. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1968. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  1969. return -EINVAL;
  1970. return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
  1971. }
  1972. EXPORT_SYMBOL(snd_pcm_lib_readv);