pcm_lib.c 60 KB

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