pcm_lib.c 68 KB

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