pcm_lib.c 73 KB

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