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/time.h>
  24. #include <linux/math64.h>
  25. #include <linux/export.h>
  26. #include <sound/core.h>
  27. #include <sound/control.h>
  28. #include <sound/tlv.h>
  29. #include <sound/info.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/timer.h>
  33. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  34. #define CREATE_TRACE_POINTS
  35. #include "pcm_trace.h"
  36. #else
  37. #define trace_hwptr(substream, pos, in_interrupt)
  38. #define trace_xrun(substream)
  39. #define trace_hw_ptr_error(substream, reason)
  40. #endif
  41. /*
  42. * fill ring buffer with silence
  43. * runtime->silence_start: starting pointer to silence area
  44. * runtime->silence_filled: size filled with silence
  45. * runtime->silence_threshold: threshold from application
  46. * runtime->silence_size: maximal size from application
  47. *
  48. * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
  49. */
  50. void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
  51. {
  52. struct snd_pcm_runtime *runtime = substream->runtime;
  53. snd_pcm_uframes_t frames, ofs, transfer;
  54. if (runtime->silence_size < runtime->boundary) {
  55. snd_pcm_sframes_t noise_dist, n;
  56. if (runtime->silence_start != runtime->control->appl_ptr) {
  57. n = runtime->control->appl_ptr - runtime->silence_start;
  58. if (n < 0)
  59. n += runtime->boundary;
  60. if ((snd_pcm_uframes_t)n < runtime->silence_filled)
  61. runtime->silence_filled -= n;
  62. else
  63. runtime->silence_filled = 0;
  64. runtime->silence_start = runtime->control->appl_ptr;
  65. }
  66. if (runtime->silence_filled >= runtime->buffer_size)
  67. return;
  68. noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
  69. if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
  70. return;
  71. frames = runtime->silence_threshold - noise_dist;
  72. if (frames > runtime->silence_size)
  73. frames = runtime->silence_size;
  74. } else {
  75. if (new_hw_ptr == ULONG_MAX) { /* initialization */
  76. snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
  77. if (avail > runtime->buffer_size)
  78. avail = runtime->buffer_size;
  79. runtime->silence_filled = avail > 0 ? avail : 0;
  80. runtime->silence_start = (runtime->status->hw_ptr +
  81. runtime->silence_filled) %
  82. runtime->boundary;
  83. } else {
  84. ofs = runtime->status->hw_ptr;
  85. frames = new_hw_ptr - ofs;
  86. if ((snd_pcm_sframes_t)frames < 0)
  87. frames += runtime->boundary;
  88. runtime->silence_filled -= frames;
  89. if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
  90. runtime->silence_filled = 0;
  91. runtime->silence_start = new_hw_ptr;
  92. } else {
  93. runtime->silence_start = ofs;
  94. }
  95. }
  96. frames = runtime->buffer_size - runtime->silence_filled;
  97. }
  98. if (snd_BUG_ON(frames > runtime->buffer_size))
  99. return;
  100. if (frames == 0)
  101. return;
  102. ofs = runtime->silence_start % runtime->buffer_size;
  103. while (frames > 0) {
  104. transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
  105. if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
  106. runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
  107. if (substream->ops->silence) {
  108. int err;
  109. err = substream->ops->silence(substream, -1, ofs, transfer);
  110. snd_BUG_ON(err < 0);
  111. } else {
  112. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
  113. snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
  114. }
  115. } else {
  116. unsigned int c;
  117. unsigned int channels = runtime->channels;
  118. if (substream->ops->silence) {
  119. for (c = 0; c < channels; ++c) {
  120. int err;
  121. err = substream->ops->silence(substream, c, ofs, transfer);
  122. snd_BUG_ON(err < 0);
  123. }
  124. } else {
  125. size_t dma_csize = runtime->dma_bytes / channels;
  126. for (c = 0; c < channels; ++c) {
  127. char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
  128. snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
  129. }
  130. }
  131. }
  132. runtime->silence_filled += transfer;
  133. frames -= transfer;
  134. ofs = 0;
  135. }
  136. }
  137. #ifdef CONFIG_SND_DEBUG
  138. void snd_pcm_debug_name(struct snd_pcm_substream *substream,
  139. char *name, size_t len)
  140. {
  141. snprintf(name, len, "pcmC%dD%d%c:%d",
  142. substream->pcm->card->number,
  143. substream->pcm->device,
  144. substream->stream ? 'c' : 'p',
  145. substream->number);
  146. }
  147. EXPORT_SYMBOL(snd_pcm_debug_name);
  148. #endif
  149. #define XRUN_DEBUG_BASIC (1<<0)
  150. #define XRUN_DEBUG_STACK (1<<1) /* dump also stack */
  151. #define XRUN_DEBUG_JIFFIESCHECK (1<<2) /* do jiffies check */
  152. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  153. #define xrun_debug(substream, mask) \
  154. ((substream)->pstr->xrun_debug & (mask))
  155. #else
  156. #define xrun_debug(substream, mask) 0
  157. #endif
  158. #define dump_stack_on_xrun(substream) do { \
  159. if (xrun_debug(substream, XRUN_DEBUG_STACK)) \
  160. dump_stack(); \
  161. } while (0)
  162. static void xrun(struct snd_pcm_substream *substream)
  163. {
  164. struct snd_pcm_runtime *runtime = substream->runtime;
  165. trace_xrun(substream);
  166. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
  167. snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
  168. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  169. if (xrun_debug(substream, XRUN_DEBUG_BASIC)) {
  170. char name[16];
  171. snd_pcm_debug_name(substream, name, sizeof(name));
  172. pcm_warn(substream->pcm, "XRUN: %s\n", name);
  173. dump_stack_on_xrun(substream);
  174. }
  175. }
  176. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  177. #define hw_ptr_error(substream, in_interrupt, reason, fmt, args...) \
  178. do { \
  179. trace_hw_ptr_error(substream, reason); \
  180. if (xrun_debug(substream, XRUN_DEBUG_BASIC)) { \
  181. pr_err_ratelimited("ALSA: PCM: [%c] " reason ": " fmt, \
  182. (in_interrupt) ? 'Q' : 'P', ##args); \
  183. dump_stack_on_xrun(substream); \
  184. } \
  185. } while (0)
  186. #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
  187. #define hw_ptr_error(substream, fmt, args...) do { } while (0)
  188. #endif
  189. int snd_pcm_update_state(struct snd_pcm_substream *substream,
  190. struct snd_pcm_runtime *runtime)
  191. {
  192. snd_pcm_uframes_t avail;
  193. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  194. avail = snd_pcm_playback_avail(runtime);
  195. else
  196. avail = snd_pcm_capture_avail(runtime);
  197. if (avail > runtime->avail_max)
  198. runtime->avail_max = avail;
  199. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  200. if (avail >= runtime->buffer_size) {
  201. snd_pcm_drain_done(substream);
  202. return -EPIPE;
  203. }
  204. } else {
  205. if (avail >= runtime->stop_threshold) {
  206. xrun(substream);
  207. return -EPIPE;
  208. }
  209. }
  210. if (runtime->twake) {
  211. if (avail >= runtime->twake)
  212. wake_up(&runtime->tsleep);
  213. } else if (avail >= runtime->control->avail_min)
  214. wake_up(&runtime->sleep);
  215. return 0;
  216. }
  217. static void update_audio_tstamp(struct snd_pcm_substream *substream,
  218. struct timespec *curr_tstamp,
  219. struct timespec *audio_tstamp)
  220. {
  221. struct snd_pcm_runtime *runtime = substream->runtime;
  222. u64 audio_frames, audio_nsecs;
  223. struct timespec driver_tstamp;
  224. if (runtime->tstamp_mode != SNDRV_PCM_TSTAMP_ENABLE)
  225. return;
  226. if (!(substream->ops->get_time_info) ||
  227. (runtime->audio_tstamp_report.actual_type ==
  228. SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
  229. /*
  230. * provide audio timestamp derived from pointer position
  231. * add delay only if requested
  232. */
  233. audio_frames = runtime->hw_ptr_wrap + runtime->status->hw_ptr;
  234. if (runtime->audio_tstamp_config.report_delay) {
  235. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  236. audio_frames -= runtime->delay;
  237. else
  238. audio_frames += runtime->delay;
  239. }
  240. audio_nsecs = div_u64(audio_frames * 1000000000LL,
  241. runtime->rate);
  242. *audio_tstamp = ns_to_timespec(audio_nsecs);
  243. }
  244. if (!timespec_equal(&runtime->status->audio_tstamp, audio_tstamp)) {
  245. runtime->status->audio_tstamp = *audio_tstamp;
  246. runtime->status->tstamp = *curr_tstamp;
  247. }
  248. /*
  249. * re-take a driver timestamp to let apps detect if the reference tstamp
  250. * read by low-level hardware was provided with a delay
  251. */
  252. snd_pcm_gettime(substream->runtime, (struct timespec *)&driver_tstamp);
  253. runtime->driver_tstamp = driver_tstamp;
  254. }
  255. static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
  256. unsigned int in_interrupt)
  257. {
  258. struct snd_pcm_runtime *runtime = substream->runtime;
  259. snd_pcm_uframes_t pos;
  260. snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
  261. snd_pcm_sframes_t hdelta, delta;
  262. unsigned long jdelta;
  263. unsigned long curr_jiffies;
  264. struct timespec curr_tstamp;
  265. struct timespec audio_tstamp;
  266. int crossed_boundary = 0;
  267. old_hw_ptr = runtime->status->hw_ptr;
  268. /*
  269. * group pointer, time and jiffies reads to allow for more
  270. * accurate correlations/corrections.
  271. * The values are stored at the end of this routine after
  272. * corrections for hw_ptr position
  273. */
  274. pos = substream->ops->pointer(substream);
  275. curr_jiffies = jiffies;
  276. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
  277. if ((substream->ops->get_time_info) &&
  278. (runtime->audio_tstamp_config.type_requested != SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)) {
  279. substream->ops->get_time_info(substream, &curr_tstamp,
  280. &audio_tstamp,
  281. &runtime->audio_tstamp_config,
  282. &runtime->audio_tstamp_report);
  283. /* re-test in case tstamp type is not supported in hardware and was demoted to DEFAULT */
  284. if (runtime->audio_tstamp_report.actual_type == SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT)
  285. snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
  286. } else
  287. snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
  288. }
  289. if (pos == SNDRV_PCM_POS_XRUN) {
  290. xrun(substream);
  291. return -EPIPE;
  292. }
  293. if (pos >= runtime->buffer_size) {
  294. if (printk_ratelimit()) {
  295. char name[16];
  296. snd_pcm_debug_name(substream, name, sizeof(name));
  297. pcm_err(substream->pcm,
  298. "invalid position: %s, pos = %ld, buffer size = %ld, period size = %ld\n",
  299. name, pos, runtime->buffer_size,
  300. runtime->period_size);
  301. }
  302. pos = 0;
  303. }
  304. pos -= pos % runtime->min_align;
  305. trace_hwptr(substream, pos, in_interrupt);
  306. hw_base = runtime->hw_ptr_base;
  307. new_hw_ptr = hw_base + pos;
  308. if (in_interrupt) {
  309. /* we know that one period was processed */
  310. /* delta = "expected next hw_ptr" for in_interrupt != 0 */
  311. delta = runtime->hw_ptr_interrupt + runtime->period_size;
  312. if (delta > new_hw_ptr) {
  313. /* check for double acknowledged interrupts */
  314. hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
  315. if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
  316. hw_base += runtime->buffer_size;
  317. if (hw_base >= runtime->boundary) {
  318. hw_base = 0;
  319. crossed_boundary++;
  320. }
  321. new_hw_ptr = hw_base + pos;
  322. goto __delta;
  323. }
  324. }
  325. }
  326. /* new_hw_ptr might be lower than old_hw_ptr in case when */
  327. /* pointer crosses the end of the ring buffer */
  328. if (new_hw_ptr < old_hw_ptr) {
  329. hw_base += runtime->buffer_size;
  330. if (hw_base >= runtime->boundary) {
  331. hw_base = 0;
  332. crossed_boundary++;
  333. }
  334. new_hw_ptr = hw_base + pos;
  335. }
  336. __delta:
  337. delta = new_hw_ptr - old_hw_ptr;
  338. if (delta < 0)
  339. delta += runtime->boundary;
  340. if (runtime->no_period_wakeup) {
  341. snd_pcm_sframes_t xrun_threshold;
  342. /*
  343. * Without regular period interrupts, we have to check
  344. * the elapsed time to detect xruns.
  345. */
  346. jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
  347. if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
  348. goto no_delta_check;
  349. hdelta = jdelta - delta * HZ / runtime->rate;
  350. xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
  351. while (hdelta > xrun_threshold) {
  352. delta += runtime->buffer_size;
  353. hw_base += runtime->buffer_size;
  354. if (hw_base >= runtime->boundary) {
  355. hw_base = 0;
  356. crossed_boundary++;
  357. }
  358. new_hw_ptr = hw_base + pos;
  359. hdelta -= runtime->hw_ptr_buffer_jiffies;
  360. }
  361. goto no_delta_check;
  362. }
  363. /* something must be really wrong */
  364. if (delta >= runtime->buffer_size + runtime->period_size) {
  365. hw_ptr_error(substream, in_interrupt, "Unexpected hw_ptr",
  366. "(stream=%i, pos=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
  367. substream->stream, (long)pos,
  368. (long)new_hw_ptr, (long)old_hw_ptr);
  369. return 0;
  370. }
  371. /* Do jiffies check only in xrun_debug mode */
  372. if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
  373. goto no_jiffies_check;
  374. /* Skip the jiffies check for hardwares with BATCH flag.
  375. * Such hardware usually just increases the position at each IRQ,
  376. * thus it can't give any strange position.
  377. */
  378. if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
  379. goto no_jiffies_check;
  380. hdelta = delta;
  381. if (hdelta < runtime->delay)
  382. goto no_jiffies_check;
  383. hdelta -= runtime->delay;
  384. jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
  385. if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
  386. delta = jdelta /
  387. (((runtime->period_size * HZ) / runtime->rate)
  388. + HZ/100);
  389. /* move new_hw_ptr according jiffies not pos variable */
  390. new_hw_ptr = old_hw_ptr;
  391. hw_base = delta;
  392. /* use loop to avoid checks for delta overflows */
  393. /* the delta value is small or zero in most cases */
  394. while (delta > 0) {
  395. new_hw_ptr += runtime->period_size;
  396. if (new_hw_ptr >= runtime->boundary) {
  397. new_hw_ptr -= runtime->boundary;
  398. crossed_boundary--;
  399. }
  400. delta--;
  401. }
  402. /* align hw_base to buffer_size */
  403. hw_ptr_error(substream, in_interrupt, "hw_ptr skipping",
  404. "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
  405. (long)pos, (long)hdelta,
  406. (long)runtime->period_size, jdelta,
  407. ((hdelta * HZ) / runtime->rate), hw_base,
  408. (unsigned long)old_hw_ptr,
  409. (unsigned long)new_hw_ptr);
  410. /* reset values to proper state */
  411. delta = 0;
  412. hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
  413. }
  414. no_jiffies_check:
  415. if (delta > runtime->period_size + runtime->period_size / 2) {
  416. hw_ptr_error(substream, in_interrupt,
  417. "Lost interrupts?",
  418. "(stream=%i, delta=%ld, new_hw_ptr=%ld, old_hw_ptr=%ld)\n",
  419. substream->stream, (long)delta,
  420. (long)new_hw_ptr,
  421. (long)old_hw_ptr);
  422. }
  423. no_delta_check:
  424. if (runtime->status->hw_ptr == new_hw_ptr) {
  425. update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
  426. return 0;
  427. }
  428. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  429. runtime->silence_size > 0)
  430. snd_pcm_playback_silence(substream, new_hw_ptr);
  431. if (in_interrupt) {
  432. delta = new_hw_ptr - runtime->hw_ptr_interrupt;
  433. if (delta < 0)
  434. delta += runtime->boundary;
  435. delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
  436. runtime->hw_ptr_interrupt += delta;
  437. if (runtime->hw_ptr_interrupt >= runtime->boundary)
  438. runtime->hw_ptr_interrupt -= runtime->boundary;
  439. }
  440. runtime->hw_ptr_base = hw_base;
  441. runtime->status->hw_ptr = new_hw_ptr;
  442. runtime->hw_ptr_jiffies = curr_jiffies;
  443. if (crossed_boundary) {
  444. snd_BUG_ON(crossed_boundary != 1);
  445. runtime->hw_ptr_wrap += runtime->boundary;
  446. }
  447. update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
  448. return snd_pcm_update_state(substream, runtime);
  449. }
  450. /* CAUTION: call it with irq disabled */
  451. int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
  452. {
  453. return snd_pcm_update_hw_ptr0(substream, 0);
  454. }
  455. /**
  456. * snd_pcm_set_ops - set the PCM operators
  457. * @pcm: the pcm instance
  458. * @direction: stream direction, SNDRV_PCM_STREAM_XXX
  459. * @ops: the operator table
  460. *
  461. * Sets the given PCM operators to the pcm instance.
  462. */
  463. void snd_pcm_set_ops(struct snd_pcm *pcm, int direction,
  464. const struct snd_pcm_ops *ops)
  465. {
  466. struct snd_pcm_str *stream = &pcm->streams[direction];
  467. struct snd_pcm_substream *substream;
  468. for (substream = stream->substream; substream != NULL; substream = substream->next)
  469. substream->ops = ops;
  470. }
  471. EXPORT_SYMBOL(snd_pcm_set_ops);
  472. /**
  473. * snd_pcm_sync - set the PCM sync id
  474. * @substream: the pcm substream
  475. *
  476. * Sets the PCM sync identifier for the card.
  477. */
  478. void snd_pcm_set_sync(struct snd_pcm_substream *substream)
  479. {
  480. struct snd_pcm_runtime *runtime = substream->runtime;
  481. runtime->sync.id32[0] = substream->pcm->card->number;
  482. runtime->sync.id32[1] = -1;
  483. runtime->sync.id32[2] = -1;
  484. runtime->sync.id32[3] = -1;
  485. }
  486. EXPORT_SYMBOL(snd_pcm_set_sync);
  487. /*
  488. * Standard ioctl routine
  489. */
  490. static inline unsigned int div32(unsigned int a, unsigned int b,
  491. unsigned int *r)
  492. {
  493. if (b == 0) {
  494. *r = 0;
  495. return UINT_MAX;
  496. }
  497. *r = a % b;
  498. return a / b;
  499. }
  500. static inline unsigned int div_down(unsigned int a, unsigned int b)
  501. {
  502. if (b == 0)
  503. return UINT_MAX;
  504. return a / b;
  505. }
  506. static inline unsigned int div_up(unsigned int a, unsigned int b)
  507. {
  508. unsigned int r;
  509. unsigned int q;
  510. if (b == 0)
  511. return UINT_MAX;
  512. q = div32(a, b, &r);
  513. if (r)
  514. ++q;
  515. return q;
  516. }
  517. static inline unsigned int mul(unsigned int a, unsigned int b)
  518. {
  519. if (a == 0)
  520. return 0;
  521. if (div_down(UINT_MAX, a) < b)
  522. return UINT_MAX;
  523. return a * b;
  524. }
  525. static inline unsigned int muldiv32(unsigned int a, unsigned int b,
  526. unsigned int c, unsigned int *r)
  527. {
  528. u_int64_t n = (u_int64_t) a * b;
  529. if (c == 0) {
  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. struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
  1319. if (!snd_interval_single(i))
  1320. return 0;
  1321. if ((snd_interval_value(i) == width) ||
  1322. (width == 0 && snd_interval_value(i) > msbits))
  1323. params->msbits = min_not_zero(params->msbits, msbits);
  1324. return 0;
  1325. }
  1326. /**
  1327. * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
  1328. * @runtime: PCM runtime instance
  1329. * @cond: condition bits
  1330. * @width: sample bits width
  1331. * @msbits: msbits width
  1332. *
  1333. * This constraint will set the number of most significant bits (msbits) if a
  1334. * sample format with the specified width has been select. If width is set to 0
  1335. * the msbits will be set for any sample format with a width larger than the
  1336. * specified msbits.
  1337. *
  1338. * Return: Zero if successful, or a negative error code on failure.
  1339. */
  1340. int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
  1341. unsigned int cond,
  1342. unsigned int width,
  1343. unsigned int msbits)
  1344. {
  1345. unsigned long l = (msbits << 16) | width;
  1346. return snd_pcm_hw_rule_add(runtime, cond, -1,
  1347. snd_pcm_hw_rule_msbits,
  1348. (void*) l,
  1349. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1350. }
  1351. EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
  1352. static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
  1353. struct snd_pcm_hw_rule *rule)
  1354. {
  1355. unsigned long step = (unsigned long) rule->private;
  1356. return snd_interval_step(hw_param_interval(params, rule->var), step);
  1357. }
  1358. /**
  1359. * snd_pcm_hw_constraint_step - add a hw constraint step rule
  1360. * @runtime: PCM runtime instance
  1361. * @cond: condition bits
  1362. * @var: hw_params variable to apply the step constraint
  1363. * @step: step size
  1364. *
  1365. * Return: Zero if successful, or a negative error code on failure.
  1366. */
  1367. int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
  1368. unsigned int cond,
  1369. snd_pcm_hw_param_t var,
  1370. unsigned long step)
  1371. {
  1372. return snd_pcm_hw_rule_add(runtime, cond, var,
  1373. snd_pcm_hw_rule_step, (void *) step,
  1374. var, -1);
  1375. }
  1376. EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
  1377. static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
  1378. {
  1379. static unsigned int pow2_sizes[] = {
  1380. 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
  1381. 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
  1382. 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
  1383. 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
  1384. };
  1385. return snd_interval_list(hw_param_interval(params, rule->var),
  1386. ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
  1387. }
  1388. /**
  1389. * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
  1390. * @runtime: PCM runtime instance
  1391. * @cond: condition bits
  1392. * @var: hw_params variable to apply the power-of-2 constraint
  1393. *
  1394. * Return: Zero if successful, or a negative error code on failure.
  1395. */
  1396. int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
  1397. unsigned int cond,
  1398. snd_pcm_hw_param_t var)
  1399. {
  1400. return snd_pcm_hw_rule_add(runtime, cond, var,
  1401. snd_pcm_hw_rule_pow2, NULL,
  1402. var, -1);
  1403. }
  1404. EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
  1405. static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
  1406. struct snd_pcm_hw_rule *rule)
  1407. {
  1408. unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
  1409. struct snd_interval *rate;
  1410. rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  1411. return snd_interval_list(rate, 1, &base_rate, 0);
  1412. }
  1413. /**
  1414. * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
  1415. * @runtime: PCM runtime instance
  1416. * @base_rate: the rate at which the hardware does not resample
  1417. *
  1418. * Return: Zero if successful, or a negative error code on failure.
  1419. */
  1420. int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
  1421. unsigned int base_rate)
  1422. {
  1423. return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
  1424. SNDRV_PCM_HW_PARAM_RATE,
  1425. snd_pcm_hw_rule_noresample_func,
  1426. (void *)(uintptr_t)base_rate,
  1427. SNDRV_PCM_HW_PARAM_RATE, -1);
  1428. }
  1429. EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
  1430. static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
  1431. snd_pcm_hw_param_t var)
  1432. {
  1433. if (hw_is_mask(var)) {
  1434. snd_mask_any(hw_param_mask(params, var));
  1435. params->cmask |= 1 << var;
  1436. params->rmask |= 1 << var;
  1437. return;
  1438. }
  1439. if (hw_is_interval(var)) {
  1440. snd_interval_any(hw_param_interval(params, var));
  1441. params->cmask |= 1 << var;
  1442. params->rmask |= 1 << var;
  1443. return;
  1444. }
  1445. snd_BUG();
  1446. }
  1447. void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
  1448. {
  1449. unsigned int k;
  1450. memset(params, 0, sizeof(*params));
  1451. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
  1452. _snd_pcm_hw_param_any(params, k);
  1453. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
  1454. _snd_pcm_hw_param_any(params, k);
  1455. params->info = ~0U;
  1456. }
  1457. EXPORT_SYMBOL(_snd_pcm_hw_params_any);
  1458. /**
  1459. * snd_pcm_hw_param_value - return @params field @var value
  1460. * @params: the hw_params instance
  1461. * @var: parameter to retrieve
  1462. * @dir: pointer to the direction (-1,0,1) or %NULL
  1463. *
  1464. * Return: The value for field @var if it's fixed in configuration space
  1465. * defined by @params. -%EINVAL otherwise.
  1466. */
  1467. int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
  1468. snd_pcm_hw_param_t var, int *dir)
  1469. {
  1470. if (hw_is_mask(var)) {
  1471. const struct snd_mask *mask = hw_param_mask_c(params, var);
  1472. if (!snd_mask_single(mask))
  1473. return -EINVAL;
  1474. if (dir)
  1475. *dir = 0;
  1476. return snd_mask_value(mask);
  1477. }
  1478. if (hw_is_interval(var)) {
  1479. const struct snd_interval *i = hw_param_interval_c(params, var);
  1480. if (!snd_interval_single(i))
  1481. return -EINVAL;
  1482. if (dir)
  1483. *dir = i->openmin;
  1484. return snd_interval_value(i);
  1485. }
  1486. return -EINVAL;
  1487. }
  1488. EXPORT_SYMBOL(snd_pcm_hw_param_value);
  1489. void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
  1490. snd_pcm_hw_param_t var)
  1491. {
  1492. if (hw_is_mask(var)) {
  1493. snd_mask_none(hw_param_mask(params, var));
  1494. params->cmask |= 1 << var;
  1495. params->rmask |= 1 << var;
  1496. } else if (hw_is_interval(var)) {
  1497. snd_interval_none(hw_param_interval(params, var));
  1498. params->cmask |= 1 << var;
  1499. params->rmask |= 1 << var;
  1500. } else {
  1501. snd_BUG();
  1502. }
  1503. }
  1504. EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
  1505. static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
  1506. snd_pcm_hw_param_t var)
  1507. {
  1508. int changed;
  1509. if (hw_is_mask(var))
  1510. changed = snd_mask_refine_first(hw_param_mask(params, var));
  1511. else if (hw_is_interval(var))
  1512. changed = snd_interval_refine_first(hw_param_interval(params, var));
  1513. else
  1514. return -EINVAL;
  1515. if (changed) {
  1516. params->cmask |= 1 << var;
  1517. params->rmask |= 1 << var;
  1518. }
  1519. return changed;
  1520. }
  1521. /**
  1522. * snd_pcm_hw_param_first - refine config space and return minimum value
  1523. * @pcm: PCM instance
  1524. * @params: the hw_params instance
  1525. * @var: parameter to retrieve
  1526. * @dir: pointer to the direction (-1,0,1) or %NULL
  1527. *
  1528. * Inside configuration space defined by @params remove from @var all
  1529. * values > minimum. Reduce configuration space accordingly.
  1530. *
  1531. * Return: The minimum, or a negative error code on failure.
  1532. */
  1533. int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
  1534. struct snd_pcm_hw_params *params,
  1535. snd_pcm_hw_param_t var, int *dir)
  1536. {
  1537. int changed = _snd_pcm_hw_param_first(params, var);
  1538. if (changed < 0)
  1539. return changed;
  1540. if (params->rmask) {
  1541. int err = snd_pcm_hw_refine(pcm, params);
  1542. if (err < 0)
  1543. return err;
  1544. }
  1545. return snd_pcm_hw_param_value(params, var, dir);
  1546. }
  1547. EXPORT_SYMBOL(snd_pcm_hw_param_first);
  1548. static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
  1549. snd_pcm_hw_param_t var)
  1550. {
  1551. int changed;
  1552. if (hw_is_mask(var))
  1553. changed = snd_mask_refine_last(hw_param_mask(params, var));
  1554. else if (hw_is_interval(var))
  1555. changed = snd_interval_refine_last(hw_param_interval(params, var));
  1556. else
  1557. return -EINVAL;
  1558. if (changed) {
  1559. params->cmask |= 1 << var;
  1560. params->rmask |= 1 << var;
  1561. }
  1562. return changed;
  1563. }
  1564. /**
  1565. * snd_pcm_hw_param_last - refine config space and return maximum value
  1566. * @pcm: PCM instance
  1567. * @params: the hw_params instance
  1568. * @var: parameter to retrieve
  1569. * @dir: pointer to the direction (-1,0,1) or %NULL
  1570. *
  1571. * Inside configuration space defined by @params remove from @var all
  1572. * values < maximum. Reduce configuration space accordingly.
  1573. *
  1574. * Return: The maximum, or a negative error code on failure.
  1575. */
  1576. int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
  1577. struct snd_pcm_hw_params *params,
  1578. snd_pcm_hw_param_t var, int *dir)
  1579. {
  1580. int changed = _snd_pcm_hw_param_last(params, var);
  1581. if (changed < 0)
  1582. return changed;
  1583. if (params->rmask) {
  1584. int err = snd_pcm_hw_refine(pcm, params);
  1585. if (err < 0)
  1586. return err;
  1587. }
  1588. return snd_pcm_hw_param_value(params, var, dir);
  1589. }
  1590. EXPORT_SYMBOL(snd_pcm_hw_param_last);
  1591. /**
  1592. * snd_pcm_hw_param_choose - choose a configuration defined by @params
  1593. * @pcm: PCM instance
  1594. * @params: the hw_params instance
  1595. *
  1596. * Choose one configuration from configuration space defined by @params.
  1597. * The configuration chosen is that obtained fixing in this order:
  1598. * first access, first format, first subformat, min channels,
  1599. * min rate, min period time, max buffer size, min tick time
  1600. *
  1601. * Return: Zero if successful, or a negative error code on failure.
  1602. */
  1603. int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
  1604. struct snd_pcm_hw_params *params)
  1605. {
  1606. static int vars[] = {
  1607. SNDRV_PCM_HW_PARAM_ACCESS,
  1608. SNDRV_PCM_HW_PARAM_FORMAT,
  1609. SNDRV_PCM_HW_PARAM_SUBFORMAT,
  1610. SNDRV_PCM_HW_PARAM_CHANNELS,
  1611. SNDRV_PCM_HW_PARAM_RATE,
  1612. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1613. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1614. SNDRV_PCM_HW_PARAM_TICK_TIME,
  1615. -1
  1616. };
  1617. int err, *v;
  1618. for (v = vars; *v != -1; v++) {
  1619. if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
  1620. err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
  1621. else
  1622. err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
  1623. if (snd_BUG_ON(err < 0))
  1624. return err;
  1625. }
  1626. return 0;
  1627. }
  1628. static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
  1629. void *arg)
  1630. {
  1631. struct snd_pcm_runtime *runtime = substream->runtime;
  1632. unsigned long flags;
  1633. snd_pcm_stream_lock_irqsave(substream, flags);
  1634. if (snd_pcm_running(substream) &&
  1635. snd_pcm_update_hw_ptr(substream) >= 0)
  1636. runtime->status->hw_ptr %= runtime->buffer_size;
  1637. else {
  1638. runtime->status->hw_ptr = 0;
  1639. runtime->hw_ptr_wrap = 0;
  1640. }
  1641. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1642. return 0;
  1643. }
  1644. static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
  1645. void *arg)
  1646. {
  1647. struct snd_pcm_channel_info *info = arg;
  1648. struct snd_pcm_runtime *runtime = substream->runtime;
  1649. int width;
  1650. if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
  1651. info->offset = -1;
  1652. return 0;
  1653. }
  1654. width = snd_pcm_format_physical_width(runtime->format);
  1655. if (width < 0)
  1656. return width;
  1657. info->offset = 0;
  1658. switch (runtime->access) {
  1659. case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
  1660. case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
  1661. info->first = info->channel * width;
  1662. info->step = runtime->channels * width;
  1663. break;
  1664. case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
  1665. case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
  1666. {
  1667. size_t size = runtime->dma_bytes / runtime->channels;
  1668. info->first = info->channel * size * 8;
  1669. info->step = width;
  1670. break;
  1671. }
  1672. default:
  1673. snd_BUG();
  1674. break;
  1675. }
  1676. return 0;
  1677. }
  1678. static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
  1679. void *arg)
  1680. {
  1681. struct snd_pcm_hw_params *params = arg;
  1682. snd_pcm_format_t format;
  1683. int channels;
  1684. ssize_t frame_size;
  1685. params->fifo_size = substream->runtime->hw.fifo_size;
  1686. if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
  1687. format = params_format(params);
  1688. channels = params_channels(params);
  1689. frame_size = snd_pcm_format_size(format, channels);
  1690. if (frame_size > 0)
  1691. params->fifo_size /= (unsigned)frame_size;
  1692. }
  1693. return 0;
  1694. }
  1695. /**
  1696. * snd_pcm_lib_ioctl - a generic PCM ioctl callback
  1697. * @substream: the pcm substream instance
  1698. * @cmd: ioctl command
  1699. * @arg: ioctl argument
  1700. *
  1701. * Processes the generic ioctl commands for PCM.
  1702. * Can be passed as the ioctl callback for PCM ops.
  1703. *
  1704. * Return: Zero if successful, or a negative error code on failure.
  1705. */
  1706. int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  1707. unsigned int cmd, void *arg)
  1708. {
  1709. switch (cmd) {
  1710. case SNDRV_PCM_IOCTL1_INFO:
  1711. return 0;
  1712. case SNDRV_PCM_IOCTL1_RESET:
  1713. return snd_pcm_lib_ioctl_reset(substream, arg);
  1714. case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
  1715. return snd_pcm_lib_ioctl_channel_info(substream, arg);
  1716. case SNDRV_PCM_IOCTL1_FIFO_SIZE:
  1717. return snd_pcm_lib_ioctl_fifo_size(substream, arg);
  1718. }
  1719. return -ENXIO;
  1720. }
  1721. EXPORT_SYMBOL(snd_pcm_lib_ioctl);
  1722. /**
  1723. * snd_pcm_period_elapsed - update the pcm status for the next period
  1724. * @substream: the pcm substream instance
  1725. *
  1726. * This function is called from the interrupt handler when the
  1727. * PCM has processed the period size. It will update the current
  1728. * pointer, wake up sleepers, etc.
  1729. *
  1730. * Even if more than one periods have elapsed since the last call, you
  1731. * have to call this only once.
  1732. */
  1733. void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
  1734. {
  1735. struct snd_pcm_runtime *runtime;
  1736. unsigned long flags;
  1737. if (PCM_RUNTIME_CHECK(substream))
  1738. return;
  1739. runtime = substream->runtime;
  1740. snd_pcm_stream_lock_irqsave(substream, flags);
  1741. if (!snd_pcm_running(substream) ||
  1742. snd_pcm_update_hw_ptr0(substream, 1) < 0)
  1743. goto _end;
  1744. #ifdef CONFIG_SND_PCM_TIMER
  1745. if (substream->timer_running)
  1746. snd_timer_interrupt(substream->timer, 1);
  1747. #endif
  1748. _end:
  1749. kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
  1750. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1751. }
  1752. EXPORT_SYMBOL(snd_pcm_period_elapsed);
  1753. /*
  1754. * Wait until avail_min data becomes available
  1755. * Returns a negative error code if any error occurs during operation.
  1756. * The available space is stored on availp. When err = 0 and avail = 0
  1757. * on the capture stream, it indicates the stream is in DRAINING state.
  1758. */
  1759. static int wait_for_avail(struct snd_pcm_substream *substream,
  1760. snd_pcm_uframes_t *availp)
  1761. {
  1762. struct snd_pcm_runtime *runtime = substream->runtime;
  1763. int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  1764. wait_queue_t wait;
  1765. int err = 0;
  1766. snd_pcm_uframes_t avail = 0;
  1767. long wait_time, tout;
  1768. init_waitqueue_entry(&wait, current);
  1769. set_current_state(TASK_INTERRUPTIBLE);
  1770. add_wait_queue(&runtime->tsleep, &wait);
  1771. if (runtime->no_period_wakeup)
  1772. wait_time = MAX_SCHEDULE_TIMEOUT;
  1773. else {
  1774. wait_time = 10;
  1775. if (runtime->rate) {
  1776. long t = runtime->period_size * 2 / runtime->rate;
  1777. wait_time = max(t, wait_time);
  1778. }
  1779. wait_time = msecs_to_jiffies(wait_time * 1000);
  1780. }
  1781. for (;;) {
  1782. if (signal_pending(current)) {
  1783. err = -ERESTARTSYS;
  1784. break;
  1785. }
  1786. /*
  1787. * We need to check if space became available already
  1788. * (and thus the wakeup happened already) first to close
  1789. * the race of space already having become available.
  1790. * This check must happen after been added to the waitqueue
  1791. * and having current state be INTERRUPTIBLE.
  1792. */
  1793. if (is_playback)
  1794. avail = snd_pcm_playback_avail(runtime);
  1795. else
  1796. avail = snd_pcm_capture_avail(runtime);
  1797. if (avail >= runtime->twake)
  1798. break;
  1799. snd_pcm_stream_unlock_irq(substream);
  1800. tout = schedule_timeout(wait_time);
  1801. snd_pcm_stream_lock_irq(substream);
  1802. set_current_state(TASK_INTERRUPTIBLE);
  1803. switch (runtime->status->state) {
  1804. case SNDRV_PCM_STATE_SUSPENDED:
  1805. err = -ESTRPIPE;
  1806. goto _endloop;
  1807. case SNDRV_PCM_STATE_XRUN:
  1808. err = -EPIPE;
  1809. goto _endloop;
  1810. case SNDRV_PCM_STATE_DRAINING:
  1811. if (is_playback)
  1812. err = -EPIPE;
  1813. else
  1814. avail = 0; /* indicate draining */
  1815. goto _endloop;
  1816. case SNDRV_PCM_STATE_OPEN:
  1817. case SNDRV_PCM_STATE_SETUP:
  1818. case SNDRV_PCM_STATE_DISCONNECTED:
  1819. err = -EBADFD;
  1820. goto _endloop;
  1821. case SNDRV_PCM_STATE_PAUSED:
  1822. continue;
  1823. }
  1824. if (!tout) {
  1825. pcm_dbg(substream->pcm,
  1826. "%s write error (DMA or IRQ trouble?)\n",
  1827. is_playback ? "playback" : "capture");
  1828. err = -EIO;
  1829. break;
  1830. }
  1831. }
  1832. _endloop:
  1833. set_current_state(TASK_RUNNING);
  1834. remove_wait_queue(&runtime->tsleep, &wait);
  1835. *availp = avail;
  1836. return err;
  1837. }
  1838. static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
  1839. unsigned int hwoff,
  1840. unsigned long data, unsigned int off,
  1841. snd_pcm_uframes_t frames)
  1842. {
  1843. struct snd_pcm_runtime *runtime = substream->runtime;
  1844. int err;
  1845. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  1846. if (substream->ops->copy) {
  1847. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  1848. return err;
  1849. } else {
  1850. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  1851. if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
  1852. return -EFAULT;
  1853. }
  1854. return 0;
  1855. }
  1856. typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
  1857. unsigned long data, unsigned int off,
  1858. snd_pcm_uframes_t size);
  1859. static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
  1860. unsigned long data,
  1861. snd_pcm_uframes_t size,
  1862. int nonblock,
  1863. transfer_f transfer)
  1864. {
  1865. struct snd_pcm_runtime *runtime = substream->runtime;
  1866. snd_pcm_uframes_t xfer = 0;
  1867. snd_pcm_uframes_t offset = 0;
  1868. snd_pcm_uframes_t avail;
  1869. int err = 0;
  1870. if (size == 0)
  1871. return 0;
  1872. snd_pcm_stream_lock_irq(substream);
  1873. switch (runtime->status->state) {
  1874. case SNDRV_PCM_STATE_PREPARED:
  1875. case SNDRV_PCM_STATE_RUNNING:
  1876. case SNDRV_PCM_STATE_PAUSED:
  1877. break;
  1878. case SNDRV_PCM_STATE_XRUN:
  1879. err = -EPIPE;
  1880. goto _end_unlock;
  1881. case SNDRV_PCM_STATE_SUSPENDED:
  1882. err = -ESTRPIPE;
  1883. goto _end_unlock;
  1884. default:
  1885. err = -EBADFD;
  1886. goto _end_unlock;
  1887. }
  1888. runtime->twake = runtime->control->avail_min ? : 1;
  1889. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1890. snd_pcm_update_hw_ptr(substream);
  1891. avail = snd_pcm_playback_avail(runtime);
  1892. while (size > 0) {
  1893. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  1894. snd_pcm_uframes_t cont;
  1895. if (!avail) {
  1896. if (nonblock) {
  1897. err = -EAGAIN;
  1898. goto _end_unlock;
  1899. }
  1900. runtime->twake = min_t(snd_pcm_uframes_t, size,
  1901. runtime->control->avail_min ? : 1);
  1902. err = wait_for_avail(substream, &avail);
  1903. if (err < 0)
  1904. goto _end_unlock;
  1905. }
  1906. frames = size > avail ? avail : size;
  1907. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  1908. if (frames > cont)
  1909. frames = cont;
  1910. if (snd_BUG_ON(!frames)) {
  1911. runtime->twake = 0;
  1912. snd_pcm_stream_unlock_irq(substream);
  1913. return -EINVAL;
  1914. }
  1915. appl_ptr = runtime->control->appl_ptr;
  1916. appl_ofs = appl_ptr % runtime->buffer_size;
  1917. snd_pcm_stream_unlock_irq(substream);
  1918. err = transfer(substream, appl_ofs, data, offset, frames);
  1919. snd_pcm_stream_lock_irq(substream);
  1920. if (err < 0)
  1921. goto _end_unlock;
  1922. switch (runtime->status->state) {
  1923. case SNDRV_PCM_STATE_XRUN:
  1924. err = -EPIPE;
  1925. goto _end_unlock;
  1926. case SNDRV_PCM_STATE_SUSPENDED:
  1927. err = -ESTRPIPE;
  1928. goto _end_unlock;
  1929. default:
  1930. break;
  1931. }
  1932. appl_ptr += frames;
  1933. if (appl_ptr >= runtime->boundary)
  1934. appl_ptr -= runtime->boundary;
  1935. runtime->control->appl_ptr = appl_ptr;
  1936. if (substream->ops->ack)
  1937. substream->ops->ack(substream);
  1938. offset += frames;
  1939. size -= frames;
  1940. xfer += frames;
  1941. avail -= frames;
  1942. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
  1943. snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
  1944. err = snd_pcm_start(substream);
  1945. if (err < 0)
  1946. goto _end_unlock;
  1947. }
  1948. }
  1949. _end_unlock:
  1950. runtime->twake = 0;
  1951. if (xfer > 0 && err >= 0)
  1952. snd_pcm_update_state(substream, runtime);
  1953. snd_pcm_stream_unlock_irq(substream);
  1954. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  1955. }
  1956. /* sanity-check for read/write methods */
  1957. static int pcm_sanity_check(struct snd_pcm_substream *substream)
  1958. {
  1959. struct snd_pcm_runtime *runtime;
  1960. if (PCM_RUNTIME_CHECK(substream))
  1961. return -ENXIO;
  1962. runtime = substream->runtime;
  1963. if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
  1964. return -EINVAL;
  1965. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1966. return -EBADFD;
  1967. return 0;
  1968. }
  1969. snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
  1970. {
  1971. struct snd_pcm_runtime *runtime;
  1972. int nonblock;
  1973. int err;
  1974. err = pcm_sanity_check(substream);
  1975. if (err < 0)
  1976. return err;
  1977. runtime = substream->runtime;
  1978. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1979. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
  1980. runtime->channels > 1)
  1981. return -EINVAL;
  1982. return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
  1983. snd_pcm_lib_write_transfer);
  1984. }
  1985. EXPORT_SYMBOL(snd_pcm_lib_write);
  1986. static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
  1987. unsigned int hwoff,
  1988. unsigned long data, unsigned int off,
  1989. snd_pcm_uframes_t frames)
  1990. {
  1991. struct snd_pcm_runtime *runtime = substream->runtime;
  1992. int err;
  1993. void __user **bufs = (void __user **)data;
  1994. int channels = runtime->channels;
  1995. int c;
  1996. if (substream->ops->copy) {
  1997. if (snd_BUG_ON(!substream->ops->silence))
  1998. return -EINVAL;
  1999. for (c = 0; c < channels; ++c, ++bufs) {
  2000. if (*bufs == NULL) {
  2001. if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
  2002. return err;
  2003. } else {
  2004. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  2005. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  2006. return err;
  2007. }
  2008. }
  2009. } else {
  2010. /* default transfer behaviour */
  2011. size_t dma_csize = runtime->dma_bytes / channels;
  2012. for (c = 0; c < channels; ++c, ++bufs) {
  2013. char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  2014. if (*bufs == NULL) {
  2015. snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
  2016. } else {
  2017. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  2018. if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
  2019. return -EFAULT;
  2020. }
  2021. }
  2022. }
  2023. return 0;
  2024. }
  2025. snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
  2026. void __user **bufs,
  2027. snd_pcm_uframes_t frames)
  2028. {
  2029. struct snd_pcm_runtime *runtime;
  2030. int nonblock;
  2031. int err;
  2032. err = pcm_sanity_check(substream);
  2033. if (err < 0)
  2034. return err;
  2035. runtime = substream->runtime;
  2036. nonblock = !!(substream->f_flags & O_NONBLOCK);
  2037. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  2038. return -EINVAL;
  2039. return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
  2040. nonblock, snd_pcm_lib_writev_transfer);
  2041. }
  2042. EXPORT_SYMBOL(snd_pcm_lib_writev);
  2043. static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
  2044. unsigned int hwoff,
  2045. unsigned long data, unsigned int off,
  2046. snd_pcm_uframes_t frames)
  2047. {
  2048. struct snd_pcm_runtime *runtime = substream->runtime;
  2049. int err;
  2050. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  2051. if (substream->ops->copy) {
  2052. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  2053. return err;
  2054. } else {
  2055. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  2056. if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
  2057. return -EFAULT;
  2058. }
  2059. return 0;
  2060. }
  2061. static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
  2062. unsigned long data,
  2063. snd_pcm_uframes_t size,
  2064. int nonblock,
  2065. transfer_f transfer)
  2066. {
  2067. struct snd_pcm_runtime *runtime = substream->runtime;
  2068. snd_pcm_uframes_t xfer = 0;
  2069. snd_pcm_uframes_t offset = 0;
  2070. snd_pcm_uframes_t avail;
  2071. int err = 0;
  2072. if (size == 0)
  2073. return 0;
  2074. snd_pcm_stream_lock_irq(substream);
  2075. switch (runtime->status->state) {
  2076. case SNDRV_PCM_STATE_PREPARED:
  2077. if (size >= runtime->start_threshold) {
  2078. err = snd_pcm_start(substream);
  2079. if (err < 0)
  2080. goto _end_unlock;
  2081. }
  2082. break;
  2083. case SNDRV_PCM_STATE_DRAINING:
  2084. case SNDRV_PCM_STATE_RUNNING:
  2085. case SNDRV_PCM_STATE_PAUSED:
  2086. break;
  2087. case SNDRV_PCM_STATE_XRUN:
  2088. err = -EPIPE;
  2089. goto _end_unlock;
  2090. case SNDRV_PCM_STATE_SUSPENDED:
  2091. err = -ESTRPIPE;
  2092. goto _end_unlock;
  2093. default:
  2094. err = -EBADFD;
  2095. goto _end_unlock;
  2096. }
  2097. runtime->twake = runtime->control->avail_min ? : 1;
  2098. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  2099. snd_pcm_update_hw_ptr(substream);
  2100. avail = snd_pcm_capture_avail(runtime);
  2101. while (size > 0) {
  2102. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  2103. snd_pcm_uframes_t cont;
  2104. if (!avail) {
  2105. if (runtime->status->state ==
  2106. SNDRV_PCM_STATE_DRAINING) {
  2107. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  2108. goto _end_unlock;
  2109. }
  2110. if (nonblock) {
  2111. err = -EAGAIN;
  2112. goto _end_unlock;
  2113. }
  2114. runtime->twake = min_t(snd_pcm_uframes_t, size,
  2115. runtime->control->avail_min ? : 1);
  2116. err = wait_for_avail(substream, &avail);
  2117. if (err < 0)
  2118. goto _end_unlock;
  2119. if (!avail)
  2120. continue; /* draining */
  2121. }
  2122. frames = size > avail ? avail : size;
  2123. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  2124. if (frames > cont)
  2125. frames = cont;
  2126. if (snd_BUG_ON(!frames)) {
  2127. runtime->twake = 0;
  2128. snd_pcm_stream_unlock_irq(substream);
  2129. return -EINVAL;
  2130. }
  2131. appl_ptr = runtime->control->appl_ptr;
  2132. appl_ofs = appl_ptr % runtime->buffer_size;
  2133. snd_pcm_stream_unlock_irq(substream);
  2134. err = transfer(substream, appl_ofs, data, offset, frames);
  2135. snd_pcm_stream_lock_irq(substream);
  2136. if (err < 0)
  2137. goto _end_unlock;
  2138. switch (runtime->status->state) {
  2139. case SNDRV_PCM_STATE_XRUN:
  2140. err = -EPIPE;
  2141. goto _end_unlock;
  2142. case SNDRV_PCM_STATE_SUSPENDED:
  2143. err = -ESTRPIPE;
  2144. goto _end_unlock;
  2145. default:
  2146. break;
  2147. }
  2148. appl_ptr += frames;
  2149. if (appl_ptr >= runtime->boundary)
  2150. appl_ptr -= runtime->boundary;
  2151. runtime->control->appl_ptr = appl_ptr;
  2152. if (substream->ops->ack)
  2153. substream->ops->ack(substream);
  2154. offset += frames;
  2155. size -= frames;
  2156. xfer += frames;
  2157. avail -= frames;
  2158. }
  2159. _end_unlock:
  2160. runtime->twake = 0;
  2161. if (xfer > 0 && err >= 0)
  2162. snd_pcm_update_state(substream, runtime);
  2163. snd_pcm_stream_unlock_irq(substream);
  2164. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  2165. }
  2166. snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
  2167. {
  2168. struct snd_pcm_runtime *runtime;
  2169. int nonblock;
  2170. int err;
  2171. err = pcm_sanity_check(substream);
  2172. if (err < 0)
  2173. return err;
  2174. runtime = substream->runtime;
  2175. nonblock = !!(substream->f_flags & O_NONBLOCK);
  2176. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
  2177. return -EINVAL;
  2178. return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
  2179. }
  2180. EXPORT_SYMBOL(snd_pcm_lib_read);
  2181. static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
  2182. unsigned int hwoff,
  2183. unsigned long data, unsigned int off,
  2184. snd_pcm_uframes_t frames)
  2185. {
  2186. struct snd_pcm_runtime *runtime = substream->runtime;
  2187. int err;
  2188. void __user **bufs = (void __user **)data;
  2189. int channels = runtime->channels;
  2190. int c;
  2191. if (substream->ops->copy) {
  2192. for (c = 0; c < channels; ++c, ++bufs) {
  2193. char __user *buf;
  2194. if (*bufs == NULL)
  2195. continue;
  2196. buf = *bufs + samples_to_bytes(runtime, off);
  2197. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  2198. return err;
  2199. }
  2200. } else {
  2201. snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
  2202. for (c = 0; c < channels; ++c, ++bufs) {
  2203. char *hwbuf;
  2204. char __user *buf;
  2205. if (*bufs == NULL)
  2206. continue;
  2207. hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  2208. buf = *bufs + samples_to_bytes(runtime, off);
  2209. if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
  2210. return -EFAULT;
  2211. }
  2212. }
  2213. return 0;
  2214. }
  2215. snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
  2216. void __user **bufs,
  2217. snd_pcm_uframes_t frames)
  2218. {
  2219. struct snd_pcm_runtime *runtime;
  2220. int nonblock;
  2221. int err;
  2222. err = pcm_sanity_check(substream);
  2223. if (err < 0)
  2224. return err;
  2225. runtime = substream->runtime;
  2226. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2227. return -EBADFD;
  2228. nonblock = !!(substream->f_flags & O_NONBLOCK);
  2229. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  2230. return -EINVAL;
  2231. return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
  2232. }
  2233. EXPORT_SYMBOL(snd_pcm_lib_readv);
  2234. /*
  2235. * standard channel mapping helpers
  2236. */
  2237. /* default channel maps for multi-channel playbacks, up to 8 channels */
  2238. const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
  2239. { .channels = 1,
  2240. .map = { SNDRV_CHMAP_MONO } },
  2241. { .channels = 2,
  2242. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  2243. { .channels = 4,
  2244. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2245. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  2246. { .channels = 6,
  2247. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2248. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  2249. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
  2250. { .channels = 8,
  2251. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2252. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  2253. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  2254. SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
  2255. { }
  2256. };
  2257. EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
  2258. /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
  2259. const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
  2260. { .channels = 1,
  2261. .map = { SNDRV_CHMAP_MONO } },
  2262. { .channels = 2,
  2263. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  2264. { .channels = 4,
  2265. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2266. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  2267. { .channels = 6,
  2268. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2269. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  2270. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  2271. { .channels = 8,
  2272. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2273. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  2274. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  2275. SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
  2276. { }
  2277. };
  2278. EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
  2279. static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
  2280. {
  2281. if (ch > info->max_channels)
  2282. return false;
  2283. return !info->channel_mask || (info->channel_mask & (1U << ch));
  2284. }
  2285. static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
  2286. struct snd_ctl_elem_info *uinfo)
  2287. {
  2288. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2289. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2290. uinfo->count = 0;
  2291. uinfo->count = info->max_channels;
  2292. uinfo->value.integer.min = 0;
  2293. uinfo->value.integer.max = SNDRV_CHMAP_LAST;
  2294. return 0;
  2295. }
  2296. /* get callback for channel map ctl element
  2297. * stores the channel position firstly matching with the current channels
  2298. */
  2299. static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  2300. struct snd_ctl_elem_value *ucontrol)
  2301. {
  2302. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2303. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  2304. struct snd_pcm_substream *substream;
  2305. const struct snd_pcm_chmap_elem *map;
  2306. if (!info->chmap)
  2307. return -EINVAL;
  2308. substream = snd_pcm_chmap_substream(info, idx);
  2309. if (!substream)
  2310. return -ENODEV;
  2311. memset(ucontrol->value.integer.value, 0,
  2312. sizeof(ucontrol->value.integer.value));
  2313. if (!substream->runtime)
  2314. return 0; /* no channels set */
  2315. for (map = info->chmap; map->channels; map++) {
  2316. int i;
  2317. if (map->channels == substream->runtime->channels &&
  2318. valid_chmap_channels(info, map->channels)) {
  2319. for (i = 0; i < map->channels; i++)
  2320. ucontrol->value.integer.value[i] = map->map[i];
  2321. return 0;
  2322. }
  2323. }
  2324. return -EINVAL;
  2325. }
  2326. /* tlv callback for channel map ctl element
  2327. * expands the pre-defined channel maps in a form of TLV
  2328. */
  2329. static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  2330. unsigned int size, unsigned int __user *tlv)
  2331. {
  2332. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2333. const struct snd_pcm_chmap_elem *map;
  2334. unsigned int __user *dst;
  2335. int c, count = 0;
  2336. if (!info->chmap)
  2337. return -EINVAL;
  2338. if (size < 8)
  2339. return -ENOMEM;
  2340. if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
  2341. return -EFAULT;
  2342. size -= 8;
  2343. dst = tlv + 2;
  2344. for (map = info->chmap; map->channels; map++) {
  2345. int chs_bytes = map->channels * 4;
  2346. if (!valid_chmap_channels(info, map->channels))
  2347. continue;
  2348. if (size < 8)
  2349. return -ENOMEM;
  2350. if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
  2351. put_user(chs_bytes, dst + 1))
  2352. return -EFAULT;
  2353. dst += 2;
  2354. size -= 8;
  2355. count += 8;
  2356. if (size < chs_bytes)
  2357. return -ENOMEM;
  2358. size -= chs_bytes;
  2359. count += chs_bytes;
  2360. for (c = 0; c < map->channels; c++) {
  2361. if (put_user(map->map[c], dst))
  2362. return -EFAULT;
  2363. dst++;
  2364. }
  2365. }
  2366. if (put_user(count, tlv + 1))
  2367. return -EFAULT;
  2368. return 0;
  2369. }
  2370. static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
  2371. {
  2372. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2373. info->pcm->streams[info->stream].chmap_kctl = NULL;
  2374. kfree(info);
  2375. }
  2376. /**
  2377. * snd_pcm_add_chmap_ctls - create channel-mapping control elements
  2378. * @pcm: the assigned PCM instance
  2379. * @stream: stream direction
  2380. * @chmap: channel map elements (for query)
  2381. * @max_channels: the max number of channels for the stream
  2382. * @private_value: the value passed to each kcontrol's private_value field
  2383. * @info_ret: store struct snd_pcm_chmap instance if non-NULL
  2384. *
  2385. * Create channel-mapping control elements assigned to the given PCM stream(s).
  2386. * Return: Zero if successful, or a negative error value.
  2387. */
  2388. int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
  2389. const struct snd_pcm_chmap_elem *chmap,
  2390. int max_channels,
  2391. unsigned long private_value,
  2392. struct snd_pcm_chmap **info_ret)
  2393. {
  2394. struct snd_pcm_chmap *info;
  2395. struct snd_kcontrol_new knew = {
  2396. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2397. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  2398. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  2399. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
  2400. .info = pcm_chmap_ctl_info,
  2401. .get = pcm_chmap_ctl_get,
  2402. .tlv.c = pcm_chmap_ctl_tlv,
  2403. };
  2404. int err;
  2405. if (WARN_ON(pcm->streams[stream].chmap_kctl))
  2406. return -EBUSY;
  2407. info = kzalloc(sizeof(*info), GFP_KERNEL);
  2408. if (!info)
  2409. return -ENOMEM;
  2410. info->pcm = pcm;
  2411. info->stream = stream;
  2412. info->chmap = chmap;
  2413. info->max_channels = max_channels;
  2414. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  2415. knew.name = "Playback Channel Map";
  2416. else
  2417. knew.name = "Capture Channel Map";
  2418. knew.device = pcm->device;
  2419. knew.count = pcm->streams[stream].substream_count;
  2420. knew.private_value = private_value;
  2421. info->kctl = snd_ctl_new1(&knew, info);
  2422. if (!info->kctl) {
  2423. kfree(info);
  2424. return -ENOMEM;
  2425. }
  2426. info->kctl->private_free = pcm_chmap_ctl_private_free;
  2427. err = snd_ctl_add(pcm->card, info->kctl);
  2428. if (err < 0)
  2429. return err;
  2430. pcm->streams[stream].chmap_kctl = info->kctl;
  2431. if (info_ret)
  2432. *info_ret = info;
  2433. return 0;
  2434. }
  2435. EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);