pcm_lib.c 70 KB

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