pcm_lib.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629
  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. "BUG: %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, 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, struct snd_ratden *rats,
  857. unsigned int *nump, unsigned int *denp)
  858. {
  859. unsigned int best_num, best_diff, best_den;
  860. unsigned int k;
  861. struct snd_interval t;
  862. int err;
  863. best_num = best_den = best_diff = 0;
  864. for (k = 0; k < rats_count; ++k) {
  865. unsigned int num;
  866. unsigned int den = rats[k].den;
  867. unsigned int q = i->min;
  868. int diff;
  869. num = mul(q, den);
  870. if (num > rats[k].num_max)
  871. continue;
  872. if (num < rats[k].num_min)
  873. num = rats[k].num_max;
  874. else {
  875. unsigned int r;
  876. r = (num - rats[k].num_min) % rats[k].num_step;
  877. if (r != 0)
  878. num += rats[k].num_step - r;
  879. }
  880. diff = num - q * den;
  881. if (best_num == 0 ||
  882. diff * best_den < best_diff * den) {
  883. best_diff = diff;
  884. best_den = den;
  885. best_num = num;
  886. }
  887. }
  888. if (best_den == 0) {
  889. i->empty = 1;
  890. return -EINVAL;
  891. }
  892. t.min = div_down(best_num, best_den);
  893. t.openmin = !!(best_num % best_den);
  894. best_num = best_den = best_diff = 0;
  895. for (k = 0; k < rats_count; ++k) {
  896. unsigned int num;
  897. unsigned int den = rats[k].den;
  898. unsigned int q = i->max;
  899. int diff;
  900. num = mul(q, den);
  901. if (num < rats[k].num_min)
  902. continue;
  903. if (num > rats[k].num_max)
  904. num = rats[k].num_max;
  905. else {
  906. unsigned int r;
  907. r = (num - rats[k].num_min) % rats[k].num_step;
  908. if (r != 0)
  909. num -= r;
  910. }
  911. diff = q * den - num;
  912. if (best_num == 0 ||
  913. diff * best_den < best_diff * den) {
  914. best_diff = diff;
  915. best_den = den;
  916. best_num = num;
  917. }
  918. }
  919. if (best_den == 0) {
  920. i->empty = 1;
  921. return -EINVAL;
  922. }
  923. t.max = div_up(best_num, best_den);
  924. t.openmax = !!(best_num % best_den);
  925. t.integer = 0;
  926. err = snd_interval_refine(i, &t);
  927. if (err < 0)
  928. return err;
  929. if (snd_interval_single(i)) {
  930. if (nump)
  931. *nump = best_num;
  932. if (denp)
  933. *denp = best_den;
  934. }
  935. return err;
  936. }
  937. /**
  938. * snd_interval_list - refine the interval value from the list
  939. * @i: the interval value to refine
  940. * @count: the number of elements in the list
  941. * @list: the value list
  942. * @mask: the bit-mask to evaluate
  943. *
  944. * Refines the interval value from the list.
  945. * When mask is non-zero, only the elements corresponding to bit 1 are
  946. * evaluated.
  947. *
  948. * Return: Positive if the value is changed, zero if it's not changed, or a
  949. * negative error code.
  950. */
  951. int snd_interval_list(struct snd_interval *i, unsigned int count,
  952. const unsigned int *list, unsigned int mask)
  953. {
  954. unsigned int k;
  955. struct snd_interval list_range;
  956. if (!count) {
  957. i->empty = 1;
  958. return -EINVAL;
  959. }
  960. snd_interval_any(&list_range);
  961. list_range.min = UINT_MAX;
  962. list_range.max = 0;
  963. for (k = 0; k < count; k++) {
  964. if (mask && !(mask & (1 << k)))
  965. continue;
  966. if (!snd_interval_test(i, list[k]))
  967. continue;
  968. list_range.min = min(list_range.min, list[k]);
  969. list_range.max = max(list_range.max, list[k]);
  970. }
  971. return snd_interval_refine(i, &list_range);
  972. }
  973. EXPORT_SYMBOL(snd_interval_list);
  974. /**
  975. * snd_interval_ranges - refine the interval value from the list of ranges
  976. * @i: the interval value to refine
  977. * @count: the number of elements in the list of ranges
  978. * @ranges: the ranges list
  979. * @mask: the bit-mask to evaluate
  980. *
  981. * Refines the interval value from the list of ranges.
  982. * When mask is non-zero, only the elements corresponding to bit 1 are
  983. * evaluated.
  984. *
  985. * Return: Positive if the value is changed, zero if it's not changed, or a
  986. * negative error code.
  987. */
  988. int snd_interval_ranges(struct snd_interval *i, unsigned int count,
  989. const struct snd_interval *ranges, unsigned int mask)
  990. {
  991. unsigned int k;
  992. struct snd_interval range_union;
  993. struct snd_interval range;
  994. if (!count) {
  995. snd_interval_none(i);
  996. return -EINVAL;
  997. }
  998. snd_interval_any(&range_union);
  999. range_union.min = UINT_MAX;
  1000. range_union.max = 0;
  1001. for (k = 0; k < count; k++) {
  1002. if (mask && !(mask & (1 << k)))
  1003. continue;
  1004. snd_interval_copy(&range, &ranges[k]);
  1005. if (snd_interval_refine(&range, i) < 0)
  1006. continue;
  1007. if (snd_interval_empty(&range))
  1008. continue;
  1009. if (range.min < range_union.min) {
  1010. range_union.min = range.min;
  1011. range_union.openmin = 1;
  1012. }
  1013. if (range.min == range_union.min && !range.openmin)
  1014. range_union.openmin = 0;
  1015. if (range.max > range_union.max) {
  1016. range_union.max = range.max;
  1017. range_union.openmax = 1;
  1018. }
  1019. if (range.max == range_union.max && !range.openmax)
  1020. range_union.openmax = 0;
  1021. }
  1022. return snd_interval_refine(i, &range_union);
  1023. }
  1024. EXPORT_SYMBOL(snd_interval_ranges);
  1025. static int snd_interval_step(struct snd_interval *i, unsigned int step)
  1026. {
  1027. unsigned int n;
  1028. int changed = 0;
  1029. n = i->min % step;
  1030. if (n != 0 || i->openmin) {
  1031. i->min += step - n;
  1032. i->openmin = 0;
  1033. changed = 1;
  1034. }
  1035. n = i->max % step;
  1036. if (n != 0 || i->openmax) {
  1037. i->max -= n;
  1038. i->openmax = 0;
  1039. changed = 1;
  1040. }
  1041. if (snd_interval_checkempty(i)) {
  1042. i->empty = 1;
  1043. return -EINVAL;
  1044. }
  1045. return changed;
  1046. }
  1047. /* Info constraints helpers */
  1048. /**
  1049. * snd_pcm_hw_rule_add - add the hw-constraint rule
  1050. * @runtime: the pcm runtime instance
  1051. * @cond: condition bits
  1052. * @var: the variable to evaluate
  1053. * @func: the evaluation function
  1054. * @private: the private data pointer passed to function
  1055. * @dep: the dependent variables
  1056. *
  1057. * Return: Zero if successful, or a negative error code on failure.
  1058. */
  1059. int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
  1060. int var,
  1061. snd_pcm_hw_rule_func_t func, void *private,
  1062. int dep, ...)
  1063. {
  1064. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1065. struct snd_pcm_hw_rule *c;
  1066. unsigned int k;
  1067. va_list args;
  1068. va_start(args, dep);
  1069. if (constrs->rules_num >= constrs->rules_all) {
  1070. struct snd_pcm_hw_rule *new;
  1071. unsigned int new_rules = constrs->rules_all + 16;
  1072. new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
  1073. if (!new) {
  1074. va_end(args);
  1075. return -ENOMEM;
  1076. }
  1077. if (constrs->rules) {
  1078. memcpy(new, constrs->rules,
  1079. constrs->rules_num * sizeof(*c));
  1080. kfree(constrs->rules);
  1081. }
  1082. constrs->rules = new;
  1083. constrs->rules_all = new_rules;
  1084. }
  1085. c = &constrs->rules[constrs->rules_num];
  1086. c->cond = cond;
  1087. c->func = func;
  1088. c->var = var;
  1089. c->private = private;
  1090. k = 0;
  1091. while (1) {
  1092. if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
  1093. va_end(args);
  1094. return -EINVAL;
  1095. }
  1096. c->deps[k++] = dep;
  1097. if (dep < 0)
  1098. break;
  1099. dep = va_arg(args, int);
  1100. }
  1101. constrs->rules_num++;
  1102. va_end(args);
  1103. return 0;
  1104. }
  1105. EXPORT_SYMBOL(snd_pcm_hw_rule_add);
  1106. /**
  1107. * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
  1108. * @runtime: PCM runtime instance
  1109. * @var: hw_params variable to apply the mask
  1110. * @mask: the bitmap mask
  1111. *
  1112. * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
  1113. *
  1114. * Return: Zero if successful, or a negative error code on failure.
  1115. */
  1116. int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1117. u_int32_t mask)
  1118. {
  1119. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1120. struct snd_mask *maskp = constrs_mask(constrs, var);
  1121. *maskp->bits &= mask;
  1122. memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
  1123. if (*maskp->bits == 0)
  1124. return -EINVAL;
  1125. return 0;
  1126. }
  1127. /**
  1128. * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
  1129. * @runtime: PCM runtime instance
  1130. * @var: hw_params variable to apply the mask
  1131. * @mask: the 64bit bitmap mask
  1132. *
  1133. * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
  1134. *
  1135. * Return: Zero if successful, or a negative error code on failure.
  1136. */
  1137. int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1138. u_int64_t mask)
  1139. {
  1140. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1141. struct snd_mask *maskp = constrs_mask(constrs, var);
  1142. maskp->bits[0] &= (u_int32_t)mask;
  1143. maskp->bits[1] &= (u_int32_t)(mask >> 32);
  1144. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
  1145. if (! maskp->bits[0] && ! maskp->bits[1])
  1146. return -EINVAL;
  1147. return 0;
  1148. }
  1149. EXPORT_SYMBOL(snd_pcm_hw_constraint_mask64);
  1150. /**
  1151. * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
  1152. * @runtime: PCM runtime instance
  1153. * @var: hw_params variable to apply the integer constraint
  1154. *
  1155. * Apply the constraint of integer to an interval parameter.
  1156. *
  1157. * Return: Positive if the value is changed, zero if it's not changed, or a
  1158. * negative error code.
  1159. */
  1160. int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
  1161. {
  1162. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1163. return snd_interval_setinteger(constrs_interval(constrs, var));
  1164. }
  1165. EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
  1166. /**
  1167. * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
  1168. * @runtime: PCM runtime instance
  1169. * @var: hw_params variable to apply the range
  1170. * @min: the minimal value
  1171. * @max: the maximal value
  1172. *
  1173. * Apply the min/max range constraint to an interval parameter.
  1174. *
  1175. * Return: Positive if the value is changed, zero if it's not changed, or a
  1176. * negative error code.
  1177. */
  1178. int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1179. unsigned int min, unsigned int max)
  1180. {
  1181. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1182. struct snd_interval t;
  1183. t.min = min;
  1184. t.max = max;
  1185. t.openmin = t.openmax = 0;
  1186. t.integer = 0;
  1187. return snd_interval_refine(constrs_interval(constrs, var), &t);
  1188. }
  1189. EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
  1190. static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
  1191. struct snd_pcm_hw_rule *rule)
  1192. {
  1193. struct snd_pcm_hw_constraint_list *list = rule->private;
  1194. return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
  1195. }
  1196. /**
  1197. * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
  1198. * @runtime: PCM runtime instance
  1199. * @cond: condition bits
  1200. * @var: hw_params variable to apply the list constraint
  1201. * @l: list
  1202. *
  1203. * Apply the list of constraints to an interval parameter.
  1204. *
  1205. * Return: Zero if successful, or a negative error code on failure.
  1206. */
  1207. int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
  1208. unsigned int cond,
  1209. snd_pcm_hw_param_t var,
  1210. const struct snd_pcm_hw_constraint_list *l)
  1211. {
  1212. return snd_pcm_hw_rule_add(runtime, cond, var,
  1213. snd_pcm_hw_rule_list, (void *)l,
  1214. var, -1);
  1215. }
  1216. EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
  1217. static int snd_pcm_hw_rule_ranges(struct snd_pcm_hw_params *params,
  1218. struct snd_pcm_hw_rule *rule)
  1219. {
  1220. struct snd_pcm_hw_constraint_ranges *r = rule->private;
  1221. return snd_interval_ranges(hw_param_interval(params, rule->var),
  1222. r->count, r->ranges, r->mask);
  1223. }
  1224. /**
  1225. * snd_pcm_hw_constraint_ranges - apply list of range constraints to a parameter
  1226. * @runtime: PCM runtime instance
  1227. * @cond: condition bits
  1228. * @var: hw_params variable to apply the list of range constraints
  1229. * @r: ranges
  1230. *
  1231. * Apply the list of range constraints to an interval parameter.
  1232. *
  1233. * Return: Zero if successful, or a negative error code on failure.
  1234. */
  1235. int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
  1236. unsigned int cond,
  1237. snd_pcm_hw_param_t var,
  1238. const struct snd_pcm_hw_constraint_ranges *r)
  1239. {
  1240. return snd_pcm_hw_rule_add(runtime, cond, var,
  1241. snd_pcm_hw_rule_ranges, (void *)r,
  1242. var, -1);
  1243. }
  1244. EXPORT_SYMBOL(snd_pcm_hw_constraint_ranges);
  1245. static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
  1246. struct snd_pcm_hw_rule *rule)
  1247. {
  1248. struct snd_pcm_hw_constraint_ratnums *r = rule->private;
  1249. unsigned int num = 0, den = 0;
  1250. int err;
  1251. err = snd_interval_ratnum(hw_param_interval(params, rule->var),
  1252. r->nrats, r->rats, &num, &den);
  1253. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1254. params->rate_num = num;
  1255. params->rate_den = den;
  1256. }
  1257. return err;
  1258. }
  1259. /**
  1260. * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
  1261. * @runtime: PCM runtime instance
  1262. * @cond: condition bits
  1263. * @var: hw_params variable to apply the ratnums constraint
  1264. * @r: struct snd_ratnums constriants
  1265. *
  1266. * Return: Zero if successful, or a negative error code on failure.
  1267. */
  1268. int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
  1269. unsigned int cond,
  1270. snd_pcm_hw_param_t var,
  1271. struct snd_pcm_hw_constraint_ratnums *r)
  1272. {
  1273. return snd_pcm_hw_rule_add(runtime, cond, var,
  1274. snd_pcm_hw_rule_ratnums, r,
  1275. var, -1);
  1276. }
  1277. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
  1278. static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
  1279. struct snd_pcm_hw_rule *rule)
  1280. {
  1281. struct snd_pcm_hw_constraint_ratdens *r = rule->private;
  1282. unsigned int num = 0, den = 0;
  1283. int err = snd_interval_ratden(hw_param_interval(params, rule->var),
  1284. r->nrats, r->rats, &num, &den);
  1285. if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
  1286. params->rate_num = num;
  1287. params->rate_den = den;
  1288. }
  1289. return err;
  1290. }
  1291. /**
  1292. * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
  1293. * @runtime: PCM runtime instance
  1294. * @cond: condition bits
  1295. * @var: hw_params variable to apply the ratdens constraint
  1296. * @r: struct snd_ratdens constriants
  1297. *
  1298. * Return: Zero if successful, or a negative error code on failure.
  1299. */
  1300. int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
  1301. unsigned int cond,
  1302. snd_pcm_hw_param_t var,
  1303. struct snd_pcm_hw_constraint_ratdens *r)
  1304. {
  1305. return snd_pcm_hw_rule_add(runtime, cond, var,
  1306. snd_pcm_hw_rule_ratdens, r,
  1307. var, -1);
  1308. }
  1309. EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
  1310. static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
  1311. struct snd_pcm_hw_rule *rule)
  1312. {
  1313. unsigned int l = (unsigned long) rule->private;
  1314. int width = l & 0xffff;
  1315. unsigned int msbits = l >> 16;
  1316. struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
  1317. if (!snd_interval_single(i))
  1318. return 0;
  1319. if ((snd_interval_value(i) == width) ||
  1320. (width == 0 && snd_interval_value(i) > msbits))
  1321. params->msbits = min_not_zero(params->msbits, msbits);
  1322. return 0;
  1323. }
  1324. /**
  1325. * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
  1326. * @runtime: PCM runtime instance
  1327. * @cond: condition bits
  1328. * @width: sample bits width
  1329. * @msbits: msbits width
  1330. *
  1331. * This constraint will set the number of most significant bits (msbits) if a
  1332. * sample format with the specified width has been select. If width is set to 0
  1333. * the msbits will be set for any sample format with a width larger than the
  1334. * specified msbits.
  1335. *
  1336. * Return: Zero if successful, or a negative error code on failure.
  1337. */
  1338. int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
  1339. unsigned int cond,
  1340. unsigned int width,
  1341. unsigned int msbits)
  1342. {
  1343. unsigned long l = (msbits << 16) | width;
  1344. return snd_pcm_hw_rule_add(runtime, cond, -1,
  1345. snd_pcm_hw_rule_msbits,
  1346. (void*) l,
  1347. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1348. }
  1349. EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
  1350. static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
  1351. struct snd_pcm_hw_rule *rule)
  1352. {
  1353. unsigned long step = (unsigned long) rule->private;
  1354. return snd_interval_step(hw_param_interval(params, rule->var), step);
  1355. }
  1356. /**
  1357. * snd_pcm_hw_constraint_step - add a hw constraint step rule
  1358. * @runtime: PCM runtime instance
  1359. * @cond: condition bits
  1360. * @var: hw_params variable to apply the step constraint
  1361. * @step: step size
  1362. *
  1363. * Return: Zero if successful, or a negative error code on failure.
  1364. */
  1365. int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
  1366. unsigned int cond,
  1367. snd_pcm_hw_param_t var,
  1368. unsigned long step)
  1369. {
  1370. return snd_pcm_hw_rule_add(runtime, cond, var,
  1371. snd_pcm_hw_rule_step, (void *) step,
  1372. var, -1);
  1373. }
  1374. EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
  1375. static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
  1376. {
  1377. static unsigned int pow2_sizes[] = {
  1378. 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
  1379. 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
  1380. 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
  1381. 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
  1382. };
  1383. return snd_interval_list(hw_param_interval(params, rule->var),
  1384. ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
  1385. }
  1386. /**
  1387. * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
  1388. * @runtime: PCM runtime instance
  1389. * @cond: condition bits
  1390. * @var: hw_params variable to apply the power-of-2 constraint
  1391. *
  1392. * Return: Zero if successful, or a negative error code on failure.
  1393. */
  1394. int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
  1395. unsigned int cond,
  1396. snd_pcm_hw_param_t var)
  1397. {
  1398. return snd_pcm_hw_rule_add(runtime, cond, var,
  1399. snd_pcm_hw_rule_pow2, NULL,
  1400. var, -1);
  1401. }
  1402. EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
  1403. static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
  1404. struct snd_pcm_hw_rule *rule)
  1405. {
  1406. unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
  1407. struct snd_interval *rate;
  1408. rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  1409. return snd_interval_list(rate, 1, &base_rate, 0);
  1410. }
  1411. /**
  1412. * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
  1413. * @runtime: PCM runtime instance
  1414. * @base_rate: the rate at which the hardware does not resample
  1415. *
  1416. * Return: Zero if successful, or a negative error code on failure.
  1417. */
  1418. int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
  1419. unsigned int base_rate)
  1420. {
  1421. return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
  1422. SNDRV_PCM_HW_PARAM_RATE,
  1423. snd_pcm_hw_rule_noresample_func,
  1424. (void *)(uintptr_t)base_rate,
  1425. SNDRV_PCM_HW_PARAM_RATE, -1);
  1426. }
  1427. EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);
  1428. static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
  1429. snd_pcm_hw_param_t var)
  1430. {
  1431. if (hw_is_mask(var)) {
  1432. snd_mask_any(hw_param_mask(params, var));
  1433. params->cmask |= 1 << var;
  1434. params->rmask |= 1 << var;
  1435. return;
  1436. }
  1437. if (hw_is_interval(var)) {
  1438. snd_interval_any(hw_param_interval(params, var));
  1439. params->cmask |= 1 << var;
  1440. params->rmask |= 1 << var;
  1441. return;
  1442. }
  1443. snd_BUG();
  1444. }
  1445. void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
  1446. {
  1447. unsigned int k;
  1448. memset(params, 0, sizeof(*params));
  1449. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
  1450. _snd_pcm_hw_param_any(params, k);
  1451. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
  1452. _snd_pcm_hw_param_any(params, k);
  1453. params->info = ~0U;
  1454. }
  1455. EXPORT_SYMBOL(_snd_pcm_hw_params_any);
  1456. /**
  1457. * snd_pcm_hw_param_value - return @params field @var value
  1458. * @params: the hw_params instance
  1459. * @var: parameter to retrieve
  1460. * @dir: pointer to the direction (-1,0,1) or %NULL
  1461. *
  1462. * Return: The value for field @var if it's fixed in configuration space
  1463. * defined by @params. -%EINVAL otherwise.
  1464. */
  1465. int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
  1466. snd_pcm_hw_param_t var, int *dir)
  1467. {
  1468. if (hw_is_mask(var)) {
  1469. const struct snd_mask *mask = hw_param_mask_c(params, var);
  1470. if (!snd_mask_single(mask))
  1471. return -EINVAL;
  1472. if (dir)
  1473. *dir = 0;
  1474. return snd_mask_value(mask);
  1475. }
  1476. if (hw_is_interval(var)) {
  1477. const struct snd_interval *i = hw_param_interval_c(params, var);
  1478. if (!snd_interval_single(i))
  1479. return -EINVAL;
  1480. if (dir)
  1481. *dir = i->openmin;
  1482. return snd_interval_value(i);
  1483. }
  1484. return -EINVAL;
  1485. }
  1486. EXPORT_SYMBOL(snd_pcm_hw_param_value);
  1487. void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
  1488. snd_pcm_hw_param_t var)
  1489. {
  1490. if (hw_is_mask(var)) {
  1491. snd_mask_none(hw_param_mask(params, var));
  1492. params->cmask |= 1 << var;
  1493. params->rmask |= 1 << var;
  1494. } else if (hw_is_interval(var)) {
  1495. snd_interval_none(hw_param_interval(params, var));
  1496. params->cmask |= 1 << var;
  1497. params->rmask |= 1 << var;
  1498. } else {
  1499. snd_BUG();
  1500. }
  1501. }
  1502. EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
  1503. static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
  1504. snd_pcm_hw_param_t var)
  1505. {
  1506. int changed;
  1507. if (hw_is_mask(var))
  1508. changed = snd_mask_refine_first(hw_param_mask(params, var));
  1509. else if (hw_is_interval(var))
  1510. changed = snd_interval_refine_first(hw_param_interval(params, var));
  1511. else
  1512. return -EINVAL;
  1513. if (changed) {
  1514. params->cmask |= 1 << var;
  1515. params->rmask |= 1 << var;
  1516. }
  1517. return changed;
  1518. }
  1519. /**
  1520. * snd_pcm_hw_param_first - refine config space and return minimum value
  1521. * @pcm: PCM instance
  1522. * @params: the hw_params instance
  1523. * @var: parameter to retrieve
  1524. * @dir: pointer to the direction (-1,0,1) or %NULL
  1525. *
  1526. * Inside configuration space defined by @params remove from @var all
  1527. * values > minimum. Reduce configuration space accordingly.
  1528. *
  1529. * Return: The minimum, or a negative error code on failure.
  1530. */
  1531. int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
  1532. struct snd_pcm_hw_params *params,
  1533. snd_pcm_hw_param_t var, int *dir)
  1534. {
  1535. int changed = _snd_pcm_hw_param_first(params, var);
  1536. if (changed < 0)
  1537. return changed;
  1538. if (params->rmask) {
  1539. int err = snd_pcm_hw_refine(pcm, params);
  1540. if (snd_BUG_ON(err < 0))
  1541. return err;
  1542. }
  1543. return snd_pcm_hw_param_value(params, var, dir);
  1544. }
  1545. EXPORT_SYMBOL(snd_pcm_hw_param_first);
  1546. static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
  1547. snd_pcm_hw_param_t var)
  1548. {
  1549. int changed;
  1550. if (hw_is_mask(var))
  1551. changed = snd_mask_refine_last(hw_param_mask(params, var));
  1552. else if (hw_is_interval(var))
  1553. changed = snd_interval_refine_last(hw_param_interval(params, var));
  1554. else
  1555. return -EINVAL;
  1556. if (changed) {
  1557. params->cmask |= 1 << var;
  1558. params->rmask |= 1 << var;
  1559. }
  1560. return changed;
  1561. }
  1562. /**
  1563. * snd_pcm_hw_param_last - refine config space and return maximum value
  1564. * @pcm: PCM instance
  1565. * @params: the hw_params instance
  1566. * @var: parameter to retrieve
  1567. * @dir: pointer to the direction (-1,0,1) or %NULL
  1568. *
  1569. * Inside configuration space defined by @params remove from @var all
  1570. * values < maximum. Reduce configuration space accordingly.
  1571. *
  1572. * Return: The maximum, or a negative error code on failure.
  1573. */
  1574. int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
  1575. struct snd_pcm_hw_params *params,
  1576. snd_pcm_hw_param_t var, int *dir)
  1577. {
  1578. int changed = _snd_pcm_hw_param_last(params, var);
  1579. if (changed < 0)
  1580. return changed;
  1581. if (params->rmask) {
  1582. int err = snd_pcm_hw_refine(pcm, params);
  1583. if (snd_BUG_ON(err < 0))
  1584. return err;
  1585. }
  1586. return snd_pcm_hw_param_value(params, var, dir);
  1587. }
  1588. EXPORT_SYMBOL(snd_pcm_hw_param_last);
  1589. /**
  1590. * snd_pcm_hw_param_choose - choose a configuration defined by @params
  1591. * @pcm: PCM instance
  1592. * @params: the hw_params instance
  1593. *
  1594. * Choose one configuration from configuration space defined by @params.
  1595. * The configuration chosen is that obtained fixing in this order:
  1596. * first access, first format, first subformat, min channels,
  1597. * min rate, min period time, max buffer size, min tick time
  1598. *
  1599. * Return: Zero if successful, or a negative error code on failure.
  1600. */
  1601. int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
  1602. struct snd_pcm_hw_params *params)
  1603. {
  1604. static int vars[] = {
  1605. SNDRV_PCM_HW_PARAM_ACCESS,
  1606. SNDRV_PCM_HW_PARAM_FORMAT,
  1607. SNDRV_PCM_HW_PARAM_SUBFORMAT,
  1608. SNDRV_PCM_HW_PARAM_CHANNELS,
  1609. SNDRV_PCM_HW_PARAM_RATE,
  1610. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1611. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1612. SNDRV_PCM_HW_PARAM_TICK_TIME,
  1613. -1
  1614. };
  1615. int err, *v;
  1616. for (v = vars; *v != -1; v++) {
  1617. if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
  1618. err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
  1619. else
  1620. err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
  1621. if (snd_BUG_ON(err < 0))
  1622. return err;
  1623. }
  1624. return 0;
  1625. }
  1626. static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
  1627. void *arg)
  1628. {
  1629. struct snd_pcm_runtime *runtime = substream->runtime;
  1630. unsigned long flags;
  1631. snd_pcm_stream_lock_irqsave(substream, flags);
  1632. if (snd_pcm_running(substream) &&
  1633. snd_pcm_update_hw_ptr(substream) >= 0)
  1634. runtime->status->hw_ptr %= runtime->buffer_size;
  1635. else {
  1636. runtime->status->hw_ptr = 0;
  1637. runtime->hw_ptr_wrap = 0;
  1638. }
  1639. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1640. return 0;
  1641. }
  1642. static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
  1643. void *arg)
  1644. {
  1645. struct snd_pcm_channel_info *info = arg;
  1646. struct snd_pcm_runtime *runtime = substream->runtime;
  1647. int width;
  1648. if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
  1649. info->offset = -1;
  1650. return 0;
  1651. }
  1652. width = snd_pcm_format_physical_width(runtime->format);
  1653. if (width < 0)
  1654. return width;
  1655. info->offset = 0;
  1656. switch (runtime->access) {
  1657. case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
  1658. case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
  1659. info->first = info->channel * width;
  1660. info->step = runtime->channels * width;
  1661. break;
  1662. case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
  1663. case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
  1664. {
  1665. size_t size = runtime->dma_bytes / runtime->channels;
  1666. info->first = info->channel * size * 8;
  1667. info->step = width;
  1668. break;
  1669. }
  1670. default:
  1671. snd_BUG();
  1672. break;
  1673. }
  1674. return 0;
  1675. }
  1676. static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
  1677. void *arg)
  1678. {
  1679. struct snd_pcm_hw_params *params = arg;
  1680. snd_pcm_format_t format;
  1681. int channels;
  1682. ssize_t frame_size;
  1683. params->fifo_size = substream->runtime->hw.fifo_size;
  1684. if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
  1685. format = params_format(params);
  1686. channels = params_channels(params);
  1687. frame_size = snd_pcm_format_size(format, channels);
  1688. if (frame_size > 0)
  1689. params->fifo_size /= (unsigned)frame_size;
  1690. }
  1691. return 0;
  1692. }
  1693. /**
  1694. * snd_pcm_lib_ioctl - a generic PCM ioctl callback
  1695. * @substream: the pcm substream instance
  1696. * @cmd: ioctl command
  1697. * @arg: ioctl argument
  1698. *
  1699. * Processes the generic ioctl commands for PCM.
  1700. * Can be passed as the ioctl callback for PCM ops.
  1701. *
  1702. * Return: Zero if successful, or a negative error code on failure.
  1703. */
  1704. int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  1705. unsigned int cmd, void *arg)
  1706. {
  1707. switch (cmd) {
  1708. case SNDRV_PCM_IOCTL1_INFO:
  1709. return 0;
  1710. case SNDRV_PCM_IOCTL1_RESET:
  1711. return snd_pcm_lib_ioctl_reset(substream, arg);
  1712. case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
  1713. return snd_pcm_lib_ioctl_channel_info(substream, arg);
  1714. case SNDRV_PCM_IOCTL1_FIFO_SIZE:
  1715. return snd_pcm_lib_ioctl_fifo_size(substream, arg);
  1716. }
  1717. return -ENXIO;
  1718. }
  1719. EXPORT_SYMBOL(snd_pcm_lib_ioctl);
  1720. /**
  1721. * snd_pcm_period_elapsed - update the pcm status for the next period
  1722. * @substream: the pcm substream instance
  1723. *
  1724. * This function is called from the interrupt handler when the
  1725. * PCM has processed the period size. It will update the current
  1726. * pointer, wake up sleepers, etc.
  1727. *
  1728. * Even if more than one periods have elapsed since the last call, you
  1729. * have to call this only once.
  1730. */
  1731. void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
  1732. {
  1733. struct snd_pcm_runtime *runtime;
  1734. unsigned long flags;
  1735. if (PCM_RUNTIME_CHECK(substream))
  1736. return;
  1737. runtime = substream->runtime;
  1738. if (runtime->transfer_ack_begin)
  1739. runtime->transfer_ack_begin(substream);
  1740. snd_pcm_stream_lock_irqsave(substream, flags);
  1741. if (!snd_pcm_running(substream) ||
  1742. snd_pcm_update_hw_ptr0(substream, 1) < 0)
  1743. goto _end;
  1744. if (substream->timer_running)
  1745. snd_timer_interrupt(substream->timer, 1);
  1746. _end:
  1747. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1748. if (runtime->transfer_ack_end)
  1749. runtime->transfer_ack_end(substream);
  1750. kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
  1751. }
  1752. EXPORT_SYMBOL(snd_pcm_period_elapsed);
  1753. /*
  1754. * Wait until avail_min data becomes available
  1755. * Returns a negative error code if any error occurs during operation.
  1756. * The available space is stored on availp. When err = 0 and avail = 0
  1757. * on the capture stream, it indicates the stream is in DRAINING state.
  1758. */
  1759. static int wait_for_avail(struct snd_pcm_substream *substream,
  1760. snd_pcm_uframes_t *availp)
  1761. {
  1762. struct snd_pcm_runtime *runtime = substream->runtime;
  1763. int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
  1764. wait_queue_t wait;
  1765. int err = 0;
  1766. snd_pcm_uframes_t avail = 0;
  1767. long wait_time, tout;
  1768. init_waitqueue_entry(&wait, current);
  1769. set_current_state(TASK_INTERRUPTIBLE);
  1770. add_wait_queue(&runtime->tsleep, &wait);
  1771. if (runtime->no_period_wakeup)
  1772. wait_time = MAX_SCHEDULE_TIMEOUT;
  1773. else {
  1774. wait_time = 10;
  1775. if (runtime->rate) {
  1776. long t = runtime->period_size * 2 / runtime->rate;
  1777. wait_time = max(t, wait_time);
  1778. }
  1779. wait_time = msecs_to_jiffies(wait_time * 1000);
  1780. }
  1781. for (;;) {
  1782. if (signal_pending(current)) {
  1783. err = -ERESTARTSYS;
  1784. break;
  1785. }
  1786. /*
  1787. * We need to check if space became available already
  1788. * (and thus the wakeup happened already) first to close
  1789. * the race of space already having become available.
  1790. * This check must happen after been added to the waitqueue
  1791. * and having current state be INTERRUPTIBLE.
  1792. */
  1793. if (is_playback)
  1794. avail = snd_pcm_playback_avail(runtime);
  1795. else
  1796. avail = snd_pcm_capture_avail(runtime);
  1797. if (avail >= runtime->twake)
  1798. break;
  1799. snd_pcm_stream_unlock_irq(substream);
  1800. tout = schedule_timeout(wait_time);
  1801. snd_pcm_stream_lock_irq(substream);
  1802. set_current_state(TASK_INTERRUPTIBLE);
  1803. switch (runtime->status->state) {
  1804. case SNDRV_PCM_STATE_SUSPENDED:
  1805. err = -ESTRPIPE;
  1806. goto _endloop;
  1807. case SNDRV_PCM_STATE_XRUN:
  1808. err = -EPIPE;
  1809. goto _endloop;
  1810. case SNDRV_PCM_STATE_DRAINING:
  1811. if (is_playback)
  1812. err = -EPIPE;
  1813. else
  1814. avail = 0; /* indicate draining */
  1815. goto _endloop;
  1816. case SNDRV_PCM_STATE_OPEN:
  1817. case SNDRV_PCM_STATE_SETUP:
  1818. case SNDRV_PCM_STATE_DISCONNECTED:
  1819. err = -EBADFD;
  1820. goto _endloop;
  1821. case SNDRV_PCM_STATE_PAUSED:
  1822. continue;
  1823. }
  1824. if (!tout) {
  1825. pcm_dbg(substream->pcm,
  1826. "%s write error (DMA or IRQ trouble?)\n",
  1827. is_playback ? "playback" : "capture");
  1828. err = -EIO;
  1829. break;
  1830. }
  1831. }
  1832. _endloop:
  1833. set_current_state(TASK_RUNNING);
  1834. remove_wait_queue(&runtime->tsleep, &wait);
  1835. *availp = avail;
  1836. return err;
  1837. }
  1838. static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
  1839. unsigned int hwoff,
  1840. unsigned long data, unsigned int off,
  1841. snd_pcm_uframes_t frames)
  1842. {
  1843. struct snd_pcm_runtime *runtime = substream->runtime;
  1844. int err;
  1845. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  1846. if (substream->ops->copy) {
  1847. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  1848. return err;
  1849. } else {
  1850. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  1851. if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
  1852. return -EFAULT;
  1853. }
  1854. return 0;
  1855. }
  1856. typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
  1857. unsigned long data, unsigned int off,
  1858. snd_pcm_uframes_t size);
  1859. static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
  1860. unsigned long data,
  1861. snd_pcm_uframes_t size,
  1862. int nonblock,
  1863. transfer_f transfer)
  1864. {
  1865. struct snd_pcm_runtime *runtime = substream->runtime;
  1866. snd_pcm_uframes_t xfer = 0;
  1867. snd_pcm_uframes_t offset = 0;
  1868. snd_pcm_uframes_t avail;
  1869. int err = 0;
  1870. if (size == 0)
  1871. return 0;
  1872. snd_pcm_stream_lock_irq(substream);
  1873. switch (runtime->status->state) {
  1874. case SNDRV_PCM_STATE_PREPARED:
  1875. case SNDRV_PCM_STATE_RUNNING:
  1876. case SNDRV_PCM_STATE_PAUSED:
  1877. break;
  1878. case SNDRV_PCM_STATE_XRUN:
  1879. err = -EPIPE;
  1880. goto _end_unlock;
  1881. case SNDRV_PCM_STATE_SUSPENDED:
  1882. err = -ESTRPIPE;
  1883. goto _end_unlock;
  1884. default:
  1885. err = -EBADFD;
  1886. goto _end_unlock;
  1887. }
  1888. runtime->twake = runtime->control->avail_min ? : 1;
  1889. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1890. snd_pcm_update_hw_ptr(substream);
  1891. avail = snd_pcm_playback_avail(runtime);
  1892. while (size > 0) {
  1893. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  1894. snd_pcm_uframes_t cont;
  1895. if (!avail) {
  1896. if (nonblock) {
  1897. err = -EAGAIN;
  1898. goto _end_unlock;
  1899. }
  1900. runtime->twake = min_t(snd_pcm_uframes_t, size,
  1901. runtime->control->avail_min ? : 1);
  1902. err = wait_for_avail(substream, &avail);
  1903. if (err < 0)
  1904. goto _end_unlock;
  1905. }
  1906. frames = size > avail ? avail : size;
  1907. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  1908. if (frames > cont)
  1909. frames = cont;
  1910. if (snd_BUG_ON(!frames)) {
  1911. runtime->twake = 0;
  1912. snd_pcm_stream_unlock_irq(substream);
  1913. return -EINVAL;
  1914. }
  1915. appl_ptr = runtime->control->appl_ptr;
  1916. appl_ofs = appl_ptr % runtime->buffer_size;
  1917. snd_pcm_stream_unlock_irq(substream);
  1918. err = transfer(substream, appl_ofs, data, offset, frames);
  1919. snd_pcm_stream_lock_irq(substream);
  1920. if (err < 0)
  1921. goto _end_unlock;
  1922. switch (runtime->status->state) {
  1923. case SNDRV_PCM_STATE_XRUN:
  1924. err = -EPIPE;
  1925. goto _end_unlock;
  1926. case SNDRV_PCM_STATE_SUSPENDED:
  1927. err = -ESTRPIPE;
  1928. goto _end_unlock;
  1929. default:
  1930. break;
  1931. }
  1932. appl_ptr += frames;
  1933. if (appl_ptr >= runtime->boundary)
  1934. appl_ptr -= runtime->boundary;
  1935. runtime->control->appl_ptr = appl_ptr;
  1936. if (substream->ops->ack)
  1937. substream->ops->ack(substream);
  1938. offset += frames;
  1939. size -= frames;
  1940. xfer += frames;
  1941. avail -= frames;
  1942. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
  1943. snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
  1944. err = snd_pcm_start(substream);
  1945. if (err < 0)
  1946. goto _end_unlock;
  1947. }
  1948. }
  1949. _end_unlock:
  1950. runtime->twake = 0;
  1951. if (xfer > 0 && err >= 0)
  1952. snd_pcm_update_state(substream, runtime);
  1953. snd_pcm_stream_unlock_irq(substream);
  1954. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  1955. }
  1956. /* sanity-check for read/write methods */
  1957. static int pcm_sanity_check(struct snd_pcm_substream *substream)
  1958. {
  1959. struct snd_pcm_runtime *runtime;
  1960. if (PCM_RUNTIME_CHECK(substream))
  1961. return -ENXIO;
  1962. runtime = substream->runtime;
  1963. if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
  1964. return -EINVAL;
  1965. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1966. return -EBADFD;
  1967. return 0;
  1968. }
  1969. snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
  1970. {
  1971. struct snd_pcm_runtime *runtime;
  1972. int nonblock;
  1973. int err;
  1974. err = pcm_sanity_check(substream);
  1975. if (err < 0)
  1976. return err;
  1977. runtime = substream->runtime;
  1978. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1979. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
  1980. runtime->channels > 1)
  1981. return -EINVAL;
  1982. return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
  1983. snd_pcm_lib_write_transfer);
  1984. }
  1985. EXPORT_SYMBOL(snd_pcm_lib_write);
  1986. static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
  1987. unsigned int hwoff,
  1988. unsigned long data, unsigned int off,
  1989. snd_pcm_uframes_t frames)
  1990. {
  1991. struct snd_pcm_runtime *runtime = substream->runtime;
  1992. int err;
  1993. void __user **bufs = (void __user **)data;
  1994. int channels = runtime->channels;
  1995. int c;
  1996. if (substream->ops->copy) {
  1997. if (snd_BUG_ON(!substream->ops->silence))
  1998. return -EINVAL;
  1999. for (c = 0; c < channels; ++c, ++bufs) {
  2000. if (*bufs == NULL) {
  2001. if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
  2002. return err;
  2003. } else {
  2004. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  2005. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  2006. return err;
  2007. }
  2008. }
  2009. } else {
  2010. /* default transfer behaviour */
  2011. size_t dma_csize = runtime->dma_bytes / channels;
  2012. for (c = 0; c < channels; ++c, ++bufs) {
  2013. char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  2014. if (*bufs == NULL) {
  2015. snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
  2016. } else {
  2017. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  2018. if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
  2019. return -EFAULT;
  2020. }
  2021. }
  2022. }
  2023. return 0;
  2024. }
  2025. snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
  2026. void __user **bufs,
  2027. snd_pcm_uframes_t frames)
  2028. {
  2029. struct snd_pcm_runtime *runtime;
  2030. int nonblock;
  2031. int err;
  2032. err = pcm_sanity_check(substream);
  2033. if (err < 0)
  2034. return err;
  2035. runtime = substream->runtime;
  2036. nonblock = !!(substream->f_flags & O_NONBLOCK);
  2037. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  2038. return -EINVAL;
  2039. return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
  2040. nonblock, snd_pcm_lib_writev_transfer);
  2041. }
  2042. EXPORT_SYMBOL(snd_pcm_lib_writev);
  2043. static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
  2044. unsigned int hwoff,
  2045. unsigned long data, unsigned int off,
  2046. snd_pcm_uframes_t frames)
  2047. {
  2048. struct snd_pcm_runtime *runtime = substream->runtime;
  2049. int err;
  2050. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  2051. if (substream->ops->copy) {
  2052. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  2053. return err;
  2054. } else {
  2055. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  2056. if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
  2057. return -EFAULT;
  2058. }
  2059. return 0;
  2060. }
  2061. static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
  2062. unsigned long data,
  2063. snd_pcm_uframes_t size,
  2064. int nonblock,
  2065. transfer_f transfer)
  2066. {
  2067. struct snd_pcm_runtime *runtime = substream->runtime;
  2068. snd_pcm_uframes_t xfer = 0;
  2069. snd_pcm_uframes_t offset = 0;
  2070. snd_pcm_uframes_t avail;
  2071. int err = 0;
  2072. if (size == 0)
  2073. return 0;
  2074. snd_pcm_stream_lock_irq(substream);
  2075. switch (runtime->status->state) {
  2076. case SNDRV_PCM_STATE_PREPARED:
  2077. if (size >= runtime->start_threshold) {
  2078. err = snd_pcm_start(substream);
  2079. if (err < 0)
  2080. goto _end_unlock;
  2081. }
  2082. break;
  2083. case SNDRV_PCM_STATE_DRAINING:
  2084. case SNDRV_PCM_STATE_RUNNING:
  2085. case SNDRV_PCM_STATE_PAUSED:
  2086. break;
  2087. case SNDRV_PCM_STATE_XRUN:
  2088. err = -EPIPE;
  2089. goto _end_unlock;
  2090. case SNDRV_PCM_STATE_SUSPENDED:
  2091. err = -ESTRPIPE;
  2092. goto _end_unlock;
  2093. default:
  2094. err = -EBADFD;
  2095. goto _end_unlock;
  2096. }
  2097. runtime->twake = runtime->control->avail_min ? : 1;
  2098. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  2099. snd_pcm_update_hw_ptr(substream);
  2100. avail = snd_pcm_capture_avail(runtime);
  2101. while (size > 0) {
  2102. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  2103. snd_pcm_uframes_t cont;
  2104. if (!avail) {
  2105. if (runtime->status->state ==
  2106. SNDRV_PCM_STATE_DRAINING) {
  2107. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  2108. goto _end_unlock;
  2109. }
  2110. if (nonblock) {
  2111. err = -EAGAIN;
  2112. goto _end_unlock;
  2113. }
  2114. runtime->twake = min_t(snd_pcm_uframes_t, size,
  2115. runtime->control->avail_min ? : 1);
  2116. err = wait_for_avail(substream, &avail);
  2117. if (err < 0)
  2118. goto _end_unlock;
  2119. if (!avail)
  2120. continue; /* draining */
  2121. }
  2122. frames = size > avail ? avail : size;
  2123. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  2124. if (frames > cont)
  2125. frames = cont;
  2126. if (snd_BUG_ON(!frames)) {
  2127. runtime->twake = 0;
  2128. snd_pcm_stream_unlock_irq(substream);
  2129. return -EINVAL;
  2130. }
  2131. appl_ptr = runtime->control->appl_ptr;
  2132. appl_ofs = appl_ptr % runtime->buffer_size;
  2133. snd_pcm_stream_unlock_irq(substream);
  2134. err = transfer(substream, appl_ofs, data, offset, frames);
  2135. snd_pcm_stream_lock_irq(substream);
  2136. if (err < 0)
  2137. goto _end_unlock;
  2138. switch (runtime->status->state) {
  2139. case SNDRV_PCM_STATE_XRUN:
  2140. err = -EPIPE;
  2141. goto _end_unlock;
  2142. case SNDRV_PCM_STATE_SUSPENDED:
  2143. err = -ESTRPIPE;
  2144. goto _end_unlock;
  2145. default:
  2146. break;
  2147. }
  2148. appl_ptr += frames;
  2149. if (appl_ptr >= runtime->boundary)
  2150. appl_ptr -= runtime->boundary;
  2151. runtime->control->appl_ptr = appl_ptr;
  2152. if (substream->ops->ack)
  2153. substream->ops->ack(substream);
  2154. offset += frames;
  2155. size -= frames;
  2156. xfer += frames;
  2157. avail -= frames;
  2158. }
  2159. _end_unlock:
  2160. runtime->twake = 0;
  2161. if (xfer > 0 && err >= 0)
  2162. snd_pcm_update_state(substream, runtime);
  2163. snd_pcm_stream_unlock_irq(substream);
  2164. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  2165. }
  2166. snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
  2167. {
  2168. struct snd_pcm_runtime *runtime;
  2169. int nonblock;
  2170. int err;
  2171. err = pcm_sanity_check(substream);
  2172. if (err < 0)
  2173. return err;
  2174. runtime = substream->runtime;
  2175. nonblock = !!(substream->f_flags & O_NONBLOCK);
  2176. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
  2177. return -EINVAL;
  2178. return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
  2179. }
  2180. EXPORT_SYMBOL(snd_pcm_lib_read);
  2181. static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
  2182. unsigned int hwoff,
  2183. unsigned long data, unsigned int off,
  2184. snd_pcm_uframes_t frames)
  2185. {
  2186. struct snd_pcm_runtime *runtime = substream->runtime;
  2187. int err;
  2188. void __user **bufs = (void __user **)data;
  2189. int channels = runtime->channels;
  2190. int c;
  2191. if (substream->ops->copy) {
  2192. for (c = 0; c < channels; ++c, ++bufs) {
  2193. char __user *buf;
  2194. if (*bufs == NULL)
  2195. continue;
  2196. buf = *bufs + samples_to_bytes(runtime, off);
  2197. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  2198. return err;
  2199. }
  2200. } else {
  2201. snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
  2202. for (c = 0; c < channels; ++c, ++bufs) {
  2203. char *hwbuf;
  2204. char __user *buf;
  2205. if (*bufs == NULL)
  2206. continue;
  2207. hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  2208. buf = *bufs + samples_to_bytes(runtime, off);
  2209. if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
  2210. return -EFAULT;
  2211. }
  2212. }
  2213. return 0;
  2214. }
  2215. snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
  2216. void __user **bufs,
  2217. snd_pcm_uframes_t frames)
  2218. {
  2219. struct snd_pcm_runtime *runtime;
  2220. int nonblock;
  2221. int err;
  2222. err = pcm_sanity_check(substream);
  2223. if (err < 0)
  2224. return err;
  2225. runtime = substream->runtime;
  2226. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2227. return -EBADFD;
  2228. nonblock = !!(substream->f_flags & O_NONBLOCK);
  2229. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  2230. return -EINVAL;
  2231. return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
  2232. }
  2233. EXPORT_SYMBOL(snd_pcm_lib_readv);
  2234. /*
  2235. * standard channel mapping helpers
  2236. */
  2237. /* default channel maps for multi-channel playbacks, up to 8 channels */
  2238. const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
  2239. { .channels = 1,
  2240. .map = { SNDRV_CHMAP_MONO } },
  2241. { .channels = 2,
  2242. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  2243. { .channels = 4,
  2244. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2245. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  2246. { .channels = 6,
  2247. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2248. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  2249. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
  2250. { .channels = 8,
  2251. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2252. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  2253. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  2254. SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
  2255. { }
  2256. };
  2257. EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
  2258. /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
  2259. const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
  2260. { .channels = 1,
  2261. .map = { SNDRV_CHMAP_MONO } },
  2262. { .channels = 2,
  2263. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  2264. { .channels = 4,
  2265. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2266. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  2267. { .channels = 6,
  2268. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2269. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  2270. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  2271. { .channels = 8,
  2272. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2273. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  2274. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  2275. SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
  2276. { }
  2277. };
  2278. EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
  2279. static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
  2280. {
  2281. if (ch > info->max_channels)
  2282. return false;
  2283. return !info->channel_mask || (info->channel_mask & (1U << ch));
  2284. }
  2285. static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
  2286. struct snd_ctl_elem_info *uinfo)
  2287. {
  2288. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2289. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2290. uinfo->count = 0;
  2291. uinfo->count = info->max_channels;
  2292. uinfo->value.integer.min = 0;
  2293. uinfo->value.integer.max = SNDRV_CHMAP_LAST;
  2294. return 0;
  2295. }
  2296. /* get callback for channel map ctl element
  2297. * stores the channel position firstly matching with the current channels
  2298. */
  2299. static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  2300. struct snd_ctl_elem_value *ucontrol)
  2301. {
  2302. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2303. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  2304. struct snd_pcm_substream *substream;
  2305. const struct snd_pcm_chmap_elem *map;
  2306. if (snd_BUG_ON(!info->chmap))
  2307. return -EINVAL;
  2308. substream = snd_pcm_chmap_substream(info, idx);
  2309. if (!substream)
  2310. return -ENODEV;
  2311. memset(ucontrol->value.integer.value, 0,
  2312. sizeof(ucontrol->value.integer.value));
  2313. if (!substream->runtime)
  2314. return 0; /* no channels set */
  2315. for (map = info->chmap; map->channels; map++) {
  2316. int i;
  2317. if (map->channels == substream->runtime->channels &&
  2318. valid_chmap_channels(info, map->channels)) {
  2319. for (i = 0; i < map->channels; i++)
  2320. ucontrol->value.integer.value[i] = map->map[i];
  2321. return 0;
  2322. }
  2323. }
  2324. return -EINVAL;
  2325. }
  2326. /* tlv callback for channel map ctl element
  2327. * expands the pre-defined channel maps in a form of TLV
  2328. */
  2329. static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  2330. unsigned int size, unsigned int __user *tlv)
  2331. {
  2332. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2333. const struct snd_pcm_chmap_elem *map;
  2334. unsigned int __user *dst;
  2335. int c, count = 0;
  2336. if (snd_BUG_ON(!info->chmap))
  2337. return -EINVAL;
  2338. if (size < 8)
  2339. return -ENOMEM;
  2340. if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
  2341. return -EFAULT;
  2342. size -= 8;
  2343. dst = tlv + 2;
  2344. for (map = info->chmap; map->channels; map++) {
  2345. int chs_bytes = map->channels * 4;
  2346. if (!valid_chmap_channels(info, map->channels))
  2347. continue;
  2348. if (size < 8)
  2349. return -ENOMEM;
  2350. if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
  2351. put_user(chs_bytes, dst + 1))
  2352. return -EFAULT;
  2353. dst += 2;
  2354. size -= 8;
  2355. count += 8;
  2356. if (size < chs_bytes)
  2357. return -ENOMEM;
  2358. size -= chs_bytes;
  2359. count += chs_bytes;
  2360. for (c = 0; c < map->channels; c++) {
  2361. if (put_user(map->map[c], dst))
  2362. return -EFAULT;
  2363. dst++;
  2364. }
  2365. }
  2366. if (put_user(count, tlv + 1))
  2367. return -EFAULT;
  2368. return 0;
  2369. }
  2370. static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
  2371. {
  2372. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2373. info->pcm->streams[info->stream].chmap_kctl = NULL;
  2374. kfree(info);
  2375. }
  2376. /**
  2377. * snd_pcm_add_chmap_ctls - create channel-mapping control elements
  2378. * @pcm: the assigned PCM instance
  2379. * @stream: stream direction
  2380. * @chmap: channel map elements (for query)
  2381. * @max_channels: the max number of channels for the stream
  2382. * @private_value: the value passed to each kcontrol's private_value field
  2383. * @info_ret: store struct snd_pcm_chmap instance if non-NULL
  2384. *
  2385. * Create channel-mapping control elements assigned to the given PCM stream(s).
  2386. * Return: Zero if successful, or a negative error value.
  2387. */
  2388. int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
  2389. const struct snd_pcm_chmap_elem *chmap,
  2390. int max_channels,
  2391. unsigned long private_value,
  2392. struct snd_pcm_chmap **info_ret)
  2393. {
  2394. struct snd_pcm_chmap *info;
  2395. struct snd_kcontrol_new knew = {
  2396. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2397. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  2398. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  2399. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
  2400. .info = pcm_chmap_ctl_info,
  2401. .get = pcm_chmap_ctl_get,
  2402. .tlv.c = pcm_chmap_ctl_tlv,
  2403. };
  2404. int err;
  2405. info = kzalloc(sizeof(*info), GFP_KERNEL);
  2406. if (!info)
  2407. return -ENOMEM;
  2408. info->pcm = pcm;
  2409. info->stream = stream;
  2410. info->chmap = chmap;
  2411. info->max_channels = max_channels;
  2412. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  2413. knew.name = "Playback Channel Map";
  2414. else
  2415. knew.name = "Capture Channel Map";
  2416. knew.device = pcm->device;
  2417. knew.count = pcm->streams[stream].substream_count;
  2418. knew.private_value = private_value;
  2419. info->kctl = snd_ctl_new1(&knew, info);
  2420. if (!info->kctl) {
  2421. kfree(info);
  2422. return -ENOMEM;
  2423. }
  2424. info->kctl->private_free = pcm_chmap_ctl_private_free;
  2425. err = snd_ctl_add(pcm->card, info->kctl);
  2426. if (err < 0)
  2427. return err;
  2428. pcm->streams[stream].chmap_kctl = info->kctl;
  2429. if (info_ret)
  2430. *info_ret = info;
  2431. return 0;
  2432. }
  2433. EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);