pcm_lib.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489
  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. if (runtime->silence_start != runtime->control->appl_ptr) {
  63. n = runtime->control->appl_ptr - runtime->silence_start;
  64. if (n < 0)
  65. n += runtime->boundary;
  66. if ((snd_pcm_uframes_t)n < runtime->silence_filled)
  67. runtime->silence_filled -= n;
  68. else
  69. runtime->silence_filled = 0;
  70. runtime->silence_start = runtime->control->appl_ptr;
  71. }
  72. if (runtime->silence_filled >= runtime->buffer_size)
  73. return;
  74. noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
  75. if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
  76. return;
  77. frames = runtime->silence_threshold - noise_dist;
  78. if (frames > runtime->silence_size)
  79. frames = runtime->silence_size;
  80. } else {
  81. if (new_hw_ptr == ULONG_MAX) { /* initialization */
  82. snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
  83. if (avail > runtime->buffer_size)
  84. avail = runtime->buffer_size;
  85. runtime->silence_filled = avail > 0 ? avail : 0;
  86. runtime->silence_start = (runtime->status->hw_ptr +
  87. runtime->silence_filled) %
  88. runtime->boundary;
  89. } else {
  90. ofs = runtime->status->hw_ptr;
  91. frames = new_hw_ptr - ofs;
  92. if ((snd_pcm_sframes_t)frames < 0)
  93. frames += runtime->boundary;
  94. runtime->silence_filled -= frames;
  95. if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
  96. runtime->silence_filled = 0;
  97. runtime->silence_start = new_hw_ptr;
  98. } else {
  99. runtime->silence_start = ofs;
  100. }
  101. }
  102. frames = runtime->buffer_size - runtime->silence_filled;
  103. }
  104. if (snd_BUG_ON(frames > runtime->buffer_size))
  105. return;
  106. if (frames == 0)
  107. return;
  108. ofs = runtime->silence_start % runtime->buffer_size;
  109. while (frames > 0) {
  110. transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
  111. err = fill_silence_frames(substream, ofs, transfer);
  112. snd_BUG_ON(err < 0);
  113. runtime->silence_filled += transfer;
  114. frames -= transfer;
  115. ofs = 0;
  116. }
  117. }
  118. #ifdef CONFIG_SND_DEBUG
  119. void snd_pcm_debug_name(struct snd_pcm_substream *substream,
  120. char *name, size_t len)
  121. {
  122. snprintf(name, len, "pcmC%dD%d%c:%d",
  123. substream->pcm->card->number,
  124. substream->pcm->device,
  125. substream->stream ? 'c' : 'p',
  126. substream->number);
  127. }
  128. EXPORT_SYMBOL(snd_pcm_debug_name);
  129. #endif
  130. #define XRUN_DEBUG_BASIC (1<<0)
  131. #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
  132. #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
  133. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  134. #define xrun_debug(substream, mask) \
  135. ((substream)->pstr->xrun_debug & (mask))
  136. #else
  137. #define xrun_debug(substream, mask) 0
  138. #endif
  139. #define dump_stack_on_xrun(substream) do { \
  140. if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
  141. dump_stack(); \
  142. } while (0)
  143. static void xrun(struct snd_pcm_substream *substream)
  144. {
  145. struct snd_pcm_runtime *runtime = substream->runtime;
  146. trace_xrun(substream);
  147. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
  148. snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
  149. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  150. if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
  151. char name[16];
  152. snd_pcm_debug_name(substream, name, sizeof(name));
  153. pcm_warn(substream->pcm, "XRUN: %s\n", name);
  154. dump_stack_on_xrun(substream);
  155. }
  156. }
  157. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  158. #define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \
  159. do { \
  160. trace_hw_ptr_error(substream, reason); \
  161. if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
  162. pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \
  163. (in_interrupt) ? 'Q' : 'P', ##args); \
  164. dump_stack_on_xrun(substream); \
  165. } \
  166. } while (0)
  167. #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
  168. #define hw_ptr_error(substream, fmt, args...) do { } while (0)
  169. #endif
  170. int snd_pcm_update_state(struct snd_pcm_substream *substream,
  171. struct snd_pcm_runtime *runtime)
  172. {
  173. snd_pcm_uframes_t avail;
  174. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  175. avail = snd_pcm_playback_avail(runtime);
  176. else
  177. avail = snd_pcm_capture_avail(runtime);
  178. if (avail > runtime->avail_max)
  179. runtime->avail_max = avail;
  180. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  181. if (avail >= runtime->buffer_size) {
  182. snd_pcm_drain_done(substream);
  183. return -EPIPE;
  184. }
  185. } else {
  186. if (avail >= runtime->stop_threshold) {
  187. xrun(substream);
  188. return -EPIPE;
  189. }
  190. }
  191. if (runtime->twake) {
  192. if (avail >= runtime->twake)
  193. wake_up(&runtime->tsleep);
  194. } else if (avail >= runtime->control->avail_min)
  195. wake_up(&runtime->sleep);
  196. return 0;
  197. }
  198. static void update_audio_tstamp(struct snd_pcm_substream *substream,
  199. struct timespec *curr_tstamp,
  200. struct timespec *audio_tstamp)
  201. {
  202. struct snd_pcm_runtime *runtime = substream->runtime;
  203. u64 audio_frames, audio_nsecs;
  204. struct timespec driver_tstamp;
  205. if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE)
  206. return;
  207. if (!(substream->ops->get_time_info) ||
  208. (runtime->audio_tstamp_report.actual_type ==
  209. SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
  210. /*
  211. * provide audio timestamp derived from pointer position
  212. * add delay only if requested
  213. */
  214. audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr;
  215. if (runtime->audio_tstamp_config.report_delay) {
  216. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  217. audio_frames -= runtime->delay;
  218. else
  219. audio_frames += runtime->delay;
  220. }
  221. audio_nsecs = div_u64(audio_frames * 1000000000LL,
  222. runtime->rate);
  223. *audio_tstamp = ns_to_timespec(audio_nsecs);
  224. }
  225. runtime->status->audio_tstamp = *audio_tstamp;
  226. runtime->status->tstamp = *curr_tstamp;
  227. /*
  228. * re-take a driver timestamp to let apps detect if the reference tstamp
  229. * read by low-level hardware was provided with a delay
  230. */
  231. snd_pcm_gettime(substream->runtime, (struct timespec *)&driver_tstamp);
  232. runtime->driver_tstamp = driver_tstamp;
  233. }
  234. static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
  235. unsigned int in_interrupt)
  236. {
  237. struct snd_pcm_runtime *runtime = substream->runtime;
  238. snd_pcm_uframes_t pos;
  239. snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
  240. snd_pcm_sframes_t hdelta, delta;
  241. unsigned long jdelta;
  242. unsigned long curr_jiffies;
  243. struct timespec curr_tstamp;
  244. struct timespec audio_tstamp;
  245. int crossed_boundary = 0;
  246. old_hw_ptr = runtime->status->hw_ptr;
  247. /*
  248. * group pointer, time and jiffies reads to allow for more
  249. * accurate correlations/corrections.
  250. * The values are stored at the end of this routine after
  251. * corrections for hw_ptr position
  252. */
  253. pos = substream->ops->pointer(substream);
  254. curr_jiffies = jiffies;
  255. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
  256. if ((substream->ops->get_time_info) &&
  257. (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
  258. substream->ops->get_time_info(substream, &curr_tstamp,
  259. &audio_tstamp,
  260. &runtime->audio_tstamp_config,
  261. &runtime->audio_tstamp_report);
  262. /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
  263. if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
  264. snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
  265. } else
  266. snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
  267. }
  268. if (pos == SNDRV_PCM_POS_XRUN) {
  269. xrun(substream);
  270. return -EPIPE;
  271. }
  272. if (pos >= runtime->buffer_size) {
  273. if (printk_ratelimit()) {
  274. char name[16];
  275. snd_pcm_debug_name(substream, name, sizeof(name));
  276. pcm_err(substream->pcm,
  277. "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
  278. name, pos, runtime->buffer_size,
  279. runtime->period_size);
  280. }
  281. pos = 0;
  282. }
  283. pos -= pos % runtime->min_align;
  284. trace_hwptr(substream, pos, in_interrupt);
  285. hw_base = runtime->hw_ptr_base;
  286. new_hw_ptr = hw_base + pos;
  287. if (in_interrupt) {
  288. /* we know that one period was processed */
  289. /* delta = "expected next hw_ptr" for in_interrupt != 0 */
  290. delta = runtime->hw_ptr_interrupt + runtime->period_size;
  291. if (delta > new_hw_ptr) {
  292. /* check for double acknowledged interrupts */
  293. hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
  294. if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
  295. hw_base += runtime->buffer_size;
  296. if (hw_base >= runtime->boundary) {
  297. hw_base = 0;
  298. crossed_boundary++;
  299. }
  300. new_hw_ptr = hw_base + pos;
  301. goto __delta;
  302. }
  303. }
  304. }
  305. /* new_hw_ptr might be lower than old_hw_ptr in case when */
  306. /* pointer crosses the end of the ring buffer */
  307. if (new_hw_ptr < old_hw_ptr) {
  308. hw_base += runtime->buffer_size;
  309. if (hw_base >= runtime->boundary) {
  310. hw_base = 0;
  311. crossed_boundary++;
  312. }
  313. new_hw_ptr = hw_base + pos;
  314. }
  315. __delta:
  316. delta = new_hw_ptr - old_hw_ptr;
  317. if (delta < 0)
  318. delta += runtime->boundary;
  319. if (runtime->no_period_wakeup) {
  320. snd_pcm_sframes_t xrun_threshold;
  321. /*
  322. * Without regular period interrupts, we have to check
  323. * the elapsed time to detect xruns.
  324. */
  325. jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
  326. if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
  327. goto no_delta_check;
  328. hdelta = jdelta - delta * HZ / runtime->rate;
  329. xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
  330. while (hdelta > xrun_threshold) {
  331. delta += runtime->buffer_size;
  332. hw_base += runtime->buffer_size;
  333. if (hw_base >= runtime->boundary) {
  334. hw_base = 0;
  335. crossed_boundary++;
  336. }
  337. new_hw_ptr = hw_base + pos;
  338. hdelta -= runtime->hw_ptr_buffer_jiffies;
  339. }
  340. goto no_delta_check;
  341. }
  342. /* something must be really wrong */
  343. if (delta >= runtime->buffer_size + runtime->period_size) {
  344. hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
  345. "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
  346. substream->stream, (long)pos,
  347. (long)new_hw_ptr, (long)old_hw_ptr);
  348. return 0;
  349. }
  350. /* Do jiffies check only in xrun_debug mode */
  351. if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
  352. goto no_jiffies_check;
  353. /* Skip the jiffies check for hardwares with BATCH flag.
  354. * Such hardware usually just increases the position at each IRQ,
  355. * thus it can't give any strange position.
  356. */
  357. if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
  358. goto no_jiffies_check;
  359. hdelta = delta;
  360. if (hdelta < runtime->delay)
  361. goto no_jiffies_check;
  362. hdelta -= runtime->delay;
  363. jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
  364. if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
  365. delta = jdelta /
  366. (((runtime->period_size * HZ) / runtime->rate)
  367. + HZ/100);
  368. /* move new_hw_ptr according jiffies not pos variable */
  369. new_hw_ptr = old_hw_ptr;
  370. hw_base = delta;
  371. /* use loop to avoid checks for delta overflows */
  372. /* the delta value is small or zero in most cases */
  373. while (delta > 0) {
  374. new_hw_ptr += runtime->period_size;
  375. if (new_hw_ptr >= runtime->boundary) {
  376. new_hw_ptr -= runtime->boundary;
  377. crossed_boundary--;
  378. }
  379. delta--;
  380. }
  381. /* align hw_base to buffer_size */
  382. hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
  383. "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
  384. (long)pos, (long)hdelta,
  385. (long)runtime->period_size, jdelta,
  386. ((hdelta * HZ) / runtime->rate), hw_base,
  387. (unsigned long)old_hw_ptr,
  388. (unsigned long)new_hw_ptr);
  389. /* reset values to proper state */
  390. delta = 0;
  391. hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
  392. }
  393. no_jiffies_check:
  394. if (delta > runtime->period_size + runtime->period_size / 2) {
  395. hw_ptr_error(substream, in_interrupt,
  396. "Lost interrupts?",
  397. "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
  398. substream->stream, (long)delta,
  399. (long)new_hw_ptr,
  400. (long)old_hw_ptr);
  401. }
  402. no_delta_check:
  403. if (runtime->status->hw_ptr == new_hw_ptr) {
  404. update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
  405. return 0;
  406. }
  407. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  408. runtime->silence_size > 0)
  409. snd_pcm_playback_silence(substream, new_hw_ptr);
  410. if (in_interrupt) {
  411. delta = new_hw_ptr - runtime->hw_ptr_interrupt;
  412. if (delta < 0)
  413. delta += runtime->boundary;
  414. delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
  415. runtime->hw_ptr_interrupt += delta;
  416. if (runtime->hw_ptr_interrupt >= runtime->boundary)
  417. runtime->hw_ptr_interrupt -= runtime->boundary;
  418. }
  419. runtime->hw_ptr_base = hw_base;
  420. runtime->status->hw_ptr = new_hw_ptr;
  421. runtime->hw_ptr_jiffies = curr_jiffies;
  422. if (crossed_boundary) {
  423. snd_BUG_ON(crossed_boundary != 1);
  424. runtime->hw_ptr_wrap += runtime->boundary;
  425. }
  426. update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
  427. return snd_pcm_update_state(substream, runtime);
  428. }
  429. /* CAUTION: call it with irq disabled */
  430. int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
  431. {
  432. return snd_pcm_update_hw_ptr0(substream, 0);
  433. }
  434. /**
  435. * snd_pcm_set_ops - set the PCM operators
  436. * @pcm: the pcm instance
  437. * @direction: stream direction, SNDRV_PCM_STREAM_XXX
  438. * @ops: the operator table
  439. *
  440. * Sets the given PCM operators to the pcm instance.
  441. */
  442. void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
  443. const struct snd_pcm_ops *ops)
  444. {
  445. struct snd_pcm_str *stream = &pcm->streams[direction];
  446. struct snd_pcm_substream *substream;
  447. for (substream = stream->substream; substream != NULL; substream = substream->next)
  448. substream->ops = ops;
  449. }
  450. EXPORT_SYMBOL(snd_pcm_set_ops);
  451. /**
  452. * snd_pcm_sync - set the PCM sync id
  453. * @substream: the pcm substream
  454. *
  455. * Sets the PCM sync identifier for the card.
  456. */
  457. void snd_pcm_set_sync(struct snd_pcm_substream *substream)
  458. {
  459. struct snd_pcm_runtime *runtime = substream->runtime;
  460. runtime->sync.id32[0] = substream->pcm->card->number;
  461. runtime->sync.id32[1] = -1;
  462. runtime->sync.id32[2] = -1;
  463. runtime->sync.id32[3] = -1;
  464. }
  465. EXPORT_SYMBOL(snd_pcm_set_sync);
  466. /*
  467. * Standard ioctl routine
  468. */
  469. static inline unsigned int div32(unsigned int a, unsigned int b,
  470. unsigned int *r)
  471. {
  472. if (b == 0) {
  473. *r = 0;
  474. return UINT_MAX;
  475. }
  476. *r = a % b;
  477. return a / b;
  478. }
  479. static inline unsigned int div_down(unsigned int a, unsigned int b)
  480. {
  481. if (b == 0)
  482. return UINT_MAX;
  483. return a / b;
  484. }
  485. static inline unsigned int div_up(unsigned int a, unsigned int b)
  486. {
  487. unsigned int r;
  488. unsigned int q;
  489. if (b == 0)
  490. return UINT_MAX;
  491. q = div32(a, b, &r);
  492. if (r)
  493. ++q;
  494. return q;
  495. }
  496. static inline unsigned int mul(unsigned int a, unsigned int b)
  497. {
  498. if (a == 0)
  499. return 0;
  500. if (div_down(UINT_MAX, a) < b)
  501. return UINT_MAX;
  502. return a * b;
  503. }
  504. static inline unsigned int muldiv32(unsigned int a, unsigned int b,
  505. unsigned int c, unsigned int *r)
  506. {
  507. u_int64_t n = (u_int64_t) a * b;
  508. if (c == 0) {
  509. snd_BUG_ON(!n);
  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 = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
  1055. if (!new) {
  1056. va_end(args);
  1057. return -ENOMEM;
  1058. }
  1059. if (constrs->rules) {
  1060. memcpy(new, constrs->rules,
  1061. constrs->rules_num * sizeof(*c));
  1062. kfree(constrs->rules);
  1063. }
  1064. constrs->rules = new;
  1065. constrs->rules_all = new_rules;
  1066. }
  1067. c = &constrs->rules[constrs->rules_num];
  1068. c->cond = cond;
  1069. c->func = func;
  1070. c->var = var;
  1071. c->private = private;
  1072. k = 0;
  1073. while (1) {
  1074. if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
  1075. va_end(args);
  1076. return -EINVAL;
  1077. }
  1078. c->deps[k++] = dep;
  1079. if (dep < 0)
  1080. break;
  1081. dep = va_arg(args, int);
  1082. }
  1083. constrs->rules_num++;
  1084. va_end(args);
  1085. return 0;
  1086. }
  1087. EXPORT_SYMBOL(snd_pcm_hw_rule_add);
  1088. /**
  1089. * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
  1090. * @runtime: PCM runtime instance
  1091. * @var: hw_params variable to apply the mask
  1092. * @mask: the bitmap mask
  1093. *
  1094. * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
  1095. *
  1096. * Return: Zero if successful, or a negative error code on failure.
  1097. */
  1098. int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1099. u_int32_t mask)
  1100. {
  1101. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1102. struct snd_mask *maskp = constrs_mask(constrs, var);
  1103. *maskp->bits &= mask;
  1104. memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
  1105. if (*maskp->bits == 0)
  1106. return -EINVAL;
  1107. return 0;
  1108. }
  1109. /**
  1110. * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
  1111. * @runtime: PCM runtime instance
  1112. * @var: hw_params variable to apply the mask
  1113. * @mask: the 64bit bitmap mask
  1114. *
  1115. * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
  1116. *
  1117. * Return: Zero if successful, or a negative error code on failure.
  1118. */
  1119. int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1120. u_int64_t mask)
  1121. {
  1122. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1123. struct snd_mask *maskp = constrs_mask(constrs, var);
  1124. maskp->bits[0] &= (u_int32_t)mask;
  1125. maskp->bits[1] &= (u_int32_t)(mask >> 32);
  1126. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
  1127. if (! maskp->bits[0] && ! maskp->bits[1])
  1128. return -EINVAL;
  1129. return 0;
  1130. }
  1131. EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
  1132. /**
  1133. * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
  1134. * @runtime: PCM runtime instance
  1135. * @var: hw_params variable to apply the integer constraint
  1136. *
  1137. * Apply the constraint of integer to an interval parameter.
  1138. *
  1139. * Return: Positive if the value is changed, zero if it's not changed, or a
  1140. * negative error code.
  1141. */
  1142. int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
  1143. {
  1144. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1145. return snd_interval_setinteger(constrs_interval(constrs, var));
  1146. }
  1147. EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
  1148. /**
  1149. * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
  1150. * @runtime: PCM runtime instance
  1151. * @var: hw_params variable to apply the range
  1152. * @min: the minimal value
  1153. * @max: the maximal value
  1154. *
  1155. * Apply the min/max range constraint to an interval parameter.
  1156. *
  1157. * Return: Positive if the value is changed, zero if it's not changed, or a
  1158. * negative error code.
  1159. */
  1160. int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1161. unsigned int min, unsigned int max)
  1162. {
  1163. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1164. struct snd_interval t;
  1165. t.min = min;
  1166. t.max = max;
  1167. t.openmin = t.openmax = 0;
  1168. t.integer = 0;
  1169. return snd_interval_refine(constrs_interval(constrs, var), &t);
  1170. }
  1171. EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
  1172. static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
  1173. struct snd_pcm_hw_rule *rule)
  1174. {
  1175. struct snd_pcm_hw_constraint_list *list = rule->private;
  1176. return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
  1177. }
  1178. /**
  1179. * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
  1180. * @runtime: PCM runtime instance
  1181. * @cond: condition bits
  1182. * @var: hw_params variable to apply the list constraint
  1183. * @l: list
  1184. *
  1185. * Apply the list of constraints to an interval parameter.
  1186. *
  1187. * Return: Zero if successful, or a negative error code on failure.
  1188. */
  1189. int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
  1190. unsigned int cond,
  1191. snd_pcm_hw_param_t var,
  1192. const struct snd_pcm_hw_constraint_list *l)
  1193. {
  1194. return snd_pcm_hw_rule_add(runtime, cond, var,
  1195. snd_pcm_hw_rule_list, (void *)l,
  1196. var, -1);
  1197. }
  1198. EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
  1199. static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
  1200. struct snd_pcm_hw_rule *rule)
  1201. {
  1202. struct snd_pcm_hw_constraint_ranges *r = rule->private;
  1203. return snd_interval_ranges(hw_param_interval(params, rule->var),
  1204. r->count, r->ranges, r->mask);
  1205. }
  1206. /**
  1207. * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
  1208. * @runtime: PCM runtime instance
  1209. * @cond: condition bits
  1210. * @var: hw_params variable to apply the list of range constraints
  1211. * @r: ranges
  1212. *
  1213. * Apply the list of range constraints to an interval parameter.
  1214. *
  1215. * Return: Zero if successful, or a negative error code on failure.
  1216. */
  1217. int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
  1218. unsigned int cond,
  1219. snd_pcm_hw_param_t var,
  1220. const struct snd_pcm_hw_constraint_ranges *r)
  1221. {
  1222. return snd_pcm_hw_rule_add(runtime, cond, var,
  1223. snd_pcm_hw_rule_ranges, (void *)r,
  1224. var, -1);
  1225. }
  1226. EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
  1227. static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
  1228. struct snd_pcm_hw_rule *rule)
  1229. {
  1230. const struct snd_pcm_hw_constraint_ratnums *r = rule->private;
  1231. unsigned int num = 0, den = 0;
  1232. int err;
  1233. err = snd_interval_ratnum(hw_param_interval(params, rule->var),
  1234. r->nrats, r->rats, &num, &den);
  1235. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1236. params->rate_num = num;
  1237. params->rate_den = den;
  1238. }
  1239. return err;
  1240. }
  1241. /**
  1242. * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
  1243. * @runtime: PCM runtime instance
  1244. * @cond: condition bits
  1245. * @var: hw_params variable to apply the ratnums constraint
  1246. * @r: struct snd_ratnums constriants
  1247. *
  1248. * Return: Zero if successful, or a negative error code on failure.
  1249. */
  1250. int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
  1251. unsigned int cond,
  1252. snd_pcm_hw_param_t var,
  1253. const struct snd_pcm_hw_constraint_ratnums *r)
  1254. {
  1255. return snd_pcm_hw_rule_add(runtime, cond, var,
  1256. snd_pcm_hw_rule_ratnums, (void *)r,
  1257. var, -1);
  1258. }
  1259. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
  1260. static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
  1261. struct snd_pcm_hw_rule *rule)
  1262. {
  1263. const struct snd_pcm_hw_constraint_ratdens *r = rule->private;
  1264. unsigned int num = 0, den = 0;
  1265. int err = snd_interval_ratden(hw_param_interval(params, rule->var),
  1266. r->nrats, r->rats, &num, &den);
  1267. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1268. params->rate_num = num;
  1269. params->rate_den = den;
  1270. }
  1271. return err;
  1272. }
  1273. /**
  1274. * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
  1275. * @runtime: PCM runtime instance
  1276. * @cond: condition bits
  1277. * @var: hw_params variable to apply the ratdens constraint
  1278. * @r: struct snd_ratdens constriants
  1279. *
  1280. * Return: Zero if successful, or a negative error code on failure.
  1281. */
  1282. int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
  1283. unsigned int cond,
  1284. snd_pcm_hw_param_t var,
  1285. const struct snd_pcm_hw_constraint_ratdens *r)
  1286. {
  1287. return snd_pcm_hw_rule_add(runtime, cond, var,
  1288. snd_pcm_hw_rule_ratdens, (void *)r,
  1289. var, -1);
  1290. }
  1291. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
  1292. static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
  1293. struct snd_pcm_hw_rule *rule)
  1294. {
  1295. unsigned int l = (unsigned long) rule->private;
  1296. int width = l & 0xffff;
  1297. unsigned int msbits = l >> 16;
  1298. const struct snd_interval *i =
  1299. hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
  1300. if (!snd_interval_single(i))
  1301. return 0;
  1302. if ((snd_interval_value(i) == width) ||
  1303. (width == 0 && snd_interval_value(i) > msbits))
  1304. params->msbits = min_not_zero(params->msbits, msbits);
  1305. return 0;
  1306. }
  1307. /**
  1308. * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
  1309. * @runtime: PCM runtime instance
  1310. * @cond: condition bits
  1311. * @width: sample bits width
  1312. * @msbits: msbits width
  1313. *
  1314. * This constraint will set the number of most significant bits (msbits) if a
  1315. * sample format with the specified width has been select. If width is set to 0
  1316. * the msbits will be set for any sample format with a width larger than the
  1317. * specified msbits.
  1318. *
  1319. * Return: Zero if successful, or a negative error code on failure.
  1320. */
  1321. int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
  1322. unsigned int cond,
  1323. unsigned int width,
  1324. unsigned int msbits)
  1325. {
  1326. unsigned long l = (msbits << 16) | width;
  1327. return snd_pcm_hw_rule_add(runtime, cond, -1,
  1328. snd_pcm_hw_rule_msbits,
  1329. (void*) l,
  1330. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1331. }
  1332. EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
  1333. static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
  1334. struct snd_pcm_hw_rule *rule)
  1335. {
  1336. unsigned long step = (unsigned long) rule->private;
  1337. return snd_interval_step(hw_param_interval(params, rule->var), step);
  1338. }
  1339. /**
  1340. * snd_pcm_hw_constraint_step - add a hw constraint step rule
  1341. * @runtime: PCM runtime instance
  1342. * @cond: condition bits
  1343. * @var: hw_params variable to apply the step constraint
  1344. * @step: step size
  1345. *
  1346. * Return: Zero if successful, or a negative error code on failure.
  1347. */
  1348. int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
  1349. unsigned int cond,
  1350. snd_pcm_hw_param_t var,
  1351. unsigned long step)
  1352. {
  1353. return snd_pcm_hw_rule_add(runtime, cond, var,
  1354. snd_pcm_hw_rule_step, (void *) step,
  1355. var, -1);
  1356. }
  1357. EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
  1358. static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
  1359. {
  1360. static unsigned int pow2_sizes[] = {
  1361. 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
  1362. 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
  1363. 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
  1364. 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
  1365. };
  1366. return snd_interval_list(hw_param_interval(params, rule->var),
  1367. ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
  1368. }
  1369. /**
  1370. * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
  1371. * @runtime: PCM runtime instance
  1372. * @cond: condition bits
  1373. * @var: hw_params variable to apply the power-of-2 constraint
  1374. *
  1375. * Return: Zero if successful, or a negative error code on failure.
  1376. */
  1377. int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
  1378. unsigned int cond,
  1379. snd_pcm_hw_param_t var)
  1380. {
  1381. return snd_pcm_hw_rule_add(runtime, cond, var,
  1382. snd_pcm_hw_rule_pow2, NULL,
  1383. var, -1);
  1384. }
  1385. EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
  1386. static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
  1387. struct snd_pcm_hw_rule *rule)
  1388. {
  1389. unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
  1390. struct snd_interval *rate;
  1391. rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  1392. return snd_interval_list(rate, 1, &base_rate, 0);
  1393. }
  1394. /**
  1395. * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
  1396. * @runtime: PCM runtime instance
  1397. * @base_rate: the rate at which the hardware does not resample
  1398. *
  1399. * Return: Zero if successful, or a negative error code on failure.
  1400. */
  1401. int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
  1402. unsigned int base_rate)
  1403. {
  1404. return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
  1405. SNDRV_PCM_HW_PARAM_RATE,
  1406. snd_pcm_hw_rule_noresample_func,
  1407. (void *)(uintptr_t)base_rate,
  1408. SNDRV_PCM_HW_PARAM_RATE, -1);
  1409. }
  1410. EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
  1411. static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
  1412. snd_pcm_hw_param_t var)
  1413. {
  1414. if (hw_is_mask(var)) {
  1415. snd_mask_any(hw_param_mask(params, var));
  1416. params->cmask |= 1 << var;
  1417. params->rmask |= 1 << var;
  1418. return;
  1419. }
  1420. if (hw_is_interval(var)) {
  1421. snd_interval_any(hw_param_interval(params, var));
  1422. params->cmask |= 1 << var;
  1423. params->rmask |= 1 << var;
  1424. return;
  1425. }
  1426. snd_BUG();
  1427. }
  1428. void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
  1429. {
  1430. unsigned int k;
  1431. memset(params, 0, sizeof(*params));
  1432. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
  1433. _snd_pcm_hw_param_any(params, k);
  1434. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
  1435. _snd_pcm_hw_param_any(params, k);
  1436. params->info = ~0U;
  1437. }
  1438. EXPORT_SYMBOL(_snd_pcm_hw_params_any);
  1439. /**
  1440. * snd_pcm_hw_param_value - return @params field @var value
  1441. * @params: the hw_params instance
  1442. * @var: parameter to retrieve
  1443. * @dir: pointer to the direction (-1,0,1) or %NULL
  1444. *
  1445. * Return: The value for field @var if it's fixed in configuration space
  1446. * defined by @params. -%EINVAL otherwise.
  1447. */
  1448. int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
  1449. snd_pcm_hw_param_t var, int *dir)
  1450. {
  1451. if (hw_is_mask(var)) {
  1452. const struct snd_mask *mask = hw_param_mask_c(params, var);
  1453. if (!snd_mask_single(mask))
  1454. return -EINVAL;
  1455. if (dir)
  1456. *dir = 0;
  1457. return snd_mask_value(mask);
  1458. }
  1459. if (hw_is_interval(var)) {
  1460. const struct snd_interval *i = hw_param_interval_c(params, var);
  1461. if (!snd_interval_single(i))
  1462. return -EINVAL;
  1463. if (dir)
  1464. *dir = i->openmin;
  1465. return snd_interval_value(i);
  1466. }
  1467. return -EINVAL;
  1468. }
  1469. EXPORT_SYMBOL(snd_pcm_hw_param_value);
  1470. void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
  1471. snd_pcm_hw_param_t var)
  1472. {
  1473. if (hw_is_mask(var)) {
  1474. snd_mask_none(hw_param_mask(params, var));
  1475. params->cmask |= 1 << var;
  1476. params->rmask |= 1 << var;
  1477. } else if (hw_is_interval(var)) {
  1478. snd_interval_none(hw_param_interval(params, var));
  1479. params->cmask |= 1 << var;
  1480. params->rmask |= 1 << var;
  1481. } else {
  1482. snd_BUG();
  1483. }
  1484. }
  1485. EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
  1486. static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
  1487. snd_pcm_hw_param_t var)
  1488. {
  1489. int changed;
  1490. if (hw_is_mask(var))
  1491. changed = snd_mask_refine_first(hw_param_mask(params, var));
  1492. else if (hw_is_interval(var))
  1493. changed = snd_interval_refine_first(hw_param_interval(params, var));
  1494. else
  1495. return -EINVAL;
  1496. if (changed) {
  1497. params->cmask |= 1 << var;
  1498. params->rmask |= 1 << var;
  1499. }
  1500. return changed;
  1501. }
  1502. /**
  1503. * snd_pcm_hw_param_first - refine config space and return minimum value
  1504. * @pcm: PCM instance
  1505. * @params: the hw_params instance
  1506. * @var: parameter to retrieve
  1507. * @dir: pointer to the direction (-1,0,1) or %NULL
  1508. *
  1509. * Inside configuration space defined by @params remove from @var all
  1510. * values > minimum. Reduce configuration space accordingly.
  1511. *
  1512. * Return: The minimum, or a negative error code on failure.
  1513. */
  1514. int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
  1515. struct snd_pcm_hw_params *params,
  1516. snd_pcm_hw_param_t var, int *dir)
  1517. {
  1518. int changed = _snd_pcm_hw_param_first(params, var);
  1519. if (changed < 0)
  1520. return changed;
  1521. if (params->rmask) {
  1522. int err = snd_pcm_hw_refine(pcm, params);
  1523. if (snd_BUG_ON(err < 0))
  1524. return err;
  1525. }
  1526. return snd_pcm_hw_param_value(params, var, dir);
  1527. }
  1528. EXPORT_SYMBOL(snd_pcm_hw_param_first);
  1529. static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
  1530. snd_pcm_hw_param_t var)
  1531. {
  1532. int changed;
  1533. if (hw_is_mask(var))
  1534. changed = snd_mask_refine_last(hw_param_mask(params, var));
  1535. else if (hw_is_interval(var))
  1536. changed = snd_interval_refine_last(hw_param_interval(params, var));
  1537. else
  1538. return -EINVAL;
  1539. if (changed) {
  1540. params->cmask |= 1 << var;
  1541. params->rmask |= 1 << var;
  1542. }
  1543. return changed;
  1544. }
  1545. /**
  1546. * snd_pcm_hw_param_last - refine config space and return maximum value
  1547. * @pcm: PCM instance
  1548. * @params: the hw_params instance
  1549. * @var: parameter to retrieve
  1550. * @dir: pointer to the direction (-1,0,1) or %NULL
  1551. *
  1552. * Inside configuration space defined by @params remove from @var all
  1553. * values < maximum. Reduce configuration space accordingly.
  1554. *
  1555. * Return: The maximum, or a negative error code on failure.
  1556. */
  1557. int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
  1558. struct snd_pcm_hw_params *params,
  1559. snd_pcm_hw_param_t var, int *dir)
  1560. {
  1561. int changed = _snd_pcm_hw_param_last(params, var);
  1562. if (changed < 0)
  1563. return changed;
  1564. if (params->rmask) {
  1565. int err = snd_pcm_hw_refine(pcm, params);
  1566. if (snd_BUG_ON(err < 0))
  1567. return err;
  1568. }
  1569. return snd_pcm_hw_param_value(params, var, dir);
  1570. }
  1571. EXPORT_SYMBOL(snd_pcm_hw_param_last);
  1572. static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
  1573. void *arg)
  1574. {
  1575. struct snd_pcm_runtime *runtime = substream->runtime;
  1576. unsigned long flags;
  1577. snd_pcm_stream_lock_irqsave(substream, flags);
  1578. if (snd_pcm_running(substream) &&
  1579. snd_pcm_update_hw_ptr(substream) >= 0)
  1580. runtime->status->hw_ptr %= runtime->buffer_size;
  1581. else {
  1582. runtime->status->hw_ptr = 0;
  1583. runtime->hw_ptr_wrap = 0;
  1584. }
  1585. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1586. return 0;
  1587. }
  1588. static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
  1589. void *arg)
  1590. {
  1591. struct snd_pcm_channel_info *info = arg;
  1592. struct snd_pcm_runtime *runtime = substream->runtime;
  1593. int width;
  1594. if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
  1595. info->offset = -1;
  1596. return 0;
  1597. }
  1598. width = snd_pcm_format_physical_width(runtime->format);
  1599. if (width < 0)
  1600. return width;
  1601. info->offset = 0;
  1602. switch (runtime->access) {
  1603. case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
  1604. case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
  1605. info->first = info->channel * width;
  1606. info->step = runtime->channels * width;
  1607. break;
  1608. case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
  1609. case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
  1610. {
  1611. size_t size = runtime->dma_bytes / runtime->channels;
  1612. info->first = info->channel * size * 8;
  1613. info->step = width;
  1614. break;
  1615. }
  1616. default:
  1617. snd_BUG();
  1618. break;
  1619. }
  1620. return 0;
  1621. }
  1622. static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
  1623. void *arg)
  1624. {
  1625. struct snd_pcm_hw_params *params = arg;
  1626. snd_pcm_format_t format;
  1627. int channels;
  1628. ssize_t frame_size;
  1629. params->fifo_size = substream->runtime->hw.fifo_size;
  1630. if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
  1631. format = params_format(params);
  1632. channels = params_channels(params);
  1633. frame_size = snd_pcm_format_size(format, channels);
  1634. if (frame_size > 0)
  1635. params->fifo_size /= (unsigned)frame_size;
  1636. }
  1637. return 0;
  1638. }
  1639. /**
  1640. * snd_pcm_lib_ioctl - a generic PCM ioctl callback
  1641. * @substream: the pcm substream instance
  1642. * @cmd: ioctl command
  1643. * @arg: ioctl argument
  1644. *
  1645. * Processes the generic ioctl commands for PCM.
  1646. * Can be passed as the ioctl callback for PCM ops.
  1647. *
  1648. * Return: Zero if successful, or a negative error code on failure.
  1649. */
  1650. int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  1651. unsigned int cmd, void *arg)
  1652. {
  1653. switch (cmd) {
  1654. case SNDRV_PCM_IOCTL1_INFO:
  1655. return 0;
  1656. case SNDRV_PCM_IOCTL1_RESET:
  1657. return snd_pcm_lib_ioctl_reset(substream, arg);
  1658. case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
  1659. return snd_pcm_lib_ioctl_channel_info(substream, arg);
  1660. case SNDRV_PCM_IOCTL1_FIFO_SIZE:
  1661. return snd_pcm_lib_ioctl_fifo_size(substream, arg);
  1662. }
  1663. return -ENXIO;
  1664. }
  1665. EXPORT_SYMBOL(snd_pcm_lib_ioctl);
  1666. /**
  1667. * snd_pcm_period_elapsed - update the pcm status for the next period
  1668. * @substream: the pcm substream instance
  1669. *
  1670. * This function is called from the interrupt handler when the
  1671. * PCM has processed the period size. It will update the current
  1672. * pointer, wake up sleepers, etc.
  1673. *
  1674. * Even if more than one periods have elapsed since the last call, you
  1675. * have to call this only once.
  1676. */
  1677. void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
  1678. {
  1679. struct snd_pcm_runtime *runtime;
  1680. unsigned long flags;
  1681. if (PCM_RUNTIME_CHECK(substream))
  1682. return;
  1683. runtime = substream->runtime;
  1684. snd_pcm_stream_lock_irqsave(substream, flags);
  1685. if (!snd_pcm_running(substream) ||
  1686. snd_pcm_update_hw_ptr0(substream, 1) < 0)
  1687. goto _end;
  1688. #ifdef CONFIG_SND_PCM_TIMER
  1689. if (substream->timer_running)
  1690. snd_timer_interrupt(substream->timer, 1);
  1691. #endif
  1692. _end:
  1693. kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
  1694. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1695. }
  1696. EXPORT_SYMBOL(snd_pcm_period_elapsed);
  1697. /*
  1698. * Wait until avail_min data becomes available
  1699. * Returns a negative error code if any error occurs during operation.
  1700. * The available space is stored on availp. When err = 0 and avail = 0
  1701. * on the capture stream, it indicates the stream is in DRAINING state.
  1702. */
  1703. static int wait_for_avail(struct snd_pcm_substream *substream,
  1704. snd_pcm_uframes_t *availp)
  1705. {
  1706. struct snd_pcm_runtime *runtime = substream->runtime;
  1707. int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  1708. wait_queue_t wait;
  1709. int err = 0;
  1710. snd_pcm_uframes_t avail = 0;
  1711. long wait_time, tout;
  1712. init_waitqueue_entry(&wait, current);
  1713. set_current_state(TASK_INTERRUPTIBLE);
  1714. add_wait_queue(&runtime->tsleep, &wait);
  1715. if (runtime->no_period_wakeup)
  1716. wait_time = MAX_SCHEDULE_TIMEOUT;
  1717. else {
  1718. wait_time = 10;
  1719. if (runtime->rate) {
  1720. long t = runtime->period_size * 2 / runtime->rate;
  1721. wait_time = max(t, wait_time);
  1722. }
  1723. wait_time = msecs_to_jiffies(wait_time * 1000);
  1724. }
  1725. for (;;) {
  1726. if (signal_pending(current)) {
  1727. err = -ERESTARTSYS;
  1728. break;
  1729. }
  1730. /*
  1731. * We need to check if space became available already
  1732. * (and thus the wakeup happened already) first to close
  1733. * the race of space already having become available.
  1734. * This check must happen after been added to the waitqueue
  1735. * and having current state be INTERRUPTIBLE.
  1736. */
  1737. if (is_playback)
  1738. avail = snd_pcm_playback_avail(runtime);
  1739. else
  1740. avail = snd_pcm_capture_avail(runtime);
  1741. if (avail >= runtime->twake)
  1742. break;
  1743. snd_pcm_stream_unlock_irq(substream);
  1744. tout = schedule_timeout(wait_time);
  1745. snd_pcm_stream_lock_irq(substream);
  1746. set_current_state(TASK_INTERRUPTIBLE);
  1747. switch (runtime->status->state) {
  1748. case SNDRV_PCM_STATE_SUSPENDED:
  1749. err = -ESTRPIPE;
  1750. goto _endloop;
  1751. case SNDRV_PCM_STATE_XRUN:
  1752. err = -EPIPE;
  1753. goto _endloop;
  1754. case SNDRV_PCM_STATE_DRAINING:
  1755. if (is_playback)
  1756. err = -EPIPE;
  1757. else
  1758. avail = 0; /* indicate draining */
  1759. goto _endloop;
  1760. case SNDRV_PCM_STATE_OPEN:
  1761. case SNDRV_PCM_STATE_SETUP:
  1762. case SNDRV_PCM_STATE_DISCONNECTED:
  1763. err = -EBADFD;
  1764. goto _endloop;
  1765. case SNDRV_PCM_STATE_PAUSED:
  1766. continue;
  1767. }
  1768. if (!tout) {
  1769. pcm_dbg(substream->pcm,
  1770. "%s write error (DMA or IRQ trouble?)\n",
  1771. is_playback ? "playback" : "capture");
  1772. err = -EIO;
  1773. break;
  1774. }
  1775. }
  1776. _endloop:
  1777. set_current_state(TASK_RUNNING);
  1778. remove_wait_queue(&runtime->tsleep, &wait);
  1779. *availp = avail;
  1780. return err;
  1781. }
  1782. typedef int (*pcm_transfer_f)(struct snd_pcm_substream *substream,
  1783. int channel, unsigned long hwoff,
  1784. void *buf, unsigned long bytes);
  1785. typedef int (*pcm_copy_f)(struct snd_pcm_substream *, snd_pcm_uframes_t, void *,
  1786. snd_pcm_uframes_t, snd_pcm_uframes_t, pcm_transfer_f);
  1787. /* calculate the target DMA-buffer position to be written/read */
  1788. static void *get_dma_ptr(struct snd_pcm_runtime *runtime,
  1789. int channel, unsigned long hwoff)
  1790. {
  1791. return runtime->dma_area + hwoff +
  1792. channel * (runtime->dma_bytes / runtime->channels);
  1793. }
  1794. /* default copy_user ops for write; used for both interleaved and non- modes */
  1795. static int default_write_copy(struct snd_pcm_substream *substream,
  1796. int channel, unsigned long hwoff,
  1797. void *buf, unsigned long bytes)
  1798. {
  1799. if (copy_from_user(get_dma_ptr(substream->runtime, channel, hwoff),
  1800. (void __user *)buf, bytes))
  1801. return -EFAULT;
  1802. return 0;
  1803. }
  1804. /* default copy_kernel ops for write */
  1805. static int default_write_copy_kernel(struct snd_pcm_substream *substream,
  1806. int channel, unsigned long hwoff,
  1807. void *buf, unsigned long bytes)
  1808. {
  1809. memcpy(get_dma_ptr(substream->runtime, channel, hwoff), buf, bytes);
  1810. return 0;
  1811. }
  1812. /* fill silence instead of copy data; called as a transfer helper
  1813. * from __snd_pcm_lib_write() or directly from noninterleaved_copy() when
  1814. * a NULL buffer is passed
  1815. */
  1816. static int fill_silence(struct snd_pcm_substream *substream, int channel,
  1817. unsigned long hwoff, void *buf, unsigned long bytes)
  1818. {
  1819. struct snd_pcm_runtime *runtime = substream->runtime;
  1820. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  1821. return 0;
  1822. if (substream->ops->fill_silence)
  1823. return substream->ops->fill_silence(substream, channel,
  1824. hwoff, bytes);
  1825. snd_pcm_format_set_silence(runtime->format,
  1826. get_dma_ptr(runtime, channel, hwoff),
  1827. bytes_to_samples(runtime, bytes));
  1828. return 0;
  1829. }
  1830. /* default copy_user ops for read; used for both interleaved and non- modes */
  1831. static int default_read_copy(struct snd_pcm_substream *substream,
  1832. int channel, unsigned long hwoff,
  1833. void *buf, unsigned long bytes)
  1834. {
  1835. if (copy_to_user((void __user *)buf,
  1836. get_dma_ptr(substream->runtime, channel, hwoff),
  1837. bytes))
  1838. return -EFAULT;
  1839. return 0;
  1840. }
  1841. /* default copy_kernel ops for read */
  1842. static int default_read_copy_kernel(struct snd_pcm_substream *substream,
  1843. int channel, unsigned long hwoff,
  1844. void *buf, unsigned long bytes)
  1845. {
  1846. memcpy(buf, get_dma_ptr(substream->runtime, channel, hwoff), bytes);
  1847. return 0;
  1848. }
  1849. /* call transfer function with the converted pointers and sizes;
  1850. * for interleaved mode, it's one shot for all samples
  1851. */
  1852. static int interleaved_copy(struct snd_pcm_substream *substream,
  1853. snd_pcm_uframes_t hwoff, void *data,
  1854. snd_pcm_uframes_t off,
  1855. snd_pcm_uframes_t frames,
  1856. pcm_transfer_f transfer)
  1857. {
  1858. struct snd_pcm_runtime *runtime = substream->runtime;
  1859. /* convert to bytes */
  1860. hwoff = frames_to_bytes(runtime, hwoff);
  1861. off = frames_to_bytes(runtime, off);
  1862. frames = frames_to_bytes(runtime, frames);
  1863. return transfer(substream, 0, hwoff, data + off, frames);
  1864. }
  1865. /* call transfer function with the converted pointers and sizes for each
  1866. * non-interleaved channel; when buffer is NULL, silencing instead of copying
  1867. */
  1868. static int noninterleaved_copy(struct snd_pcm_substream *substream,
  1869. snd_pcm_uframes_t hwoff, void *data,
  1870. snd_pcm_uframes_t off,
  1871. snd_pcm_uframes_t frames,
  1872. pcm_transfer_f transfer)
  1873. {
  1874. struct snd_pcm_runtime *runtime = substream->runtime;
  1875. int channels = runtime->channels;
  1876. void **bufs = data;
  1877. int c, err;
  1878. /* convert to bytes; note that it's not frames_to_bytes() here.
  1879. * in non-interleaved mode, we copy for each channel, thus
  1880. * each copy is n_samples bytes x channels = whole frames.
  1881. */
  1882. off = samples_to_bytes(runtime, off);
  1883. frames = samples_to_bytes(runtime, frames);
  1884. hwoff = samples_to_bytes(runtime, hwoff);
  1885. for (c = 0; c < channels; ++c, ++bufs) {
  1886. if (!data || !*bufs)
  1887. err = fill_silence(substream, c, hwoff, NULL, frames);
  1888. else
  1889. err = transfer(substream, c, hwoff, *bufs + off,
  1890. frames);
  1891. if (err < 0)
  1892. return err;
  1893. }
  1894. return 0;
  1895. }
  1896. /* fill silence on the given buffer position;
  1897. * called from snd_pcm_playback_silence()
  1898. */
  1899. static int fill_silence_frames(struct snd_pcm_substream *substream,
  1900. snd_pcm_uframes_t off, snd_pcm_uframes_t frames)
  1901. {
  1902. if (substream->runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
  1903. substream->runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED)
  1904. return interleaved_copy(substream, off, NULL, 0, frames,
  1905. fill_silence);
  1906. else
  1907. return noninterleaved_copy(substream, off, NULL, 0, frames,
  1908. fill_silence);
  1909. }
  1910. /* sanity-check for read/write methods */
  1911. static int pcm_sanity_check(struct snd_pcm_substream *substream)
  1912. {
  1913. struct snd_pcm_runtime *runtime;
  1914. if (PCM_RUNTIME_CHECK(substream))
  1915. return -ENXIO;
  1916. runtime = substream->runtime;
  1917. if (snd_BUG_ON(!substream->ops->copy_user && !runtime->dma_area))
  1918. return -EINVAL;
  1919. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1920. return -EBADFD;
  1921. return 0;
  1922. }
  1923. static int pcm_accessible_state(struct snd_pcm_runtime *runtime)
  1924. {
  1925. switch (runtime->status->state) {
  1926. case SNDRV_PCM_STATE_PREPARED:
  1927. case SNDRV_PCM_STATE_RUNNING:
  1928. case SNDRV_PCM_STATE_PAUSED:
  1929. return 0;
  1930. case SNDRV_PCM_STATE_XRUN:
  1931. return -EPIPE;
  1932. case SNDRV_PCM_STATE_SUSPENDED:
  1933. return -ESTRPIPE;
  1934. default:
  1935. return -EBADFD;
  1936. }
  1937. }
  1938. /* update to the given appl_ptr and call ack callback if needed;
  1939. * when an error is returned, take back to the original value
  1940. */
  1941. int pcm_lib_apply_appl_ptr(struct snd_pcm_substream *substream,
  1942. snd_pcm_uframes_t appl_ptr)
  1943. {
  1944. struct snd_pcm_runtime *runtime = substream->runtime;
  1945. snd_pcm_uframes_t old_appl_ptr = runtime->control->appl_ptr;
  1946. int ret;
  1947. if (old_appl_ptr == appl_ptr)
  1948. return 0;
  1949. runtime->control->appl_ptr = appl_ptr;
  1950. if (substream->ops->ack) {
  1951. ret = substream->ops->ack(substream);
  1952. if (ret < 0) {
  1953. runtime->control->appl_ptr = old_appl_ptr;
  1954. return ret;
  1955. }
  1956. }
  1957. trace_applptr(substream, old_appl_ptr, appl_ptr);
  1958. return 0;
  1959. }
  1960. /* the common loop for read/write data */
  1961. snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
  1962. void *data, bool interleaved,
  1963. snd_pcm_uframes_t size, bool in_kernel)
  1964. {
  1965. struct snd_pcm_runtime *runtime = substream->runtime;
  1966. snd_pcm_uframes_t xfer = 0;
  1967. snd_pcm_uframes_t offset = 0;
  1968. snd_pcm_uframes_t avail;
  1969. pcm_copy_f writer;
  1970. pcm_transfer_f transfer;
  1971. bool nonblock;
  1972. bool is_playback;
  1973. int err;
  1974. err = pcm_sanity_check(substream);
  1975. if (err < 0)
  1976. return err;
  1977. is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  1978. if (interleaved) {
  1979. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
  1980. runtime->channels > 1)
  1981. return -EINVAL;
  1982. writer = interleaved_copy;
  1983. } else {
  1984. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  1985. return -EINVAL;
  1986. writer = noninterleaved_copy;
  1987. }
  1988. if (!data) {
  1989. if (is_playback)
  1990. transfer = fill_silence;
  1991. else
  1992. return -EINVAL;
  1993. } else if (in_kernel) {
  1994. if (substream->ops->copy_kernel)
  1995. transfer = substream->ops->copy_kernel;
  1996. else
  1997. transfer = is_playback ?
  1998. default_write_copy_kernel : default_read_copy_kernel;
  1999. } else {
  2000. if (substream->ops->copy_user)
  2001. transfer = (pcm_transfer_f)substream->ops->copy_user;
  2002. else
  2003. transfer = is_playback ?
  2004. default_write_copy : default_read_copy;
  2005. }
  2006. if (size == 0)
  2007. return 0;
  2008. nonblock = !!(substream->f_flags & O_NONBLOCK);
  2009. snd_pcm_stream_lock_irq(substream);
  2010. err = pcm_accessible_state(runtime);
  2011. if (err < 0)
  2012. goto _end_unlock;
  2013. if (!is_playback &&
  2014. runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
  2015. size >= runtime->start_threshold) {
  2016. err = snd_pcm_start(substream);
  2017. if (err < 0)
  2018. goto _end_unlock;
  2019. }
  2020. runtime->twake = runtime->control->avail_min ? : 1;
  2021. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  2022. snd_pcm_update_hw_ptr(substream);
  2023. if (is_playback)
  2024. avail = snd_pcm_playback_avail(runtime);
  2025. else
  2026. avail = snd_pcm_capture_avail(runtime);
  2027. while (size > 0) {
  2028. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  2029. snd_pcm_uframes_t cont;
  2030. if (!avail) {
  2031. if (!is_playback &&
  2032. runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  2033. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  2034. goto _end_unlock;
  2035. }
  2036. if (nonblock) {
  2037. err = -EAGAIN;
  2038. goto _end_unlock;
  2039. }
  2040. runtime->twake = min_t(snd_pcm_uframes_t, size,
  2041. runtime->control->avail_min ? : 1);
  2042. err = wait_for_avail(substream, &avail);
  2043. if (err < 0)
  2044. goto _end_unlock;
  2045. if (!avail)
  2046. continue; /* draining */
  2047. }
  2048. frames = size > avail ? avail : size;
  2049. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  2050. if (frames > cont)
  2051. frames = cont;
  2052. if (snd_BUG_ON(!frames)) {
  2053. runtime->twake = 0;
  2054. snd_pcm_stream_unlock_irq(substream);
  2055. return -EINVAL;
  2056. }
  2057. appl_ptr = runtime->control->appl_ptr;
  2058. appl_ofs = appl_ptr % runtime->buffer_size;
  2059. snd_pcm_stream_unlock_irq(substream);
  2060. err = writer(substream, appl_ofs, data, offset, frames,
  2061. transfer);
  2062. snd_pcm_stream_lock_irq(substream);
  2063. if (err < 0)
  2064. goto _end_unlock;
  2065. err = pcm_accessible_state(runtime);
  2066. if (err < 0)
  2067. goto _end_unlock;
  2068. appl_ptr += frames;
  2069. if (appl_ptr >= runtime->boundary)
  2070. appl_ptr -= runtime->boundary;
  2071. err = pcm_lib_apply_appl_ptr(substream, appl_ptr);
  2072. if (err < 0)
  2073. goto _end_unlock;
  2074. offset += frames;
  2075. size -= frames;
  2076. xfer += frames;
  2077. avail -= frames;
  2078. if (is_playback &&
  2079. runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
  2080. snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
  2081. err = snd_pcm_start(substream);
  2082. if (err < 0)
  2083. goto _end_unlock;
  2084. }
  2085. }
  2086. _end_unlock:
  2087. runtime->twake = 0;
  2088. if (xfer > 0 && err >= 0)
  2089. snd_pcm_update_state(substream, runtime);
  2090. snd_pcm_stream_unlock_irq(substream);
  2091. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  2092. }
  2093. EXPORT_SYMBOL(__snd_pcm_lib_xfer);
  2094. /*
  2095. * standard channel mapping helpers
  2096. */
  2097. /* default channel maps for multi-channel playbacks, up to 8 channels */
  2098. const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
  2099. { .channels = 1,
  2100. .map = { SNDRV_CHMAP_MONO } },
  2101. { .channels = 2,
  2102. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  2103. { .channels = 4,
  2104. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2105. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  2106. { .channels = 6,
  2107. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2108. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  2109. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
  2110. { .channels = 8,
  2111. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2112. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  2113. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  2114. SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
  2115. { }
  2116. };
  2117. EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
  2118. /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
  2119. const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
  2120. { .channels = 1,
  2121. .map = { SNDRV_CHMAP_MONO } },
  2122. { .channels = 2,
  2123. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  2124. { .channels = 4,
  2125. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2126. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  2127. { .channels = 6,
  2128. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2129. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  2130. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  2131. { .channels = 8,
  2132. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2133. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  2134. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  2135. SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
  2136. { }
  2137. };
  2138. EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
  2139. static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
  2140. {
  2141. if (ch > info->max_channels)
  2142. return false;
  2143. return !info->channel_mask || (info->channel_mask & (1U << ch));
  2144. }
  2145. static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
  2146. struct snd_ctl_elem_info *uinfo)
  2147. {
  2148. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2149. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2150. uinfo->count = 0;
  2151. uinfo->count = info->max_channels;
  2152. uinfo->value.integer.min = 0;
  2153. uinfo->value.integer.max = SNDRV_CHMAP_LAST;
  2154. return 0;
  2155. }
  2156. /* get callback for channel map ctl element
  2157. * stores the channel position firstly matching with the current channels
  2158. */
  2159. static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  2160. struct snd_ctl_elem_value *ucontrol)
  2161. {
  2162. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2163. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  2164. struct snd_pcm_substream *substream;
  2165. const struct snd_pcm_chmap_elem *map;
  2166. if (snd_BUG_ON(!info->chmap))
  2167. return -EINVAL;
  2168. substream = snd_pcm_chmap_substream(info, idx);
  2169. if (!substream)
  2170. return -ENODEV;
  2171. memset(ucontrol->value.integer.value, 0,
  2172. sizeof(ucontrol->value.integer.value));
  2173. if (!substream->runtime)
  2174. return 0; /* no channels set */
  2175. for (map = info->chmap; map->channels; map++) {
  2176. int i;
  2177. if (map->channels == substream->runtime->channels &&
  2178. valid_chmap_channels(info, map->channels)) {
  2179. for (i = 0; i < map->channels; i++)
  2180. ucontrol->value.integer.value[i] = map->map[i];
  2181. return 0;
  2182. }
  2183. }
  2184. return -EINVAL;
  2185. }
  2186. /* tlv callback for channel map ctl element
  2187. * expands the pre-defined channel maps in a form of TLV
  2188. */
  2189. static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  2190. unsigned int size, unsigned int __user *tlv)
  2191. {
  2192. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2193. const struct snd_pcm_chmap_elem *map;
  2194. unsigned int __user *dst;
  2195. int c, count = 0;
  2196. if (snd_BUG_ON(!info->chmap))
  2197. return -EINVAL;
  2198. if (size < 8)
  2199. return -ENOMEM;
  2200. if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
  2201. return -EFAULT;
  2202. size -= 8;
  2203. dst = tlv + 2;
  2204. for (map = info->chmap; map->channels; map++) {
  2205. int chs_bytes = map->channels * 4;
  2206. if (!valid_chmap_channels(info, map->channels))
  2207. continue;
  2208. if (size < 8)
  2209. return -ENOMEM;
  2210. if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
  2211. put_user(chs_bytes, dst + 1))
  2212. return -EFAULT;
  2213. dst += 2;
  2214. size -= 8;
  2215. count += 8;
  2216. if (size < chs_bytes)
  2217. return -ENOMEM;
  2218. size -= chs_bytes;
  2219. count += chs_bytes;
  2220. for (c = 0; c < map->channels; c++) {
  2221. if (put_user(map->map[c], dst))
  2222. return -EFAULT;
  2223. dst++;
  2224. }
  2225. }
  2226. if (put_user(count, tlv + 1))
  2227. return -EFAULT;
  2228. return 0;
  2229. }
  2230. static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
  2231. {
  2232. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2233. info->pcm->streams[info->stream].chmap_kctl = NULL;
  2234. kfree(info);
  2235. }
  2236. /**
  2237. * snd_pcm_add_chmap_ctls - create channel-mapping control elements
  2238. * @pcm: the assigned PCM instance
  2239. * @stream: stream direction
  2240. * @chmap: channel map elements (for query)
  2241. * @max_channels: the max number of channels for the stream
  2242. * @private_value: the value passed to each kcontrol's private_value field
  2243. * @info_ret: store struct snd_pcm_chmap instance if non-NULL
  2244. *
  2245. * Create channel-mapping control elements assigned to the given PCM stream(s).
  2246. * Return: Zero if successful, or a negative error value.
  2247. */
  2248. int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
  2249. const struct snd_pcm_chmap_elem *chmap,
  2250. int max_channels,
  2251. unsigned long private_value,
  2252. struct snd_pcm_chmap **info_ret)
  2253. {
  2254. struct snd_pcm_chmap *info;
  2255. struct snd_kcontrol_new knew = {
  2256. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2257. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  2258. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  2259. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
  2260. .info = pcm_chmap_ctl_info,
  2261. .get = pcm_chmap_ctl_get,
  2262. .tlv.c = pcm_chmap_ctl_tlv,
  2263. };
  2264. int err;
  2265. if (WARN_ON(pcm->streams[stream].chmap_kctl))
  2266. return -EBUSY;
  2267. info = kzalloc(sizeof(*info), GFP_KERNEL);
  2268. if (!info)
  2269. return -ENOMEM;
  2270. info->pcm = pcm;
  2271. info->stream = stream;
  2272. info->chmap = chmap;
  2273. info->max_channels = max_channels;
  2274. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  2275. knew.name = "Playback Channel Map";
  2276. else
  2277. knew.name = "Capture Channel Map";
  2278. knew.device = pcm->device;
  2279. knew.count = pcm->streams[stream].substream_count;
  2280. knew.private_value = private_value;
  2281. info->kctl = snd_ctl_new1(&knew, info);
  2282. if (!info->kctl) {
  2283. kfree(info);
  2284. return -ENOMEM;
  2285. }
  2286. info->kctl->private_free = pcm_chmap_ctl_private_free;
  2287. err = snd_ctl_add(pcm->card, info->kctl);
  2288. if (err < 0)
  2289. return err;
  2290. pcm->streams[stream].chmap_kctl = info->kctl;
  2291. if (info_ret)
  2292. *info_ret = info;
  2293. return 0;
  2294. }
  2295. EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);