pcm_lib.c 60 KB

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