pcm_lib.c 68 KB

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