pcm_lib.c 59 KB

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