pcm_lib.c 70 KB

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