pcm_lib.c 67 KB

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