pcm_lib.c 57 KB

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