pcm_lib.c 67 KB

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