pcm_lib.c 61 KB

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