pcm_lib.c 67 KB

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