pcm_lib.c 68 KB

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