pcm_lib.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291
  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,
  967. const unsigned int *list, unsigned int mask)
  968. {
  969. unsigned int k;
  970. struct snd_interval list_range;
  971. if (!count) {
  972. i->empty = 1;
  973. return -EINVAL;
  974. }
  975. snd_interval_any(&list_range);
  976. list_range.min = UINT_MAX;
  977. list_range.max = 0;
  978. for (k = 0; k < count; k++) {
  979. if (mask && !(mask & (1 << k)))
  980. continue;
  981. if (!snd_interval_test(i, list[k]))
  982. continue;
  983. list_range.min = min(list_range.min, list[k]);
  984. list_range.max = max(list_range.max, list[k]);
  985. }
  986. return snd_interval_refine(i, &list_range);
  987. }
  988. EXPORT_SYMBOL(snd_interval_list);
  989. static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
  990. {
  991. unsigned int n;
  992. int changed = 0;
  993. n = (i->min - min) % step;
  994. if (n != 0 || i->openmin) {
  995. i->min += step - n;
  996. changed = 1;
  997. }
  998. n = (i->max - min) % step;
  999. if (n != 0 || i->openmax) {
  1000. i->max -= n;
  1001. changed = 1;
  1002. }
  1003. if (snd_interval_checkempty(i)) {
  1004. i->empty = 1;
  1005. return -EINVAL;
  1006. }
  1007. return changed;
  1008. }
  1009. /* Info constraints helpers */
  1010. /**
  1011. * snd_pcm_hw_rule_add - add the hw-constraint rule
  1012. * @runtime: the pcm runtime instance
  1013. * @cond: condition bits
  1014. * @var: the variable to evaluate
  1015. * @func: the evaluation function
  1016. * @private: the private data pointer passed to function
  1017. * @dep: the dependent variables
  1018. *
  1019. * Returns zero if successful, or a negative error code on failure.
  1020. */
  1021. int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
  1022. int var,
  1023. snd_pcm_hw_rule_func_t func, void *private,
  1024. int dep, ...)
  1025. {
  1026. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1027. struct snd_pcm_hw_rule *c;
  1028. unsigned int k;
  1029. va_list args;
  1030. va_start(args, dep);
  1031. if (constrs->rules_num >= constrs->rules_all) {
  1032. struct snd_pcm_hw_rule *new;
  1033. unsigned int new_rules = constrs->rules_all + 16;
  1034. new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
  1035. if (!new) {
  1036. va_end(args);
  1037. return -ENOMEM;
  1038. }
  1039. if (constrs->rules) {
  1040. memcpy(new, constrs->rules,
  1041. constrs->rules_num * sizeof(*c));
  1042. kfree(constrs->rules);
  1043. }
  1044. constrs->rules = new;
  1045. constrs->rules_all = new_rules;
  1046. }
  1047. c = &constrs->rules[constrs->rules_num];
  1048. c->cond = cond;
  1049. c->func = func;
  1050. c->var = var;
  1051. c->private = private;
  1052. k = 0;
  1053. while (1) {
  1054. if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
  1055. va_end(args);
  1056. return -EINVAL;
  1057. }
  1058. c->deps[k++] = dep;
  1059. if (dep < 0)
  1060. break;
  1061. dep = va_arg(args, int);
  1062. }
  1063. constrs->rules_num++;
  1064. va_end(args);
  1065. return 0;
  1066. }
  1067. EXPORT_SYMBOL(snd_pcm_hw_rule_add);
  1068. /**
  1069. * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
  1070. * @runtime: PCM runtime instance
  1071. * @var: hw_params variable to apply the mask
  1072. * @mask: the bitmap mask
  1073. *
  1074. * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
  1075. */
  1076. int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1077. u_int32_t mask)
  1078. {
  1079. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1080. struct snd_mask *maskp = constrs_mask(constrs, var);
  1081. *maskp->bits &= mask;
  1082. memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
  1083. if (*maskp->bits == 0)
  1084. return -EINVAL;
  1085. return 0;
  1086. }
  1087. /**
  1088. * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
  1089. * @runtime: PCM runtime instance
  1090. * @var: hw_params variable to apply the mask
  1091. * @mask: the 64bit bitmap mask
  1092. *
  1093. * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
  1094. */
  1095. int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1096. u_int64_t mask)
  1097. {
  1098. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1099. struct snd_mask *maskp = constrs_mask(constrs, var);
  1100. maskp->bits[0] &= (u_int32_t)mask;
  1101. maskp->bits[1] &= (u_int32_t)(mask >> 32);
  1102. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
  1103. if (! maskp->bits[0] && ! maskp->bits[1])
  1104. return -EINVAL;
  1105. return 0;
  1106. }
  1107. /**
  1108. * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
  1109. * @runtime: PCM runtime instance
  1110. * @var: hw_params variable to apply the integer constraint
  1111. *
  1112. * Apply the constraint of integer to an interval parameter.
  1113. */
  1114. int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
  1115. {
  1116. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1117. return snd_interval_setinteger(constrs_interval(constrs, var));
  1118. }
  1119. EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
  1120. /**
  1121. * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
  1122. * @runtime: PCM runtime instance
  1123. * @var: hw_params variable to apply the range
  1124. * @min: the minimal value
  1125. * @max: the maximal value
  1126. *
  1127. * Apply the min/max range constraint to an interval parameter.
  1128. */
  1129. int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1130. unsigned int min, unsigned int max)
  1131. {
  1132. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1133. struct snd_interval t;
  1134. t.min = min;
  1135. t.max = max;
  1136. t.openmin = t.openmax = 0;
  1137. t.integer = 0;
  1138. return snd_interval_refine(constrs_interval(constrs, var), &t);
  1139. }
  1140. EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
  1141. static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
  1142. struct snd_pcm_hw_rule *rule)
  1143. {
  1144. struct snd_pcm_hw_constraint_list *list = rule->private;
  1145. return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
  1146. }
  1147. /**
  1148. * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
  1149. * @runtime: PCM runtime instance
  1150. * @cond: condition bits
  1151. * @var: hw_params variable to apply the list constraint
  1152. * @l: list
  1153. *
  1154. * Apply the list of constraints to an interval parameter.
  1155. */
  1156. int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
  1157. unsigned int cond,
  1158. snd_pcm_hw_param_t var,
  1159. struct snd_pcm_hw_constraint_list *l)
  1160. {
  1161. return snd_pcm_hw_rule_add(runtime, cond, var,
  1162. snd_pcm_hw_rule_list, l,
  1163. var, -1);
  1164. }
  1165. EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
  1166. static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
  1167. struct snd_pcm_hw_rule *rule)
  1168. {
  1169. struct snd_pcm_hw_constraint_ratnums *r = rule->private;
  1170. unsigned int num = 0, den = 0;
  1171. int err;
  1172. err = snd_interval_ratnum(hw_param_interval(params, rule->var),
  1173. r->nrats, r->rats, &num, &den);
  1174. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1175. params->rate_num = num;
  1176. params->rate_den = den;
  1177. }
  1178. return err;
  1179. }
  1180. /**
  1181. * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
  1182. * @runtime: PCM runtime instance
  1183. * @cond: condition bits
  1184. * @var: hw_params variable to apply the ratnums constraint
  1185. * @r: struct snd_ratnums constriants
  1186. */
  1187. int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
  1188. unsigned int cond,
  1189. snd_pcm_hw_param_t var,
  1190. struct snd_pcm_hw_constraint_ratnums *r)
  1191. {
  1192. return snd_pcm_hw_rule_add(runtime, cond, var,
  1193. snd_pcm_hw_rule_ratnums, r,
  1194. var, -1);
  1195. }
  1196. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
  1197. static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
  1198. struct snd_pcm_hw_rule *rule)
  1199. {
  1200. struct snd_pcm_hw_constraint_ratdens *r = rule->private;
  1201. unsigned int num = 0, den = 0;
  1202. int err = snd_interval_ratden(hw_param_interval(params, rule->var),
  1203. r->nrats, r->rats, &num, &den);
  1204. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1205. params->rate_num = num;
  1206. params->rate_den = den;
  1207. }
  1208. return err;
  1209. }
  1210. /**
  1211. * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
  1212. * @runtime: PCM runtime instance
  1213. * @cond: condition bits
  1214. * @var: hw_params variable to apply the ratdens constraint
  1215. * @r: struct snd_ratdens constriants
  1216. */
  1217. int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
  1218. unsigned int cond,
  1219. snd_pcm_hw_param_t var,
  1220. struct snd_pcm_hw_constraint_ratdens *r)
  1221. {
  1222. return snd_pcm_hw_rule_add(runtime, cond, var,
  1223. snd_pcm_hw_rule_ratdens, r,
  1224. var, -1);
  1225. }
  1226. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
  1227. static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
  1228. struct snd_pcm_hw_rule *rule)
  1229. {
  1230. unsigned int l = (unsigned long) rule->private;
  1231. int width = l & 0xffff;
  1232. unsigned int msbits = l >> 16;
  1233. struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
  1234. if (snd_interval_single(i) && snd_interval_value(i) == width)
  1235. params->msbits = msbits;
  1236. return 0;
  1237. }
  1238. /**
  1239. * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
  1240. * @runtime: PCM runtime instance
  1241. * @cond: condition bits
  1242. * @width: sample bits width
  1243. * @msbits: msbits width
  1244. */
  1245. int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
  1246. unsigned int cond,
  1247. unsigned int width,
  1248. unsigned int msbits)
  1249. {
  1250. unsigned long l = (msbits << 16) | width;
  1251. return snd_pcm_hw_rule_add(runtime, cond, -1,
  1252. snd_pcm_hw_rule_msbits,
  1253. (void*) l,
  1254. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1255. }
  1256. EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
  1257. static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
  1258. struct snd_pcm_hw_rule *rule)
  1259. {
  1260. unsigned long step = (unsigned long) rule->private;
  1261. return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
  1262. }
  1263. /**
  1264. * snd_pcm_hw_constraint_step - add a hw constraint step rule
  1265. * @runtime: PCM runtime instance
  1266. * @cond: condition bits
  1267. * @var: hw_params variable to apply the step constraint
  1268. * @step: step size
  1269. */
  1270. int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
  1271. unsigned int cond,
  1272. snd_pcm_hw_param_t var,
  1273. unsigned long step)
  1274. {
  1275. return snd_pcm_hw_rule_add(runtime, cond, var,
  1276. snd_pcm_hw_rule_step, (void *) step,
  1277. var, -1);
  1278. }
  1279. EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
  1280. static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
  1281. {
  1282. static unsigned int pow2_sizes[] = {
  1283. 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
  1284. 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
  1285. 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
  1286. 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
  1287. };
  1288. return snd_interval_list(hw_param_interval(params, rule->var),
  1289. ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
  1290. }
  1291. /**
  1292. * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
  1293. * @runtime: PCM runtime instance
  1294. * @cond: condition bits
  1295. * @var: hw_params variable to apply the power-of-2 constraint
  1296. */
  1297. int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
  1298. unsigned int cond,
  1299. snd_pcm_hw_param_t var)
  1300. {
  1301. return snd_pcm_hw_rule_add(runtime, cond, var,
  1302. snd_pcm_hw_rule_pow2, NULL,
  1303. var, -1);
  1304. }
  1305. EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
  1306. static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
  1307. struct snd_pcm_hw_rule *rule)
  1308. {
  1309. unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
  1310. struct snd_interval *rate;
  1311. rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  1312. return snd_interval_list(rate, 1, &base_rate, 0);
  1313. }
  1314. /**
  1315. * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
  1316. * @runtime: PCM runtime instance
  1317. * @base_rate: the rate at which the hardware does not resample
  1318. */
  1319. int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
  1320. unsigned int base_rate)
  1321. {
  1322. return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
  1323. SNDRV_PCM_HW_PARAM_RATE,
  1324. snd_pcm_hw_rule_noresample_func,
  1325. (void *)(uintptr_t)base_rate,
  1326. SNDRV_PCM_HW_PARAM_RATE, -1);
  1327. }
  1328. EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
  1329. static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
  1330. snd_pcm_hw_param_t var)
  1331. {
  1332. if (hw_is_mask(var)) {
  1333. snd_mask_any(hw_param_mask(params, var));
  1334. params->cmask |= 1 << var;
  1335. params->rmask |= 1 << var;
  1336. return;
  1337. }
  1338. if (hw_is_interval(var)) {
  1339. snd_interval_any(hw_param_interval(params, var));
  1340. params->cmask |= 1 << var;
  1341. params->rmask |= 1 << var;
  1342. return;
  1343. }
  1344. snd_BUG();
  1345. }
  1346. void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
  1347. {
  1348. unsigned int k;
  1349. memset(params, 0, sizeof(*params));
  1350. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
  1351. _snd_pcm_hw_param_any(params, k);
  1352. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
  1353. _snd_pcm_hw_param_any(params, k);
  1354. params->info = ~0U;
  1355. }
  1356. EXPORT_SYMBOL(_snd_pcm_hw_params_any);
  1357. /**
  1358. * snd_pcm_hw_param_value - return @params field @var value
  1359. * @params: the hw_params instance
  1360. * @var: parameter to retrieve
  1361. * @dir: pointer to the direction (-1,0,1) or %NULL
  1362. *
  1363. * Return the value for field @var if it's fixed in configuration space
  1364. * defined by @params. Return -%EINVAL otherwise.
  1365. */
  1366. int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
  1367. snd_pcm_hw_param_t var, int *dir)
  1368. {
  1369. if (hw_is_mask(var)) {
  1370. const struct snd_mask *mask = hw_param_mask_c(params, var);
  1371. if (!snd_mask_single(mask))
  1372. return -EINVAL;
  1373. if (dir)
  1374. *dir = 0;
  1375. return snd_mask_value(mask);
  1376. }
  1377. if (hw_is_interval(var)) {
  1378. const struct snd_interval *i = hw_param_interval_c(params, var);
  1379. if (!snd_interval_single(i))
  1380. return -EINVAL;
  1381. if (dir)
  1382. *dir = i->openmin;
  1383. return snd_interval_value(i);
  1384. }
  1385. return -EINVAL;
  1386. }
  1387. EXPORT_SYMBOL(snd_pcm_hw_param_value);
  1388. void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
  1389. snd_pcm_hw_param_t var)
  1390. {
  1391. if (hw_is_mask(var)) {
  1392. snd_mask_none(hw_param_mask(params, var));
  1393. params->cmask |= 1 << var;
  1394. params->rmask |= 1 << var;
  1395. } else if (hw_is_interval(var)) {
  1396. snd_interval_none(hw_param_interval(params, var));
  1397. params->cmask |= 1 << var;
  1398. params->rmask |= 1 << var;
  1399. } else {
  1400. snd_BUG();
  1401. }
  1402. }
  1403. EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
  1404. static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
  1405. snd_pcm_hw_param_t var)
  1406. {
  1407. int changed;
  1408. if (hw_is_mask(var))
  1409. changed = snd_mask_refine_first(hw_param_mask(params, var));
  1410. else if (hw_is_interval(var))
  1411. changed = snd_interval_refine_first(hw_param_interval(params, var));
  1412. else
  1413. return -EINVAL;
  1414. if (changed) {
  1415. params->cmask |= 1 << var;
  1416. params->rmask |= 1 << var;
  1417. }
  1418. return changed;
  1419. }
  1420. /**
  1421. * snd_pcm_hw_param_first - refine config space and return minimum value
  1422. * @pcm: PCM instance
  1423. * @params: the hw_params instance
  1424. * @var: parameter to retrieve
  1425. * @dir: pointer to the direction (-1,0,1) or %NULL
  1426. *
  1427. * Inside configuration space defined by @params remove from @var all
  1428. * values > minimum. Reduce configuration space accordingly.
  1429. * Return the minimum.
  1430. */
  1431. int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
  1432. struct snd_pcm_hw_params *params,
  1433. snd_pcm_hw_param_t var, int *dir)
  1434. {
  1435. int changed = _snd_pcm_hw_param_first(params, var);
  1436. if (changed < 0)
  1437. return changed;
  1438. if (params->rmask) {
  1439. int err = snd_pcm_hw_refine(pcm, params);
  1440. if (snd_BUG_ON(err < 0))
  1441. return err;
  1442. }
  1443. return snd_pcm_hw_param_value(params, var, dir);
  1444. }
  1445. EXPORT_SYMBOL(snd_pcm_hw_param_first);
  1446. static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
  1447. snd_pcm_hw_param_t var)
  1448. {
  1449. int changed;
  1450. if (hw_is_mask(var))
  1451. changed = snd_mask_refine_last(hw_param_mask(params, var));
  1452. else if (hw_is_interval(var))
  1453. changed = snd_interval_refine_last(hw_param_interval(params, var));
  1454. else
  1455. return -EINVAL;
  1456. if (changed) {
  1457. params->cmask |= 1 << var;
  1458. params->rmask |= 1 << var;
  1459. }
  1460. return changed;
  1461. }
  1462. /**
  1463. * snd_pcm_hw_param_last - refine config space and return maximum value
  1464. * @pcm: PCM instance
  1465. * @params: the hw_params instance
  1466. * @var: parameter to retrieve
  1467. * @dir: pointer to the direction (-1,0,1) or %NULL
  1468. *
  1469. * Inside configuration space defined by @params remove from @var all
  1470. * values < maximum. Reduce configuration space accordingly.
  1471. * Return the maximum.
  1472. */
  1473. int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
  1474. struct snd_pcm_hw_params *params,
  1475. snd_pcm_hw_param_t var, int *dir)
  1476. {
  1477. int changed = _snd_pcm_hw_param_last(params, var);
  1478. if (changed < 0)
  1479. return changed;
  1480. if (params->rmask) {
  1481. int err = snd_pcm_hw_refine(pcm, params);
  1482. if (snd_BUG_ON(err < 0))
  1483. return err;
  1484. }
  1485. return snd_pcm_hw_param_value(params, var, dir);
  1486. }
  1487. EXPORT_SYMBOL(snd_pcm_hw_param_last);
  1488. /**
  1489. * snd_pcm_hw_param_choose - choose a configuration defined by @params
  1490. * @pcm: PCM instance
  1491. * @params: the hw_params instance
  1492. *
  1493. * Choose one configuration from configuration space defined by @params.
  1494. * The configuration chosen is that obtained fixing in this order:
  1495. * first access, first format, first subformat, min channels,
  1496. * min rate, min period time, max buffer size, min tick time
  1497. */
  1498. int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
  1499. struct snd_pcm_hw_params *params)
  1500. {
  1501. static int vars[] = {
  1502. SNDRV_PCM_HW_PARAM_ACCESS,
  1503. SNDRV_PCM_HW_PARAM_FORMAT,
  1504. SNDRV_PCM_HW_PARAM_SUBFORMAT,
  1505. SNDRV_PCM_HW_PARAM_CHANNELS,
  1506. SNDRV_PCM_HW_PARAM_RATE,
  1507. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1508. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1509. SNDRV_PCM_HW_PARAM_TICK_TIME,
  1510. -1
  1511. };
  1512. int err, *v;
  1513. for (v = vars; *v != -1; v++) {
  1514. if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
  1515. err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
  1516. else
  1517. err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
  1518. if (snd_BUG_ON(err < 0))
  1519. return err;
  1520. }
  1521. return 0;
  1522. }
  1523. static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
  1524. void *arg)
  1525. {
  1526. struct snd_pcm_runtime *runtime = substream->runtime;
  1527. unsigned long flags;
  1528. snd_pcm_stream_lock_irqsave(substream, flags);
  1529. if (snd_pcm_running(substream) &&
  1530. snd_pcm_update_hw_ptr(substream) >= 0)
  1531. runtime->status->hw_ptr %= runtime->buffer_size;
  1532. else
  1533. runtime->status->hw_ptr = 0;
  1534. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1535. return 0;
  1536. }
  1537. static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
  1538. void *arg)
  1539. {
  1540. struct snd_pcm_channel_info *info = arg;
  1541. struct snd_pcm_runtime *runtime = substream->runtime;
  1542. int width;
  1543. if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
  1544. info->offset = -1;
  1545. return 0;
  1546. }
  1547. width = snd_pcm_format_physical_width(runtime->format);
  1548. if (width < 0)
  1549. return width;
  1550. info->offset = 0;
  1551. switch (runtime->access) {
  1552. case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
  1553. case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
  1554. info->first = info->channel * width;
  1555. info->step = runtime->channels * width;
  1556. break;
  1557. case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
  1558. case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
  1559. {
  1560. size_t size = runtime->dma_bytes / runtime->channels;
  1561. info->first = info->channel * size * 8;
  1562. info->step = width;
  1563. break;
  1564. }
  1565. default:
  1566. snd_BUG();
  1567. break;
  1568. }
  1569. return 0;
  1570. }
  1571. static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
  1572. void *arg)
  1573. {
  1574. struct snd_pcm_hw_params *params = arg;
  1575. snd_pcm_format_t format;
  1576. int channels, width;
  1577. params->fifo_size = substream->runtime->hw.fifo_size;
  1578. if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
  1579. format = params_format(params);
  1580. channels = params_channels(params);
  1581. width = snd_pcm_format_physical_width(format);
  1582. params->fifo_size /= width * channels;
  1583. }
  1584. return 0;
  1585. }
  1586. /**
  1587. * snd_pcm_lib_ioctl - a generic PCM ioctl callback
  1588. * @substream: the pcm substream instance
  1589. * @cmd: ioctl command
  1590. * @arg: ioctl argument
  1591. *
  1592. * Processes the generic ioctl commands for PCM.
  1593. * Can be passed as the ioctl callback for PCM ops.
  1594. *
  1595. * Returns zero if successful, or a negative error code on failure.
  1596. */
  1597. int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  1598. unsigned int cmd, void *arg)
  1599. {
  1600. switch (cmd) {
  1601. case SNDRV_PCM_IOCTL1_INFO:
  1602. return 0;
  1603. case SNDRV_PCM_IOCTL1_RESET:
  1604. return snd_pcm_lib_ioctl_reset(substream, arg);
  1605. case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
  1606. return snd_pcm_lib_ioctl_channel_info(substream, arg);
  1607. case SNDRV_PCM_IOCTL1_FIFO_SIZE:
  1608. return snd_pcm_lib_ioctl_fifo_size(substream, arg);
  1609. }
  1610. return -ENXIO;
  1611. }
  1612. EXPORT_SYMBOL(snd_pcm_lib_ioctl);
  1613. /**
  1614. * snd_pcm_period_elapsed - update the pcm status for the next period
  1615. * @substream: the pcm substream instance
  1616. *
  1617. * This function is called from the interrupt handler when the
  1618. * PCM has processed the period size. It will update the current
  1619. * pointer, wake up sleepers, etc.
  1620. *
  1621. * Even if more than one periods have elapsed since the last call, you
  1622. * have to call this only once.
  1623. */
  1624. void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
  1625. {
  1626. struct snd_pcm_runtime *runtime;
  1627. unsigned long flags;
  1628. if (PCM_RUNTIME_CHECK(substream))
  1629. return;
  1630. runtime = substream->runtime;
  1631. if (runtime->transfer_ack_begin)
  1632. runtime->transfer_ack_begin(substream);
  1633. snd_pcm_stream_lock_irqsave(substream, flags);
  1634. if (!snd_pcm_running(substream) ||
  1635. snd_pcm_update_hw_ptr0(substream, 1) < 0)
  1636. goto _end;
  1637. if (substream->timer_running)
  1638. snd_timer_interrupt(substream->timer, 1);
  1639. _end:
  1640. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1641. if (runtime->transfer_ack_end)
  1642. runtime->transfer_ack_end(substream);
  1643. kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
  1644. }
  1645. EXPORT_SYMBOL(snd_pcm_period_elapsed);
  1646. /*
  1647. * Wait until avail_min data becomes available
  1648. * Returns a negative error code if any error occurs during operation.
  1649. * The available space is stored on availp. When err = 0 and avail = 0
  1650. * on the capture stream, it indicates the stream is in DRAINING state.
  1651. */
  1652. static int wait_for_avail(struct snd_pcm_substream *substream,
  1653. snd_pcm_uframes_t *availp)
  1654. {
  1655. struct snd_pcm_runtime *runtime = substream->runtime;
  1656. int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  1657. wait_queue_t wait;
  1658. int err = 0;
  1659. snd_pcm_uframes_t avail = 0;
  1660. long wait_time, tout;
  1661. init_waitqueue_entry(&wait, current);
  1662. set_current_state(TASK_INTERRUPTIBLE);
  1663. add_wait_queue(&runtime->tsleep, &wait);
  1664. if (runtime->no_period_wakeup)
  1665. wait_time = MAX_SCHEDULE_TIMEOUT;
  1666. else {
  1667. wait_time = 10;
  1668. if (runtime->rate) {
  1669. long t = runtime->period_size * 2 / runtime->rate;
  1670. wait_time = max(t, wait_time);
  1671. }
  1672. wait_time = msecs_to_jiffies(wait_time * 1000);
  1673. }
  1674. for (;;) {
  1675. if (signal_pending(current)) {
  1676. err = -ERESTARTSYS;
  1677. break;
  1678. }
  1679. /*
  1680. * We need to check if space became available already
  1681. * (and thus the wakeup happened already) first to close
  1682. * the race of space already having become available.
  1683. * This check must happen after been added to the waitqueue
  1684. * and having current state be INTERRUPTIBLE.
  1685. */
  1686. if (is_playback)
  1687. avail = snd_pcm_playback_avail(runtime);
  1688. else
  1689. avail = snd_pcm_capture_avail(runtime);
  1690. if (avail >= runtime->twake)
  1691. break;
  1692. snd_pcm_stream_unlock_irq(substream);
  1693. tout = schedule_timeout(wait_time);
  1694. snd_pcm_stream_lock_irq(substream);
  1695. set_current_state(TASK_INTERRUPTIBLE);
  1696. switch (runtime->status->state) {
  1697. case SNDRV_PCM_STATE_SUSPENDED:
  1698. err = -ESTRPIPE;
  1699. goto _endloop;
  1700. case SNDRV_PCM_STATE_XRUN:
  1701. err = -EPIPE;
  1702. goto _endloop;
  1703. case SNDRV_PCM_STATE_DRAINING:
  1704. if (is_playback)
  1705. err = -EPIPE;
  1706. else
  1707. avail = 0; /* indicate draining */
  1708. goto _endloop;
  1709. case SNDRV_PCM_STATE_OPEN:
  1710. case SNDRV_PCM_STATE_SETUP:
  1711. case SNDRV_PCM_STATE_DISCONNECTED:
  1712. err = -EBADFD;
  1713. goto _endloop;
  1714. }
  1715. if (!tout) {
  1716. snd_printd("%s write error (DMA or IRQ trouble?)\n",
  1717. is_playback ? "playback" : "capture");
  1718. err = -EIO;
  1719. break;
  1720. }
  1721. }
  1722. _endloop:
  1723. set_current_state(TASK_RUNNING);
  1724. remove_wait_queue(&runtime->tsleep, &wait);
  1725. *availp = avail;
  1726. return err;
  1727. }
  1728. static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
  1729. unsigned int hwoff,
  1730. unsigned long data, unsigned int off,
  1731. snd_pcm_uframes_t frames)
  1732. {
  1733. struct snd_pcm_runtime *runtime = substream->runtime;
  1734. int err;
  1735. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  1736. if (substream->ops->copy) {
  1737. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  1738. return err;
  1739. } else {
  1740. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  1741. if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
  1742. return -EFAULT;
  1743. }
  1744. return 0;
  1745. }
  1746. typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
  1747. unsigned long data, unsigned int off,
  1748. snd_pcm_uframes_t size);
  1749. static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
  1750. unsigned long data,
  1751. snd_pcm_uframes_t size,
  1752. int nonblock,
  1753. transfer_f transfer)
  1754. {
  1755. struct snd_pcm_runtime *runtime = substream->runtime;
  1756. snd_pcm_uframes_t xfer = 0;
  1757. snd_pcm_uframes_t offset = 0;
  1758. snd_pcm_uframes_t avail;
  1759. int err = 0;
  1760. if (size == 0)
  1761. return 0;
  1762. snd_pcm_stream_lock_irq(substream);
  1763. switch (runtime->status->state) {
  1764. case SNDRV_PCM_STATE_PREPARED:
  1765. case SNDRV_PCM_STATE_RUNNING:
  1766. case SNDRV_PCM_STATE_PAUSED:
  1767. break;
  1768. case SNDRV_PCM_STATE_XRUN:
  1769. err = -EPIPE;
  1770. goto _end_unlock;
  1771. case SNDRV_PCM_STATE_SUSPENDED:
  1772. err = -ESTRPIPE;
  1773. goto _end_unlock;
  1774. default:
  1775. err = -EBADFD;
  1776. goto _end_unlock;
  1777. }
  1778. runtime->twake = runtime->control->avail_min ? : 1;
  1779. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1780. snd_pcm_update_hw_ptr(substream);
  1781. avail = snd_pcm_playback_avail(runtime);
  1782. while (size > 0) {
  1783. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  1784. snd_pcm_uframes_t cont;
  1785. if (!avail) {
  1786. if (nonblock) {
  1787. err = -EAGAIN;
  1788. goto _end_unlock;
  1789. }
  1790. runtime->twake = min_t(snd_pcm_uframes_t, size,
  1791. runtime->control->avail_min ? : 1);
  1792. err = wait_for_avail(substream, &avail);
  1793. if (err < 0)
  1794. goto _end_unlock;
  1795. }
  1796. frames = size > avail ? avail : size;
  1797. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  1798. if (frames > cont)
  1799. frames = cont;
  1800. if (snd_BUG_ON(!frames)) {
  1801. runtime->twake = 0;
  1802. snd_pcm_stream_unlock_irq(substream);
  1803. return -EINVAL;
  1804. }
  1805. appl_ptr = runtime->control->appl_ptr;
  1806. appl_ofs = appl_ptr % runtime->buffer_size;
  1807. snd_pcm_stream_unlock_irq(substream);
  1808. err = transfer(substream, appl_ofs, data, offset, frames);
  1809. snd_pcm_stream_lock_irq(substream);
  1810. if (err < 0)
  1811. goto _end_unlock;
  1812. switch (runtime->status->state) {
  1813. case SNDRV_PCM_STATE_XRUN:
  1814. err = -EPIPE;
  1815. goto _end_unlock;
  1816. case SNDRV_PCM_STATE_SUSPENDED:
  1817. err = -ESTRPIPE;
  1818. goto _end_unlock;
  1819. default:
  1820. break;
  1821. }
  1822. appl_ptr += frames;
  1823. if (appl_ptr >= runtime->boundary)
  1824. appl_ptr -= runtime->boundary;
  1825. runtime->control->appl_ptr = appl_ptr;
  1826. if (substream->ops->ack)
  1827. substream->ops->ack(substream);
  1828. offset += frames;
  1829. size -= frames;
  1830. xfer += frames;
  1831. avail -= frames;
  1832. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
  1833. snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
  1834. err = snd_pcm_start(substream);
  1835. if (err < 0)
  1836. goto _end_unlock;
  1837. }
  1838. }
  1839. _end_unlock:
  1840. runtime->twake = 0;
  1841. if (xfer > 0 && err >= 0)
  1842. snd_pcm_update_state(substream, runtime);
  1843. snd_pcm_stream_unlock_irq(substream);
  1844. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  1845. }
  1846. /* sanity-check for read/write methods */
  1847. static int pcm_sanity_check(struct snd_pcm_substream *substream)
  1848. {
  1849. struct snd_pcm_runtime *runtime;
  1850. if (PCM_RUNTIME_CHECK(substream))
  1851. return -ENXIO;
  1852. runtime = substream->runtime;
  1853. if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
  1854. return -EINVAL;
  1855. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1856. return -EBADFD;
  1857. return 0;
  1858. }
  1859. snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
  1860. {
  1861. struct snd_pcm_runtime *runtime;
  1862. int nonblock;
  1863. int err;
  1864. err = pcm_sanity_check(substream);
  1865. if (err < 0)
  1866. return err;
  1867. runtime = substream->runtime;
  1868. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1869. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
  1870. runtime->channels > 1)
  1871. return -EINVAL;
  1872. return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
  1873. snd_pcm_lib_write_transfer);
  1874. }
  1875. EXPORT_SYMBOL(snd_pcm_lib_write);
  1876. static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
  1877. unsigned int hwoff,
  1878. unsigned long data, unsigned int off,
  1879. snd_pcm_uframes_t frames)
  1880. {
  1881. struct snd_pcm_runtime *runtime = substream->runtime;
  1882. int err;
  1883. void __user **bufs = (void __user **)data;
  1884. int channels = runtime->channels;
  1885. int c;
  1886. if (substream->ops->copy) {
  1887. if (snd_BUG_ON(!substream->ops->silence))
  1888. return -EINVAL;
  1889. for (c = 0; c < channels; ++c, ++bufs) {
  1890. if (*bufs == NULL) {
  1891. if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
  1892. return err;
  1893. } else {
  1894. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  1895. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  1896. return err;
  1897. }
  1898. }
  1899. } else {
  1900. /* default transfer behaviour */
  1901. size_t dma_csize = runtime->dma_bytes / channels;
  1902. for (c = 0; c < channels; ++c, ++bufs) {
  1903. char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  1904. if (*bufs == NULL) {
  1905. snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
  1906. } else {
  1907. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  1908. if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
  1909. return -EFAULT;
  1910. }
  1911. }
  1912. }
  1913. return 0;
  1914. }
  1915. snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
  1916. void __user **bufs,
  1917. snd_pcm_uframes_t frames)
  1918. {
  1919. struct snd_pcm_runtime *runtime;
  1920. int nonblock;
  1921. int err;
  1922. err = pcm_sanity_check(substream);
  1923. if (err < 0)
  1924. return err;
  1925. runtime = substream->runtime;
  1926. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1927. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  1928. return -EINVAL;
  1929. return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
  1930. nonblock, snd_pcm_lib_writev_transfer);
  1931. }
  1932. EXPORT_SYMBOL(snd_pcm_lib_writev);
  1933. static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
  1934. unsigned int hwoff,
  1935. unsigned long data, unsigned int off,
  1936. snd_pcm_uframes_t frames)
  1937. {
  1938. struct snd_pcm_runtime *runtime = substream->runtime;
  1939. int err;
  1940. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  1941. if (substream->ops->copy) {
  1942. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  1943. return err;
  1944. } else {
  1945. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  1946. if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
  1947. return -EFAULT;
  1948. }
  1949. return 0;
  1950. }
  1951. static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
  1952. unsigned long data,
  1953. snd_pcm_uframes_t size,
  1954. int nonblock,
  1955. transfer_f transfer)
  1956. {
  1957. struct snd_pcm_runtime *runtime = substream->runtime;
  1958. snd_pcm_uframes_t xfer = 0;
  1959. snd_pcm_uframes_t offset = 0;
  1960. snd_pcm_uframes_t avail;
  1961. int err = 0;
  1962. if (size == 0)
  1963. return 0;
  1964. snd_pcm_stream_lock_irq(substream);
  1965. switch (runtime->status->state) {
  1966. case SNDRV_PCM_STATE_PREPARED:
  1967. if (size >= runtime->start_threshold) {
  1968. err = snd_pcm_start(substream);
  1969. if (err < 0)
  1970. goto _end_unlock;
  1971. }
  1972. break;
  1973. case SNDRV_PCM_STATE_DRAINING:
  1974. case SNDRV_PCM_STATE_RUNNING:
  1975. case SNDRV_PCM_STATE_PAUSED:
  1976. break;
  1977. case SNDRV_PCM_STATE_XRUN:
  1978. err = -EPIPE;
  1979. goto _end_unlock;
  1980. case SNDRV_PCM_STATE_SUSPENDED:
  1981. err = -ESTRPIPE;
  1982. goto _end_unlock;
  1983. default:
  1984. err = -EBADFD;
  1985. goto _end_unlock;
  1986. }
  1987. runtime->twake = runtime->control->avail_min ? : 1;
  1988. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1989. snd_pcm_update_hw_ptr(substream);
  1990. avail = snd_pcm_capture_avail(runtime);
  1991. while (size > 0) {
  1992. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  1993. snd_pcm_uframes_t cont;
  1994. if (!avail) {
  1995. if (runtime->status->state ==
  1996. SNDRV_PCM_STATE_DRAINING) {
  1997. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1998. goto _end_unlock;
  1999. }
  2000. if (nonblock) {
  2001. err = -EAGAIN;
  2002. goto _end_unlock;
  2003. }
  2004. runtime->twake = min_t(snd_pcm_uframes_t, size,
  2005. runtime->control->avail_min ? : 1);
  2006. err = wait_for_avail(substream, &avail);
  2007. if (err < 0)
  2008. goto _end_unlock;
  2009. if (!avail)
  2010. continue; /* draining */
  2011. }
  2012. frames = size > avail ? avail : size;
  2013. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  2014. if (frames > cont)
  2015. frames = cont;
  2016. if (snd_BUG_ON(!frames)) {
  2017. runtime->twake = 0;
  2018. snd_pcm_stream_unlock_irq(substream);
  2019. return -EINVAL;
  2020. }
  2021. appl_ptr = runtime->control->appl_ptr;
  2022. appl_ofs = appl_ptr % runtime->buffer_size;
  2023. snd_pcm_stream_unlock_irq(substream);
  2024. err = transfer(substream, appl_ofs, data, offset, frames);
  2025. snd_pcm_stream_lock_irq(substream);
  2026. if (err < 0)
  2027. goto _end_unlock;
  2028. switch (runtime->status->state) {
  2029. case SNDRV_PCM_STATE_XRUN:
  2030. err = -EPIPE;
  2031. goto _end_unlock;
  2032. case SNDRV_PCM_STATE_SUSPENDED:
  2033. err = -ESTRPIPE;
  2034. goto _end_unlock;
  2035. default:
  2036. break;
  2037. }
  2038. appl_ptr += frames;
  2039. if (appl_ptr >= runtime->boundary)
  2040. appl_ptr -= runtime->boundary;
  2041. runtime->control->appl_ptr = appl_ptr;
  2042. if (substream->ops->ack)
  2043. substream->ops->ack(substream);
  2044. offset += frames;
  2045. size -= frames;
  2046. xfer += frames;
  2047. avail -= frames;
  2048. }
  2049. _end_unlock:
  2050. runtime->twake = 0;
  2051. if (xfer > 0 && err >= 0)
  2052. snd_pcm_update_state(substream, runtime);
  2053. snd_pcm_stream_unlock_irq(substream);
  2054. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  2055. }
  2056. snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
  2057. {
  2058. struct snd_pcm_runtime *runtime;
  2059. int nonblock;
  2060. int err;
  2061. err = pcm_sanity_check(substream);
  2062. if (err < 0)
  2063. return err;
  2064. runtime = substream->runtime;
  2065. nonblock = !!(substream->f_flags & O_NONBLOCK);
  2066. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
  2067. return -EINVAL;
  2068. return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
  2069. }
  2070. EXPORT_SYMBOL(snd_pcm_lib_read);
  2071. static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
  2072. unsigned int hwoff,
  2073. unsigned long data, unsigned int off,
  2074. snd_pcm_uframes_t frames)
  2075. {
  2076. struct snd_pcm_runtime *runtime = substream->runtime;
  2077. int err;
  2078. void __user **bufs = (void __user **)data;
  2079. int channels = runtime->channels;
  2080. int c;
  2081. if (substream->ops->copy) {
  2082. for (c = 0; c < channels; ++c, ++bufs) {
  2083. char __user *buf;
  2084. if (*bufs == NULL)
  2085. continue;
  2086. buf = *bufs + samples_to_bytes(runtime, off);
  2087. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  2088. return err;
  2089. }
  2090. } else {
  2091. snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
  2092. for (c = 0; c < channels; ++c, ++bufs) {
  2093. char *hwbuf;
  2094. char __user *buf;
  2095. if (*bufs == NULL)
  2096. continue;
  2097. hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  2098. buf = *bufs + samples_to_bytes(runtime, off);
  2099. if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
  2100. return -EFAULT;
  2101. }
  2102. }
  2103. return 0;
  2104. }
  2105. snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
  2106. void __user **bufs,
  2107. snd_pcm_uframes_t frames)
  2108. {
  2109. struct snd_pcm_runtime *runtime;
  2110. int nonblock;
  2111. int err;
  2112. err = pcm_sanity_check(substream);
  2113. if (err < 0)
  2114. return err;
  2115. runtime = substream->runtime;
  2116. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2117. return -EBADFD;
  2118. nonblock = !!(substream->f_flags & O_NONBLOCK);
  2119. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  2120. return -EINVAL;
  2121. return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
  2122. }
  2123. EXPORT_SYMBOL(snd_pcm_lib_readv);