pcm_lib.c 56 KB

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