pcm_lib.c 71 KB

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