pcm_lib.c 52 KB

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