pcm_lib.c 68 KB

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