pcm_lib.c 71 KB

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