pcm_lib.c 55 KB

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