pcm_lib.c 67 KB

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