pcm_lib.c 67 KB

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