pcm_lib.c 68 KB

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