pcm_lib.c 60 KB

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