pcm_lib.c 71 KB

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