pcm_lib.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593
  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. snd_printd(KERN_DEBUG "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. if (printk_ratelimit()) { \
  177. snd_printd("PCM: " fmt, ##args); \
  178. } \
  179. dump_stack_on_xrun(substream); \
  180. } \
  181. } while (0)
  182. #define XRUN_LOG_CNT 10
  183. struct hwptr_log_entry {
  184. unsigned int in_interrupt;
  185. unsigned long jiffies;
  186. snd_pcm_uframes_t pos;
  187. snd_pcm_uframes_t period_size;
  188. snd_pcm_uframes_t buffer_size;
  189. snd_pcm_uframes_t old_hw_ptr;
  190. snd_pcm_uframes_t hw_ptr_base;
  191. };
  192. struct snd_pcm_hwptr_log {
  193. unsigned int idx;
  194. unsigned int hit: 1;
  195. struct hwptr_log_entry entries[XRUN_LOG_CNT];
  196. };
  197. static void xrun_log(struct snd_pcm_substream *substream,
  198. snd_pcm_uframes_t pos, int in_interrupt)
  199. {
  200. struct snd_pcm_runtime *runtime = substream->runtime;
  201. struct snd_pcm_hwptr_log *log = runtime->hwptr_log;
  202. struct hwptr_log_entry *entry;
  203. if (log == NULL) {
  204. log = kzalloc(sizeof(*log), GFP_ATOMIC);
  205. if (log == NULL)
  206. return;
  207. runtime->hwptr_log = log;
  208. } else {
  209. if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
  210. return;
  211. }
  212. entry = &log->entries[log->idx];
  213. entry->in_interrupt = in_interrupt;
  214. entry->jiffies = jiffies;
  215. entry->pos = pos;
  216. entry->period_size = runtime->period_size;
  217. entry->buffer_size = runtime->buffer_size;
  218. entry->old_hw_ptr = runtime->status->hw_ptr;
  219. entry->hw_ptr_base = runtime->hw_ptr_base;
  220. log->idx = (log->idx + 1) % XRUN_LOG_CNT;
  221. }
  222. static void xrun_log_show(struct snd_pcm_substream *substream)
  223. {
  224. struct snd_pcm_hwptr_log *log = substream->runtime->hwptr_log;
  225. struct hwptr_log_entry *entry;
  226. char name[16];
  227. unsigned int idx;
  228. int cnt;
  229. if (log == NULL)
  230. return;
  231. if (xrun_debug(substream, XRUN_DEBUG_LOGONCE) && log->hit)
  232. return;
  233. snd_pcm_debug_name(substream, name, sizeof(name));
  234. for (cnt = 0, idx = log->idx; cnt < XRUN_LOG_CNT; cnt++) {
  235. entry = &log->entries[idx];
  236. if (entry->period_size == 0)
  237. break;
  238. snd_printd("hwptr log: %s: %sj=%lu, pos=%ld/%ld/%ld, "
  239. "hwptr=%ld/%ld\n",
  240. name, entry->in_interrupt ? "[Q] " : "",
  241. entry->jiffies,
  242. (unsigned long)entry->pos,
  243. (unsigned long)entry->period_size,
  244. (unsigned long)entry->buffer_size,
  245. (unsigned long)entry->old_hw_ptr,
  246. (unsigned long)entry->hw_ptr_base);
  247. idx++;
  248. idx %= XRUN_LOG_CNT;
  249. }
  250. log->hit = 1;
  251. }
  252. #else /* ! CONFIG_SND_PCM_XRUN_DEBUG */
  253. #define hw_ptr_error(substream, fmt, args...) do { } while (0)
  254. #define xrun_log(substream, pos, in_interrupt) do { } while (0)
  255. #define xrun_log_show(substream) do { } while (0)
  256. #endif
  257. int snd_pcm_update_state(struct snd_pcm_substream *substream,
  258. struct snd_pcm_runtime *runtime)
  259. {
  260. snd_pcm_uframes_t avail;
  261. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  262. avail = snd_pcm_playback_avail(runtime);
  263. else
  264. avail = snd_pcm_capture_avail(runtime);
  265. if (avail > runtime->avail_max)
  266. runtime->avail_max = avail;
  267. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  268. if (avail >= runtime->buffer_size) {
  269. snd_pcm_drain_done(substream);
  270. return -EPIPE;
  271. }
  272. } else {
  273. if (avail >= runtime->stop_threshold) {
  274. xrun(substream);
  275. return -EPIPE;
  276. }
  277. }
  278. if (runtime->twake) {
  279. if (avail >= runtime->twake)
  280. wake_up(&runtime->tsleep);
  281. } else if (avail >= runtime->control->avail_min)
  282. wake_up(&runtime->sleep);
  283. return 0;
  284. }
  285. static int snd_pcm_update_hw_ptr0(struct snd_pcm_substream *substream,
  286. unsigned int in_interrupt)
  287. {
  288. struct snd_pcm_runtime *runtime = substream->runtime;
  289. snd_pcm_uframes_t pos;
  290. snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
  291. snd_pcm_sframes_t hdelta, delta;
  292. unsigned long jdelta;
  293. unsigned long curr_jiffies;
  294. struct timespec curr_tstamp;
  295. struct timespec audio_tstamp;
  296. int crossed_boundary = 0;
  297. old_hw_ptr = runtime->status->hw_ptr;
  298. /*
  299. * group pointer, time and jiffies reads to allow for more
  300. * accurate correlations/corrections.
  301. * The values are stored at the end of this routine after
  302. * corrections for hw_ptr position
  303. */
  304. pos = substream->ops->pointer(substream);
  305. curr_jiffies = jiffies;
  306. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
  307. snd_pcm_gettime(runtime, (struct timespec *)&curr_tstamp);
  308. if ((runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK) &&
  309. (substream->ops->wall_clock))
  310. substream->ops->wall_clock(substream, &audio_tstamp);
  311. }
  312. if (pos == SNDRV_PCM_POS_XRUN) {
  313. xrun(substream);
  314. return -EPIPE;
  315. }
  316. if (pos >= runtime->buffer_size) {
  317. if (printk_ratelimit()) {
  318. char name[16];
  319. snd_pcm_debug_name(substream, name, sizeof(name));
  320. xrun_log_show(substream);
  321. snd_printd(KERN_ERR "BUG: %s, pos = %ld, "
  322. "buffer size = %ld, period size = %ld\n",
  323. name, pos, runtime->buffer_size,
  324. runtime->period_size);
  325. }
  326. pos = 0;
  327. }
  328. pos -= pos % runtime->min_align;
  329. if (xrun_debug(substream, XRUN_DEBUG_LOG))
  330. xrun_log(substream, pos, in_interrupt);
  331. hw_base = runtime->hw_ptr_base;
  332. new_hw_ptr = hw_base + pos;
  333. if (in_interrupt) {
  334. /* we know that one period was processed */
  335. /* delta = "expected next hw_ptr" for in_interrupt != 0 */
  336. delta = runtime->hw_ptr_interrupt + runtime->period_size;
  337. if (delta > new_hw_ptr) {
  338. /* check for double acknowledged interrupts */
  339. hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
  340. if (hdelta > runtime->hw_ptr_buffer_jiffies/2) {
  341. hw_base += runtime->buffer_size;
  342. if (hw_base >= runtime->boundary) {
  343. hw_base = 0;
  344. crossed_boundary++;
  345. }
  346. new_hw_ptr = hw_base + pos;
  347. goto __delta;
  348. }
  349. }
  350. }
  351. /* new_hw_ptr might be lower than old_hw_ptr in case when */
  352. /* pointer crosses the end of the ring buffer */
  353. if (new_hw_ptr < old_hw_ptr) {
  354. hw_base += runtime->buffer_size;
  355. if (hw_base >= runtime->boundary) {
  356. hw_base = 0;
  357. crossed_boundary++;
  358. }
  359. new_hw_ptr = hw_base + pos;
  360. }
  361. __delta:
  362. delta = new_hw_ptr - old_hw_ptr;
  363. if (delta < 0)
  364. delta += runtime->boundary;
  365. if (xrun_debug(substream, in_interrupt ?
  366. XRUN_DEBUG_PERIODUPDATE : XRUN_DEBUG_HWPTRUPDATE)) {
  367. char name[16];
  368. snd_pcm_debug_name(substream, name, sizeof(name));
  369. snd_printd("%s_update: %s: pos=%u/%u/%u, "
  370. "hwptr=%ld/%ld/%ld/%ld\n",
  371. in_interrupt ? "period" : "hwptr",
  372. name,
  373. (unsigned int)pos,
  374. (unsigned int)runtime->period_size,
  375. (unsigned int)runtime->buffer_size,
  376. (unsigned long)delta,
  377. (unsigned long)old_hw_ptr,
  378. (unsigned long)new_hw_ptr,
  379. (unsigned long)runtime->hw_ptr_base);
  380. }
  381. if (runtime->no_period_wakeup) {
  382. snd_pcm_sframes_t xrun_threshold;
  383. /*
  384. * Without regular period interrupts, we have to check
  385. * the elapsed time to detect xruns.
  386. */
  387. jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
  388. if (jdelta < runtime->hw_ptr_buffer_jiffies / 2)
  389. goto no_delta_check;
  390. hdelta = jdelta - delta * HZ / runtime->rate;
  391. xrun_threshold = runtime->hw_ptr_buffer_jiffies / 2 + 1;
  392. while (hdelta > xrun_threshold) {
  393. delta += runtime->buffer_size;
  394. hw_base += runtime->buffer_size;
  395. if (hw_base >= runtime->boundary) {
  396. hw_base = 0;
  397. crossed_boundary++;
  398. }
  399. new_hw_ptr = hw_base + pos;
  400. hdelta -= runtime->hw_ptr_buffer_jiffies;
  401. }
  402. goto no_delta_check;
  403. }
  404. /* something must be really wrong */
  405. if (delta >= runtime->buffer_size + runtime->period_size) {
  406. hw_ptr_error(substream,
  407. "Unexpected hw_pointer value %s"
  408. "(stream=%i, pos=%ld, new_hw_ptr=%ld, "
  409. "old_hw_ptr=%ld)\n",
  410. in_interrupt ? "[Q] " : "[P]",
  411. substream->stream, (long)pos,
  412. (long)new_hw_ptr, (long)old_hw_ptr);
  413. return 0;
  414. }
  415. /* Do jiffies check only in xrun_debug mode */
  416. if (!xrun_debug(substream, XRUN_DEBUG_JIFFIESCHECK))
  417. goto no_jiffies_check;
  418. /* Skip the jiffies check for hardwares with BATCH flag.
  419. * Such hardware usually just increases the position at each IRQ,
  420. * thus it can't give any strange position.
  421. */
  422. if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
  423. goto no_jiffies_check;
  424. hdelta = delta;
  425. if (hdelta < runtime->delay)
  426. goto no_jiffies_check;
  427. hdelta -= runtime->delay;
  428. jdelta = curr_jiffies - runtime->hw_ptr_jiffies;
  429. if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
  430. delta = jdelta /
  431. (((runtime->period_size * HZ) / runtime->rate)
  432. + HZ/100);
  433. /* move new_hw_ptr according jiffies not pos variable */
  434. new_hw_ptr = old_hw_ptr;
  435. hw_base = delta;
  436. /* use loop to avoid checks for delta overflows */
  437. /* the delta value is small or zero in most cases */
  438. while (delta > 0) {
  439. new_hw_ptr += runtime->period_size;
  440. if (new_hw_ptr >= runtime->boundary) {
  441. new_hw_ptr -= runtime->boundary;
  442. crossed_boundary--;
  443. }
  444. delta--;
  445. }
  446. /* align hw_base to buffer_size */
  447. hw_ptr_error(substream,
  448. "hw_ptr skipping! %s"
  449. "(pos=%ld, delta=%ld, period=%ld, "
  450. "jdelta=%lu/%lu/%lu, hw_ptr=%ld/%ld)\n",
  451. in_interrupt ? "[Q] " : "",
  452. (long)pos, (long)hdelta,
  453. (long)runtime->period_size, jdelta,
  454. ((hdelta * HZ) / runtime->rate), hw_base,
  455. (unsigned long)old_hw_ptr,
  456. (unsigned long)new_hw_ptr);
  457. /* reset values to proper state */
  458. delta = 0;
  459. hw_base = new_hw_ptr - (new_hw_ptr % runtime->buffer_size);
  460. }
  461. no_jiffies_check:
  462. if (delta > runtime->period_size + runtime->period_size / 2) {
  463. hw_ptr_error(substream,
  464. "Lost interrupts? %s"
  465. "(stream=%i, delta=%ld, new_hw_ptr=%ld, "
  466. "old_hw_ptr=%ld)\n",
  467. in_interrupt ? "[Q] " : "",
  468. substream->stream, (long)delta,
  469. (long)new_hw_ptr,
  470. (long)old_hw_ptr);
  471. }
  472. no_delta_check:
  473. if (runtime->status->hw_ptr == new_hw_ptr)
  474. return 0;
  475. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  476. runtime->silence_size > 0)
  477. snd_pcm_playback_silence(substream, new_hw_ptr);
  478. if (in_interrupt) {
  479. delta = new_hw_ptr - runtime->hw_ptr_interrupt;
  480. if (delta < 0)
  481. delta += runtime->boundary;
  482. delta -= (snd_pcm_uframes_t)delta % runtime->period_size;
  483. runtime->hw_ptr_interrupt += delta;
  484. if (runtime->hw_ptr_interrupt >= runtime->boundary)
  485. runtime->hw_ptr_interrupt -= runtime->boundary;
  486. }
  487. runtime->hw_ptr_base = hw_base;
  488. runtime->status->hw_ptr = new_hw_ptr;
  489. runtime->hw_ptr_jiffies = curr_jiffies;
  490. if (crossed_boundary) {
  491. snd_BUG_ON(crossed_boundary != 1);
  492. runtime->hw_ptr_wrap += runtime->boundary;
  493. }
  494. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
  495. runtime->status->tstamp = curr_tstamp;
  496. if (!(runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)) {
  497. /*
  498. * no wall clock available, provide audio timestamp
  499. * derived from pointer position+delay
  500. */
  501. u64 audio_frames, audio_nsecs;
  502. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  503. audio_frames = runtime->hw_ptr_wrap
  504. + runtime->status->hw_ptr
  505. - runtime->delay;
  506. else
  507. audio_frames = runtime->hw_ptr_wrap
  508. + runtime->status->hw_ptr
  509. + runtime->delay;
  510. audio_nsecs = div_u64(audio_frames * 1000000000LL,
  511. runtime->rate);
  512. audio_tstamp = ns_to_timespec(audio_nsecs);
  513. }
  514. runtime->status->audio_tstamp = audio_tstamp;
  515. }
  516. return snd_pcm_update_state(substream, runtime);
  517. }
  518. /* CAUTION: call it with irq disabled */
  519. int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
  520. {
  521. return snd_pcm_update_hw_ptr0(substream, 0);
  522. }
  523. /**
  524. * snd_pcm_set_ops - set the PCM operators
  525. * @pcm: the pcm instance
  526. * @direction: stream direction, SNDRV_PCM_STREAM_XXX
  527. * @ops: the operator table
  528. *
  529. * Sets the given PCM operators to the pcm instance.
  530. */
  531. void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
  532. {
  533. struct snd_pcm_str *stream = &pcm->streams[direction];
  534. struct snd_pcm_substream *substream;
  535. for (substream = stream->substream; substream != NULL; substream = substream->next)
  536. substream->ops = ops;
  537. }
  538. EXPORT_SYMBOL(snd_pcm_set_ops);
  539. /**
  540. * snd_pcm_sync - set the PCM sync id
  541. * @substream: the pcm substream
  542. *
  543. * Sets the PCM sync identifier for the card.
  544. */
  545. void snd_pcm_set_sync(struct snd_pcm_substream *substream)
  546. {
  547. struct snd_pcm_runtime *runtime = substream->runtime;
  548. runtime->sync.id32[0] = substream->pcm->card->number;
  549. runtime->sync.id32[1] = -1;
  550. runtime->sync.id32[2] = -1;
  551. runtime->sync.id32[3] = -1;
  552. }
  553. EXPORT_SYMBOL(snd_pcm_set_sync);
  554. /*
  555. * Standard ioctl routine
  556. */
  557. static inline unsigned int div32(unsigned int a, unsigned int b,
  558. unsigned int *r)
  559. {
  560. if (b == 0) {
  561. *r = 0;
  562. return UINT_MAX;
  563. }
  564. *r = a % b;
  565. return a / b;
  566. }
  567. static inline unsigned int div_down(unsigned int a, unsigned int b)
  568. {
  569. if (b == 0)
  570. return UINT_MAX;
  571. return a / b;
  572. }
  573. static inline unsigned int div_up(unsigned int a, unsigned int b)
  574. {
  575. unsigned int r;
  576. unsigned int q;
  577. if (b == 0)
  578. return UINT_MAX;
  579. q = div32(a, b, &r);
  580. if (r)
  581. ++q;
  582. return q;
  583. }
  584. static inline unsigned int mul(unsigned int a, unsigned int b)
  585. {
  586. if (a == 0)
  587. return 0;
  588. if (div_down(UINT_MAX, a) < b)
  589. return UINT_MAX;
  590. return a * b;
  591. }
  592. static inline unsigned int muldiv32(unsigned int a, unsigned int b,
  593. unsigned int c, unsigned int *r)
  594. {
  595. u_int64_t n = (u_int64_t) a * b;
  596. if (c == 0) {
  597. snd_BUG_ON(!n);
  598. *r = 0;
  599. return UINT_MAX;
  600. }
  601. n = div_u64_rem(n, c, r);
  602. if (n >= UINT_MAX) {
  603. *r = 0;
  604. return UINT_MAX;
  605. }
  606. return n;
  607. }
  608. /**
  609. * snd_interval_refine - refine the interval value of configurator
  610. * @i: the interval value to refine
  611. * @v: the interval value to refer to
  612. *
  613. * Refines the interval value with the reference value.
  614. * The interval is changed to the range satisfying both intervals.
  615. * The interval status (min, max, integer, etc.) are evaluated.
  616. *
  617. * Return: Positive if the value is changed, zero if it's not changed, or a
  618. * negative error code.
  619. */
  620. int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
  621. {
  622. int changed = 0;
  623. if (snd_BUG_ON(snd_interval_empty(i)))
  624. return -EINVAL;
  625. if (i->min < v->min) {
  626. i->min = v->min;
  627. i->openmin = v->openmin;
  628. changed = 1;
  629. } else if (i->min == v->min && !i->openmin && v->openmin) {
  630. i->openmin = 1;
  631. changed = 1;
  632. }
  633. if (i->max > v->max) {
  634. i->max = v->max;
  635. i->openmax = v->openmax;
  636. changed = 1;
  637. } else if (i->max == v->max && !i->openmax && v->openmax) {
  638. i->openmax = 1;
  639. changed = 1;
  640. }
  641. if (!i->integer && v->integer) {
  642. i->integer = 1;
  643. changed = 1;
  644. }
  645. if (i->integer) {
  646. if (i->openmin) {
  647. i->min++;
  648. i->openmin = 0;
  649. }
  650. if (i->openmax) {
  651. i->max--;
  652. i->openmax = 0;
  653. }
  654. } else if (!i->openmin && !i->openmax && i->min == i->max)
  655. i->integer = 1;
  656. if (snd_interval_checkempty(i)) {
  657. snd_interval_none(i);
  658. return -EINVAL;
  659. }
  660. return changed;
  661. }
  662. EXPORT_SYMBOL(snd_interval_refine);
  663. static int snd_interval_refine_first(struct snd_interval *i)
  664. {
  665. if (snd_BUG_ON(snd_interval_empty(i)))
  666. return -EINVAL;
  667. if (snd_interval_single(i))
  668. return 0;
  669. i->max = i->min;
  670. i->openmax = i->openmin;
  671. if (i->openmax)
  672. i->max++;
  673. return 1;
  674. }
  675. static int snd_interval_refine_last(struct snd_interval *i)
  676. {
  677. if (snd_BUG_ON(snd_interval_empty(i)))
  678. return -EINVAL;
  679. if (snd_interval_single(i))
  680. return 0;
  681. i->min = i->max;
  682. i->openmin = i->openmax;
  683. if (i->openmin)
  684. i->min--;
  685. return 1;
  686. }
  687. void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
  688. {
  689. if (a->empty || b->empty) {
  690. snd_interval_none(c);
  691. return;
  692. }
  693. c->empty = 0;
  694. c->min = mul(a->min, b->min);
  695. c->openmin = (a->openmin || b->openmin);
  696. c->max = mul(a->max, b->max);
  697. c->openmax = (a->openmax || b->openmax);
  698. c->integer = (a->integer && b->integer);
  699. }
  700. /**
  701. * snd_interval_div - refine the interval value with division
  702. * @a: dividend
  703. * @b: divisor
  704. * @c: quotient
  705. *
  706. * c = a / b
  707. *
  708. * Returns non-zero if the value is changed, zero if not changed.
  709. */
  710. void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
  711. {
  712. unsigned int r;
  713. if (a->empty || b->empty) {
  714. snd_interval_none(c);
  715. return;
  716. }
  717. c->empty = 0;
  718. c->min = div32(a->min, b->max, &r);
  719. c->openmin = (r || a->openmin || b->openmax);
  720. if (b->min > 0) {
  721. c->max = div32(a->max, b->min, &r);
  722. if (r) {
  723. c->max++;
  724. c->openmax = 1;
  725. } else
  726. c->openmax = (a->openmax || b->openmin);
  727. } else {
  728. c->max = UINT_MAX;
  729. c->openmax = 0;
  730. }
  731. c->integer = 0;
  732. }
  733. /**
  734. * snd_interval_muldivk - refine the interval value
  735. * @a: dividend 1
  736. * @b: dividend 2
  737. * @k: divisor (as integer)
  738. * @c: result
  739. *
  740. * c = a * b / k
  741. *
  742. * Returns non-zero if the value is changed, zero if not changed.
  743. */
  744. void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
  745. unsigned int k, struct snd_interval *c)
  746. {
  747. unsigned int r;
  748. if (a->empty || b->empty) {
  749. snd_interval_none(c);
  750. return;
  751. }
  752. c->empty = 0;
  753. c->min = muldiv32(a->min, b->min, k, &r);
  754. c->openmin = (r || a->openmin || b->openmin);
  755. c->max = muldiv32(a->max, b->max, k, &r);
  756. if (r) {
  757. c->max++;
  758. c->openmax = 1;
  759. } else
  760. c->openmax = (a->openmax || b->openmax);
  761. c->integer = 0;
  762. }
  763. /**
  764. * snd_interval_mulkdiv - refine the interval value
  765. * @a: dividend 1
  766. * @k: dividend 2 (as integer)
  767. * @b: divisor
  768. * @c: result
  769. *
  770. * c = a * k / b
  771. *
  772. * Returns non-zero if the value is changed, zero if not changed.
  773. */
  774. void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
  775. const struct snd_interval *b, struct snd_interval *c)
  776. {
  777. unsigned int r;
  778. if (a->empty || b->empty) {
  779. snd_interval_none(c);
  780. return;
  781. }
  782. c->empty = 0;
  783. c->min = muldiv32(a->min, k, b->max, &r);
  784. c->openmin = (r || a->openmin || b->openmax);
  785. if (b->min > 0) {
  786. c->max = muldiv32(a->max, k, b->min, &r);
  787. if (r) {
  788. c->max++;
  789. c->openmax = 1;
  790. } else
  791. c->openmax = (a->openmax || b->openmin);
  792. } else {
  793. c->max = UINT_MAX;
  794. c->openmax = 0;
  795. }
  796. c->integer = 0;
  797. }
  798. /* ---- */
  799. /**
  800. * snd_interval_ratnum - refine the interval value
  801. * @i: interval to refine
  802. * @rats_count: number of ratnum_t
  803. * @rats: ratnum_t array
  804. * @nump: pointer to store the resultant numerator
  805. * @denp: pointer to store the resultant denominator
  806. *
  807. * Return: Positive if the value is changed, zero if it's not changed, or a
  808. * negative error code.
  809. */
  810. int snd_interval_ratnum(struct snd_interval *i,
  811. unsigned int rats_count, struct snd_ratnum *rats,
  812. unsigned int *nump, unsigned int *denp)
  813. {
  814. unsigned int best_num, best_den;
  815. int best_diff;
  816. unsigned int k;
  817. struct snd_interval t;
  818. int err;
  819. unsigned int result_num, result_den;
  820. int result_diff;
  821. best_num = best_den = best_diff = 0;
  822. for (k = 0; k < rats_count; ++k) {
  823. unsigned int num = rats[k].num;
  824. unsigned int den;
  825. unsigned int q = i->min;
  826. int diff;
  827. if (q == 0)
  828. q = 1;
  829. den = div_up(num, q);
  830. if (den < rats[k].den_min)
  831. continue;
  832. if (den > rats[k].den_max)
  833. den = rats[k].den_max;
  834. else {
  835. unsigned int r;
  836. r = (den - rats[k].den_min) % rats[k].den_step;
  837. if (r != 0)
  838. den -= r;
  839. }
  840. diff = num - q * den;
  841. if (diff < 0)
  842. diff = -diff;
  843. if (best_num == 0 ||
  844. diff * best_den < best_diff * den) {
  845. best_diff = diff;
  846. best_den = den;
  847. best_num = num;
  848. }
  849. }
  850. if (best_den == 0) {
  851. i->empty = 1;
  852. return -EINVAL;
  853. }
  854. t.min = div_down(best_num, best_den);
  855. t.openmin = !!(best_num % best_den);
  856. result_num = best_num;
  857. result_diff = best_diff;
  858. result_den = best_den;
  859. best_num = best_den = best_diff = 0;
  860. for (k = 0; k < rats_count; ++k) {
  861. unsigned int num = rats[k].num;
  862. unsigned int den;
  863. unsigned int q = i->max;
  864. int diff;
  865. if (q == 0) {
  866. i->empty = 1;
  867. return -EINVAL;
  868. }
  869. den = div_down(num, q);
  870. if (den > rats[k].den_max)
  871. continue;
  872. if (den < rats[k].den_min)
  873. den = rats[k].den_min;
  874. else {
  875. unsigned int r;
  876. r = (den - rats[k].den_min) % rats[k].den_step;
  877. if (r != 0)
  878. den += rats[k].den_step - r;
  879. }
  880. diff = q * den - num;
  881. if (diff < 0)
  882. diff = -diff;
  883. if (best_num == 0 ||
  884. diff * best_den < best_diff * den) {
  885. best_diff = diff;
  886. best_den = den;
  887. best_num = num;
  888. }
  889. }
  890. if (best_den == 0) {
  891. i->empty = 1;
  892. return -EINVAL;
  893. }
  894. t.max = div_up(best_num, best_den);
  895. t.openmax = !!(best_num % best_den);
  896. t.integer = 0;
  897. err = snd_interval_refine(i, &t);
  898. if (err < 0)
  899. return err;
  900. if (snd_interval_single(i)) {
  901. if (best_diff * result_den < result_diff * best_den) {
  902. result_num = best_num;
  903. result_den = best_den;
  904. }
  905. if (nump)
  906. *nump = result_num;
  907. if (denp)
  908. *denp = result_den;
  909. }
  910. return err;
  911. }
  912. EXPORT_SYMBOL(snd_interval_ratnum);
  913. /**
  914. * snd_interval_ratden - refine the interval value
  915. * @i: interval to refine
  916. * @rats_count: number of struct ratden
  917. * @rats: struct ratden array
  918. * @nump: pointer to store the resultant numerator
  919. * @denp: pointer to store the resultant denominator
  920. *
  921. * Return: Positive if the value is changed, zero if it's not changed, or a
  922. * negative error code.
  923. */
  924. static int snd_interval_ratden(struct snd_interval *i,
  925. unsigned int rats_count, struct snd_ratden *rats,
  926. unsigned int *nump, unsigned int *denp)
  927. {
  928. unsigned int best_num, best_diff, best_den;
  929. unsigned int k;
  930. struct snd_interval t;
  931. int err;
  932. best_num = best_den = best_diff = 0;
  933. for (k = 0; k < rats_count; ++k) {
  934. unsigned int num;
  935. unsigned int den = rats[k].den;
  936. unsigned int q = i->min;
  937. int diff;
  938. num = mul(q, den);
  939. if (num > rats[k].num_max)
  940. continue;
  941. if (num < rats[k].num_min)
  942. num = rats[k].num_max;
  943. else {
  944. unsigned int r;
  945. r = (num - rats[k].num_min) % rats[k].num_step;
  946. if (r != 0)
  947. num += rats[k].num_step - r;
  948. }
  949. diff = num - q * den;
  950. if (best_num == 0 ||
  951. diff * best_den < best_diff * den) {
  952. best_diff = diff;
  953. best_den = den;
  954. best_num = num;
  955. }
  956. }
  957. if (best_den == 0) {
  958. i->empty = 1;
  959. return -EINVAL;
  960. }
  961. t.min = div_down(best_num, best_den);
  962. t.openmin = !!(best_num % best_den);
  963. best_num = best_den = best_diff = 0;
  964. for (k = 0; k < rats_count; ++k) {
  965. unsigned int num;
  966. unsigned int den = rats[k].den;
  967. unsigned int q = i->max;
  968. int diff;
  969. num = mul(q, den);
  970. if (num < rats[k].num_min)
  971. continue;
  972. if (num > rats[k].num_max)
  973. num = rats[k].num_max;
  974. else {
  975. unsigned int r;
  976. r = (num - rats[k].num_min) % rats[k].num_step;
  977. if (r != 0)
  978. num -= r;
  979. }
  980. diff = q * den - num;
  981. if (best_num == 0 ||
  982. diff * best_den < best_diff * den) {
  983. best_diff = diff;
  984. best_den = den;
  985. best_num = num;
  986. }
  987. }
  988. if (best_den == 0) {
  989. i->empty = 1;
  990. return -EINVAL;
  991. }
  992. t.max = div_up(best_num, best_den);
  993. t.openmax = !!(best_num % best_den);
  994. t.integer = 0;
  995. err = snd_interval_refine(i, &t);
  996. if (err < 0)
  997. return err;
  998. if (snd_interval_single(i)) {
  999. if (nump)
  1000. *nump = best_num;
  1001. if (denp)
  1002. *denp = best_den;
  1003. }
  1004. return err;
  1005. }
  1006. /**
  1007. * snd_interval_list - refine the interval value from the list
  1008. * @i: the interval value to refine
  1009. * @count: the number of elements in the list
  1010. * @list: the value list
  1011. * @mask: the bit-mask to evaluate
  1012. *
  1013. * Refines the interval value from the list.
  1014. * When mask is non-zero, only the elements corresponding to bit 1 are
  1015. * evaluated.
  1016. *
  1017. * Return: Positive if the value is changed, zero if it's not changed, or a
  1018. * negative error code.
  1019. */
  1020. int snd_interval_list(struct snd_interval *i, unsigned int count,
  1021. const unsigned int *list, unsigned int mask)
  1022. {
  1023. unsigned int k;
  1024. struct snd_interval list_range;
  1025. if (!count) {
  1026. i->empty = 1;
  1027. return -EINVAL;
  1028. }
  1029. snd_interval_any(&list_range);
  1030. list_range.min = UINT_MAX;
  1031. list_range.max = 0;
  1032. for (k = 0; k < count; k++) {
  1033. if (mask && !(mask & (1 << k)))
  1034. continue;
  1035. if (!snd_interval_test(i, list[k]))
  1036. continue;
  1037. list_range.min = min(list_range.min, list[k]);
  1038. list_range.max = max(list_range.max, list[k]);
  1039. }
  1040. return snd_interval_refine(i, &list_range);
  1041. }
  1042. EXPORT_SYMBOL(snd_interval_list);
  1043. static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
  1044. {
  1045. unsigned int n;
  1046. int changed = 0;
  1047. n = (i->min - min) % step;
  1048. if (n != 0 || i->openmin) {
  1049. i->min += step - n;
  1050. changed = 1;
  1051. }
  1052. n = (i->max - min) % step;
  1053. if (n != 0 || i->openmax) {
  1054. i->max -= n;
  1055. changed = 1;
  1056. }
  1057. if (snd_interval_checkempty(i)) {
  1058. i->empty = 1;
  1059. return -EINVAL;
  1060. }
  1061. return changed;
  1062. }
  1063. /* Info constraints helpers */
  1064. /**
  1065. * snd_pcm_hw_rule_add - add the hw-constraint rule
  1066. * @runtime: the pcm runtime instance
  1067. * @cond: condition bits
  1068. * @var: the variable to evaluate
  1069. * @func: the evaluation function
  1070. * @private: the private data pointer passed to function
  1071. * @dep: the dependent variables
  1072. *
  1073. * Return: Zero if successful, or a negative error code on failure.
  1074. */
  1075. int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
  1076. int var,
  1077. snd_pcm_hw_rule_func_t func, void *private,
  1078. int dep, ...)
  1079. {
  1080. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1081. struct snd_pcm_hw_rule *c;
  1082. unsigned int k;
  1083. va_list args;
  1084. va_start(args, dep);
  1085. if (constrs->rules_num >= constrs->rules_all) {
  1086. struct snd_pcm_hw_rule *new;
  1087. unsigned int new_rules = constrs->rules_all + 16;
  1088. new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
  1089. if (!new) {
  1090. va_end(args);
  1091. return -ENOMEM;
  1092. }
  1093. if (constrs->rules) {
  1094. memcpy(new, constrs->rules,
  1095. constrs->rules_num * sizeof(*c));
  1096. kfree(constrs->rules);
  1097. }
  1098. constrs->rules = new;
  1099. constrs->rules_all = new_rules;
  1100. }
  1101. c = &constrs->rules[constrs->rules_num];
  1102. c->cond = cond;
  1103. c->func = func;
  1104. c->var = var;
  1105. c->private = private;
  1106. k = 0;
  1107. while (1) {
  1108. if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
  1109. va_end(args);
  1110. return -EINVAL;
  1111. }
  1112. c->deps[k++] = dep;
  1113. if (dep < 0)
  1114. break;
  1115. dep = va_arg(args, int);
  1116. }
  1117. constrs->rules_num++;
  1118. va_end(args);
  1119. return 0;
  1120. }
  1121. EXPORT_SYMBOL(snd_pcm_hw_rule_add);
  1122. /**
  1123. * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
  1124. * @runtime: PCM runtime instance
  1125. * @var: hw_params variable to apply the mask
  1126. * @mask: the bitmap mask
  1127. *
  1128. * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
  1129. *
  1130. * Return: Zero if successful, or a negative error code on failure.
  1131. */
  1132. int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1133. u_int32_t mask)
  1134. {
  1135. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1136. struct snd_mask *maskp = constrs_mask(constrs, var);
  1137. *maskp->bits &= mask;
  1138. memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
  1139. if (*maskp->bits == 0)
  1140. return -EINVAL;
  1141. return 0;
  1142. }
  1143. /**
  1144. * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
  1145. * @runtime: PCM runtime instance
  1146. * @var: hw_params variable to apply the mask
  1147. * @mask: the 64bit bitmap mask
  1148. *
  1149. * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
  1150. *
  1151. * Return: Zero if successful, or a negative error code on failure.
  1152. */
  1153. int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1154. u_int64_t mask)
  1155. {
  1156. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1157. struct snd_mask *maskp = constrs_mask(constrs, var);
  1158. maskp->bits[0] &= (u_int32_t)mask;
  1159. maskp->bits[1] &= (u_int32_t)(mask >> 32);
  1160. memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
  1161. if (! maskp->bits[0] && ! maskp->bits[1])
  1162. return -EINVAL;
  1163. return 0;
  1164. }
  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. }
  1799. if (!tout) {
  1800. snd_printd("%s write error (DMA or IRQ trouble?)\n",
  1801. is_playback ? "playback" : "capture");
  1802. err = -EIO;
  1803. break;
  1804. }
  1805. }
  1806. _endloop:
  1807. set_current_state(TASK_RUNNING);
  1808. remove_wait_queue(&runtime->tsleep, &wait);
  1809. *availp = avail;
  1810. return err;
  1811. }
  1812. static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
  1813. unsigned int hwoff,
  1814. unsigned long data, unsigned int off,
  1815. snd_pcm_uframes_t frames)
  1816. {
  1817. struct snd_pcm_runtime *runtime = substream->runtime;
  1818. int err;
  1819. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  1820. if (substream->ops->copy) {
  1821. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  1822. return err;
  1823. } else {
  1824. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  1825. if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
  1826. return -EFAULT;
  1827. }
  1828. return 0;
  1829. }
  1830. typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
  1831. unsigned long data, unsigned int off,
  1832. snd_pcm_uframes_t size);
  1833. static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
  1834. unsigned long data,
  1835. snd_pcm_uframes_t size,
  1836. int nonblock,
  1837. transfer_f transfer)
  1838. {
  1839. struct snd_pcm_runtime *runtime = substream->runtime;
  1840. snd_pcm_uframes_t xfer = 0;
  1841. snd_pcm_uframes_t offset = 0;
  1842. snd_pcm_uframes_t avail;
  1843. int err = 0;
  1844. if (size == 0)
  1845. return 0;
  1846. snd_pcm_stream_lock_irq(substream);
  1847. switch (runtime->status->state) {
  1848. case SNDRV_PCM_STATE_PREPARED:
  1849. case SNDRV_PCM_STATE_RUNNING:
  1850. case SNDRV_PCM_STATE_PAUSED:
  1851. break;
  1852. case SNDRV_PCM_STATE_XRUN:
  1853. err = -EPIPE;
  1854. goto _end_unlock;
  1855. case SNDRV_PCM_STATE_SUSPENDED:
  1856. err = -ESTRPIPE;
  1857. goto _end_unlock;
  1858. default:
  1859. err = -EBADFD;
  1860. goto _end_unlock;
  1861. }
  1862. runtime->twake = runtime->control->avail_min ? : 1;
  1863. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  1864. snd_pcm_update_hw_ptr(substream);
  1865. avail = snd_pcm_playback_avail(runtime);
  1866. while (size > 0) {
  1867. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  1868. snd_pcm_uframes_t cont;
  1869. if (!avail) {
  1870. if (nonblock) {
  1871. err = -EAGAIN;
  1872. goto _end_unlock;
  1873. }
  1874. runtime->twake = min_t(snd_pcm_uframes_t, size,
  1875. runtime->control->avail_min ? : 1);
  1876. err = wait_for_avail(substream, &avail);
  1877. if (err < 0)
  1878. goto _end_unlock;
  1879. }
  1880. frames = size > avail ? avail : size;
  1881. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  1882. if (frames > cont)
  1883. frames = cont;
  1884. if (snd_BUG_ON(!frames)) {
  1885. runtime->twake = 0;
  1886. snd_pcm_stream_unlock_irq(substream);
  1887. return -EINVAL;
  1888. }
  1889. appl_ptr = runtime->control->appl_ptr;
  1890. appl_ofs = appl_ptr % runtime->buffer_size;
  1891. snd_pcm_stream_unlock_irq(substream);
  1892. err = transfer(substream, appl_ofs, data, offset, frames);
  1893. snd_pcm_stream_lock_irq(substream);
  1894. if (err < 0)
  1895. goto _end_unlock;
  1896. switch (runtime->status->state) {
  1897. case SNDRV_PCM_STATE_XRUN:
  1898. err = -EPIPE;
  1899. goto _end_unlock;
  1900. case SNDRV_PCM_STATE_SUSPENDED:
  1901. err = -ESTRPIPE;
  1902. goto _end_unlock;
  1903. default:
  1904. break;
  1905. }
  1906. appl_ptr += frames;
  1907. if (appl_ptr >= runtime->boundary)
  1908. appl_ptr -= runtime->boundary;
  1909. runtime->control->appl_ptr = appl_ptr;
  1910. if (substream->ops->ack)
  1911. substream->ops->ack(substream);
  1912. offset += frames;
  1913. size -= frames;
  1914. xfer += frames;
  1915. avail -= frames;
  1916. if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
  1917. snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
  1918. err = snd_pcm_start(substream);
  1919. if (err < 0)
  1920. goto _end_unlock;
  1921. }
  1922. }
  1923. _end_unlock:
  1924. runtime->twake = 0;
  1925. if (xfer > 0 && err >= 0)
  1926. snd_pcm_update_state(substream, runtime);
  1927. snd_pcm_stream_unlock_irq(substream);
  1928. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  1929. }
  1930. /* sanity-check for read/write methods */
  1931. static int pcm_sanity_check(struct snd_pcm_substream *substream)
  1932. {
  1933. struct snd_pcm_runtime *runtime;
  1934. if (PCM_RUNTIME_CHECK(substream))
  1935. return -ENXIO;
  1936. runtime = substream->runtime;
  1937. if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
  1938. return -EINVAL;
  1939. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1940. return -EBADFD;
  1941. return 0;
  1942. }
  1943. snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
  1944. {
  1945. struct snd_pcm_runtime *runtime;
  1946. int nonblock;
  1947. int err;
  1948. err = pcm_sanity_check(substream);
  1949. if (err < 0)
  1950. return err;
  1951. runtime = substream->runtime;
  1952. nonblock = !!(substream->f_flags & O_NONBLOCK);
  1953. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
  1954. runtime->channels > 1)
  1955. return -EINVAL;
  1956. return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
  1957. snd_pcm_lib_write_transfer);
  1958. }
  1959. EXPORT_SYMBOL(snd_pcm_lib_write);
  1960. static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
  1961. unsigned int hwoff,
  1962. unsigned long data, unsigned int off,
  1963. snd_pcm_uframes_t frames)
  1964. {
  1965. struct snd_pcm_runtime *runtime = substream->runtime;
  1966. int err;
  1967. void __user **bufs = (void __user **)data;
  1968. int channels = runtime->channels;
  1969. int c;
  1970. if (substream->ops->copy) {
  1971. if (snd_BUG_ON(!substream->ops->silence))
  1972. return -EINVAL;
  1973. for (c = 0; c < channels; ++c, ++bufs) {
  1974. if (*bufs == NULL) {
  1975. if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
  1976. return err;
  1977. } else {
  1978. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  1979. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  1980. return err;
  1981. }
  1982. }
  1983. } else {
  1984. /* default transfer behaviour */
  1985. size_t dma_csize = runtime->dma_bytes / channels;
  1986. for (c = 0; c < channels; ++c, ++bufs) {
  1987. char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  1988. if (*bufs == NULL) {
  1989. snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
  1990. } else {
  1991. char __user *buf = *bufs + samples_to_bytes(runtime, off);
  1992. if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
  1993. return -EFAULT;
  1994. }
  1995. }
  1996. }
  1997. return 0;
  1998. }
  1999. snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
  2000. void __user **bufs,
  2001. snd_pcm_uframes_t frames)
  2002. {
  2003. struct snd_pcm_runtime *runtime;
  2004. int nonblock;
  2005. int err;
  2006. err = pcm_sanity_check(substream);
  2007. if (err < 0)
  2008. return err;
  2009. runtime = substream->runtime;
  2010. nonblock = !!(substream->f_flags & O_NONBLOCK);
  2011. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  2012. return -EINVAL;
  2013. return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
  2014. nonblock, snd_pcm_lib_writev_transfer);
  2015. }
  2016. EXPORT_SYMBOL(snd_pcm_lib_writev);
  2017. static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
  2018. unsigned int hwoff,
  2019. unsigned long data, unsigned int off,
  2020. snd_pcm_uframes_t frames)
  2021. {
  2022. struct snd_pcm_runtime *runtime = substream->runtime;
  2023. int err;
  2024. char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
  2025. if (substream->ops->copy) {
  2026. if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
  2027. return err;
  2028. } else {
  2029. char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
  2030. if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
  2031. return -EFAULT;
  2032. }
  2033. return 0;
  2034. }
  2035. static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
  2036. unsigned long data,
  2037. snd_pcm_uframes_t size,
  2038. int nonblock,
  2039. transfer_f transfer)
  2040. {
  2041. struct snd_pcm_runtime *runtime = substream->runtime;
  2042. snd_pcm_uframes_t xfer = 0;
  2043. snd_pcm_uframes_t offset = 0;
  2044. snd_pcm_uframes_t avail;
  2045. int err = 0;
  2046. if (size == 0)
  2047. return 0;
  2048. snd_pcm_stream_lock_irq(substream);
  2049. switch (runtime->status->state) {
  2050. case SNDRV_PCM_STATE_PREPARED:
  2051. if (size >= runtime->start_threshold) {
  2052. err = snd_pcm_start(substream);
  2053. if (err < 0)
  2054. goto _end_unlock;
  2055. }
  2056. break;
  2057. case SNDRV_PCM_STATE_DRAINING:
  2058. case SNDRV_PCM_STATE_RUNNING:
  2059. case SNDRV_PCM_STATE_PAUSED:
  2060. break;
  2061. case SNDRV_PCM_STATE_XRUN:
  2062. err = -EPIPE;
  2063. goto _end_unlock;
  2064. case SNDRV_PCM_STATE_SUSPENDED:
  2065. err = -ESTRPIPE;
  2066. goto _end_unlock;
  2067. default:
  2068. err = -EBADFD;
  2069. goto _end_unlock;
  2070. }
  2071. runtime->twake = runtime->control->avail_min ? : 1;
  2072. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  2073. snd_pcm_update_hw_ptr(substream);
  2074. avail = snd_pcm_capture_avail(runtime);
  2075. while (size > 0) {
  2076. snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
  2077. snd_pcm_uframes_t cont;
  2078. if (!avail) {
  2079. if (runtime->status->state ==
  2080. SNDRV_PCM_STATE_DRAINING) {
  2081. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  2082. goto _end_unlock;
  2083. }
  2084. if (nonblock) {
  2085. err = -EAGAIN;
  2086. goto _end_unlock;
  2087. }
  2088. runtime->twake = min_t(snd_pcm_uframes_t, size,
  2089. runtime->control->avail_min ? : 1);
  2090. err = wait_for_avail(substream, &avail);
  2091. if (err < 0)
  2092. goto _end_unlock;
  2093. if (!avail)
  2094. continue; /* draining */
  2095. }
  2096. frames = size > avail ? avail : size;
  2097. cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
  2098. if (frames > cont)
  2099. frames = cont;
  2100. if (snd_BUG_ON(!frames)) {
  2101. runtime->twake = 0;
  2102. snd_pcm_stream_unlock_irq(substream);
  2103. return -EINVAL;
  2104. }
  2105. appl_ptr = runtime->control->appl_ptr;
  2106. appl_ofs = appl_ptr % runtime->buffer_size;
  2107. snd_pcm_stream_unlock_irq(substream);
  2108. err = transfer(substream, appl_ofs, data, offset, frames);
  2109. snd_pcm_stream_lock_irq(substream);
  2110. if (err < 0)
  2111. goto _end_unlock;
  2112. switch (runtime->status->state) {
  2113. case SNDRV_PCM_STATE_XRUN:
  2114. err = -EPIPE;
  2115. goto _end_unlock;
  2116. case SNDRV_PCM_STATE_SUSPENDED:
  2117. err = -ESTRPIPE;
  2118. goto _end_unlock;
  2119. default:
  2120. break;
  2121. }
  2122. appl_ptr += frames;
  2123. if (appl_ptr >= runtime->boundary)
  2124. appl_ptr -= runtime->boundary;
  2125. runtime->control->appl_ptr = appl_ptr;
  2126. if (substream->ops->ack)
  2127. substream->ops->ack(substream);
  2128. offset += frames;
  2129. size -= frames;
  2130. xfer += frames;
  2131. avail -= frames;
  2132. }
  2133. _end_unlock:
  2134. runtime->twake = 0;
  2135. if (xfer > 0 && err >= 0)
  2136. snd_pcm_update_state(substream, runtime);
  2137. snd_pcm_stream_unlock_irq(substream);
  2138. return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
  2139. }
  2140. snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
  2141. {
  2142. struct snd_pcm_runtime *runtime;
  2143. int nonblock;
  2144. int err;
  2145. err = pcm_sanity_check(substream);
  2146. if (err < 0)
  2147. return err;
  2148. runtime = substream->runtime;
  2149. nonblock = !!(substream->f_flags & O_NONBLOCK);
  2150. if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
  2151. return -EINVAL;
  2152. return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
  2153. }
  2154. EXPORT_SYMBOL(snd_pcm_lib_read);
  2155. static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
  2156. unsigned int hwoff,
  2157. unsigned long data, unsigned int off,
  2158. snd_pcm_uframes_t frames)
  2159. {
  2160. struct snd_pcm_runtime *runtime = substream->runtime;
  2161. int err;
  2162. void __user **bufs = (void __user **)data;
  2163. int channels = runtime->channels;
  2164. int c;
  2165. if (substream->ops->copy) {
  2166. for (c = 0; c < channels; ++c, ++bufs) {
  2167. char __user *buf;
  2168. if (*bufs == NULL)
  2169. continue;
  2170. buf = *bufs + samples_to_bytes(runtime, off);
  2171. if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
  2172. return err;
  2173. }
  2174. } else {
  2175. snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
  2176. for (c = 0; c < channels; ++c, ++bufs) {
  2177. char *hwbuf;
  2178. char __user *buf;
  2179. if (*bufs == NULL)
  2180. continue;
  2181. hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
  2182. buf = *bufs + samples_to_bytes(runtime, off);
  2183. if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
  2184. return -EFAULT;
  2185. }
  2186. }
  2187. return 0;
  2188. }
  2189. snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
  2190. void __user **bufs,
  2191. snd_pcm_uframes_t frames)
  2192. {
  2193. struct snd_pcm_runtime *runtime;
  2194. int nonblock;
  2195. int err;
  2196. err = pcm_sanity_check(substream);
  2197. if (err < 0)
  2198. return err;
  2199. runtime = substream->runtime;
  2200. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2201. return -EBADFD;
  2202. nonblock = !!(substream->f_flags & O_NONBLOCK);
  2203. if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  2204. return -EINVAL;
  2205. return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
  2206. }
  2207. EXPORT_SYMBOL(snd_pcm_lib_readv);
  2208. /*
  2209. * standard channel mapping helpers
  2210. */
  2211. /* default channel maps for multi-channel playbacks, up to 8 channels */
  2212. const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[] = {
  2213. { .channels = 1,
  2214. .map = { SNDRV_CHMAP_MONO } },
  2215. { .channels = 2,
  2216. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  2217. { .channels = 4,
  2218. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2219. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  2220. { .channels = 6,
  2221. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2222. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  2223. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
  2224. { .channels = 8,
  2225. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2226. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  2227. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  2228. SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
  2229. { }
  2230. };
  2231. EXPORT_SYMBOL_GPL(snd_pcm_std_chmaps);
  2232. /* alternative channel maps with CLFE <-> surround swapped for 6/8 channels */
  2233. const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[] = {
  2234. { .channels = 1,
  2235. .map = { SNDRV_CHMAP_MONO } },
  2236. { .channels = 2,
  2237. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  2238. { .channels = 4,
  2239. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2240. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  2241. { .channels = 6,
  2242. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2243. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  2244. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  2245. { .channels = 8,
  2246. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2247. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  2248. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
  2249. SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } },
  2250. { }
  2251. };
  2252. EXPORT_SYMBOL_GPL(snd_pcm_alt_chmaps);
  2253. static bool valid_chmap_channels(const struct snd_pcm_chmap *info, int ch)
  2254. {
  2255. if (ch > info->max_channels)
  2256. return false;
  2257. return !info->channel_mask || (info->channel_mask & (1U << ch));
  2258. }
  2259. static int pcm_chmap_ctl_info(struct snd_kcontrol *kcontrol,
  2260. struct snd_ctl_elem_info *uinfo)
  2261. {
  2262. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2263. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2264. uinfo->count = 0;
  2265. uinfo->count = info->max_channels;
  2266. uinfo->value.integer.min = 0;
  2267. uinfo->value.integer.max = SNDRV_CHMAP_LAST;
  2268. return 0;
  2269. }
  2270. /* get callback for channel map ctl element
  2271. * stores the channel position firstly matching with the current channels
  2272. */
  2273. static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  2274. struct snd_ctl_elem_value *ucontrol)
  2275. {
  2276. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2277. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  2278. struct snd_pcm_substream *substream;
  2279. const struct snd_pcm_chmap_elem *map;
  2280. if (snd_BUG_ON(!info->chmap))
  2281. return -EINVAL;
  2282. substream = snd_pcm_chmap_substream(info, idx);
  2283. if (!substream)
  2284. return -ENODEV;
  2285. memset(ucontrol->value.integer.value, 0,
  2286. sizeof(ucontrol->value.integer.value));
  2287. if (!substream->runtime)
  2288. return 0; /* no channels set */
  2289. for (map = info->chmap; map->channels; map++) {
  2290. int i;
  2291. if (map->channels == substream->runtime->channels &&
  2292. valid_chmap_channels(info, map->channels)) {
  2293. for (i = 0; i < map->channels; i++)
  2294. ucontrol->value.integer.value[i] = map->map[i];
  2295. return 0;
  2296. }
  2297. }
  2298. return -EINVAL;
  2299. }
  2300. /* tlv callback for channel map ctl element
  2301. * expands the pre-defined channel maps in a form of TLV
  2302. */
  2303. static int pcm_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  2304. unsigned int size, unsigned int __user *tlv)
  2305. {
  2306. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2307. const struct snd_pcm_chmap_elem *map;
  2308. unsigned int __user *dst;
  2309. int c, count = 0;
  2310. if (snd_BUG_ON(!info->chmap))
  2311. return -EINVAL;
  2312. if (size < 8)
  2313. return -ENOMEM;
  2314. if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
  2315. return -EFAULT;
  2316. size -= 8;
  2317. dst = tlv + 2;
  2318. for (map = info->chmap; map->channels; map++) {
  2319. int chs_bytes = map->channels * 4;
  2320. if (!valid_chmap_channels(info, map->channels))
  2321. continue;
  2322. if (size < 8)
  2323. return -ENOMEM;
  2324. if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
  2325. put_user(chs_bytes, dst + 1))
  2326. return -EFAULT;
  2327. dst += 2;
  2328. size -= 8;
  2329. count += 8;
  2330. if (size < chs_bytes)
  2331. return -ENOMEM;
  2332. size -= chs_bytes;
  2333. count += chs_bytes;
  2334. for (c = 0; c < map->channels; c++) {
  2335. if (put_user(map->map[c], dst))
  2336. return -EFAULT;
  2337. dst++;
  2338. }
  2339. }
  2340. if (put_user(count, tlv + 1))
  2341. return -EFAULT;
  2342. return 0;
  2343. }
  2344. static void pcm_chmap_ctl_private_free(struct snd_kcontrol *kcontrol)
  2345. {
  2346. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  2347. info->pcm->streams[info->stream].chmap_kctl = NULL;
  2348. kfree(info);
  2349. }
  2350. /**
  2351. * snd_pcm_add_chmap_ctls - create channel-mapping control elements
  2352. * @pcm: the assigned PCM instance
  2353. * @stream: stream direction
  2354. * @chmap: channel map elements (for query)
  2355. * @max_channels: the max number of channels for the stream
  2356. * @private_value: the value passed to each kcontrol's private_value field
  2357. * @info_ret: store struct snd_pcm_chmap instance if non-NULL
  2358. *
  2359. * Create channel-mapping control elements assigned to the given PCM stream(s).
  2360. * Return: Zero if successful, or a negative error value.
  2361. */
  2362. int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
  2363. const struct snd_pcm_chmap_elem *chmap,
  2364. int max_channels,
  2365. unsigned long private_value,
  2366. struct snd_pcm_chmap **info_ret)
  2367. {
  2368. struct snd_pcm_chmap *info;
  2369. struct snd_kcontrol_new knew = {
  2370. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2371. .access = SNDRV_CTL_ELEM_ACCESS_READ |
  2372. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  2373. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
  2374. .info = pcm_chmap_ctl_info,
  2375. .get = pcm_chmap_ctl_get,
  2376. .tlv.c = pcm_chmap_ctl_tlv,
  2377. };
  2378. int err;
  2379. info = kzalloc(sizeof(*info), GFP_KERNEL);
  2380. if (!info)
  2381. return -ENOMEM;
  2382. info->pcm = pcm;
  2383. info->stream = stream;
  2384. info->chmap = chmap;
  2385. info->max_channels = max_channels;
  2386. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  2387. knew.name = "Playback Channel Map";
  2388. else
  2389. knew.name = "Capture Channel Map";
  2390. knew.device = pcm->device;
  2391. knew.count = pcm->streams[stream].substream_count;
  2392. knew.private_value = private_value;
  2393. info->kctl = snd_ctl_new1(&knew, info);
  2394. if (!info->kctl) {
  2395. kfree(info);
  2396. return -ENOMEM;
  2397. }
  2398. info->kctl->private_free = pcm_chmap_ctl_private_free;
  2399. err = snd_ctl_add(pcm->card, info->kctl);
  2400. if (err < 0)
  2401. return err;
  2402. pcm->streams[stream].chmap_kctl = info->kctl;
  2403. if (info_ret)
  2404. *info_ret = info;
  2405. return 0;
  2406. }
  2407. EXPORT_SYMBOL_GPL(snd_pcm_add_chmap_ctls);