pcm_lib.c 61 KB

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