pcm_lib.c 70 KB

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