pcm_lib.c 70 KB

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