pcm_lib.c 59 KB

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