pcm_lib.c 59 KB

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