pcm_lib.c 58 KB

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