pcm_native.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731
  1. /*
  2. * Digital Audio (PCM) abstract layer
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/mm.h>
  22. #include <linux/module.h>
  23. #include <linux/file.h>
  24. #include <linux/slab.h>
  25. #include <linux/sched/signal.h>
  26. #include <linux/time.h>
  27. #include <linux/pm_qos.h>
  28. #include <linux/io.h>
  29. #include <linux/dma-mapping.h>
  30. #include <sound/core.h>
  31. #include <sound/control.h>
  32. #include <sound/info.h>
  33. #include <sound/pcm.h>
  34. #include <sound/pcm_params.h>
  35. #include <sound/timer.h>
  36. #include <sound/minors.h>
  37. #include <linux/uio.h>
  38. #include "pcm_local.h"
  39. #ifdef CONFIG_SND_DEBUG
  40. #define CREATE_TRACE_POINTS
  41. #include "pcm_param_trace.h"
  42. #else
  43. #define trace_hw_mask_param_enabled() 0
  44. #define trace_hw_interval_param_enabled() 0
  45. #define trace_hw_mask_param(substream, type, index, prev, curr)
  46. #define trace_hw_interval_param(substream, type, index, prev, curr)
  47. #endif
  48. /*
  49. * Compatibility
  50. */
  51. struct snd_pcm_hw_params_old {
  52. unsigned int flags;
  53. unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
  54. SNDRV_PCM_HW_PARAM_ACCESS + 1];
  55. struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
  56. SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
  57. unsigned int rmask;
  58. unsigned int cmask;
  59. unsigned int info;
  60. unsigned int msbits;
  61. unsigned int rate_num;
  62. unsigned int rate_den;
  63. snd_pcm_uframes_t fifo_size;
  64. unsigned char reserved[64];
  65. };
  66. #ifdef CONFIG_SND_SUPPORT_OLD_API
  67. #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
  68. #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
  69. static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
  70. struct snd_pcm_hw_params_old __user * _oparams);
  71. static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
  72. struct snd_pcm_hw_params_old __user * _oparams);
  73. #endif
  74. static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
  75. /*
  76. *
  77. */
  78. static DEFINE_RWLOCK(snd_pcm_link_rwlock);
  79. static DECLARE_RWSEM(snd_pcm_link_rwsem);
  80. /* Writer in rwsem may block readers even during its waiting in queue,
  81. * and this may lead to a deadlock when the code path takes read sem
  82. * twice (e.g. one in snd_pcm_action_nonatomic() and another in
  83. * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
  84. * spin until it gets the lock.
  85. */
  86. static inline void down_write_nonblock(struct rw_semaphore *lock)
  87. {
  88. while (!down_write_trylock(lock))
  89. cond_resched();
  90. }
  91. /**
  92. * snd_pcm_stream_lock - Lock the PCM stream
  93. * @substream: PCM substream
  94. *
  95. * This locks the PCM stream's spinlock or mutex depending on the nonatomic
  96. * flag of the given substream. This also takes the global link rw lock
  97. * (or rw sem), too, for avoiding the race with linked streams.
  98. */
  99. void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
  100. {
  101. if (substream->pcm->nonatomic) {
  102. down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
  103. mutex_lock(&substream->self_group.mutex);
  104. } else {
  105. read_lock(&snd_pcm_link_rwlock);
  106. spin_lock(&substream->self_group.lock);
  107. }
  108. }
  109. EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
  110. /**
  111. * snd_pcm_stream_lock - Unlock the PCM stream
  112. * @substream: PCM substream
  113. *
  114. * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
  115. */
  116. void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
  117. {
  118. if (substream->pcm->nonatomic) {
  119. mutex_unlock(&substream->self_group.mutex);
  120. up_read(&snd_pcm_link_rwsem);
  121. } else {
  122. spin_unlock(&substream->self_group.lock);
  123. read_unlock(&snd_pcm_link_rwlock);
  124. }
  125. }
  126. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
  127. /**
  128. * snd_pcm_stream_lock_irq - Lock the PCM stream
  129. * @substream: PCM substream
  130. *
  131. * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
  132. * IRQ (only when nonatomic is false). In nonatomic case, this is identical
  133. * as snd_pcm_stream_lock().
  134. */
  135. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
  136. {
  137. if (!substream->pcm->nonatomic)
  138. local_irq_disable();
  139. snd_pcm_stream_lock(substream);
  140. }
  141. EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
  142. /**
  143. * snd_pcm_stream_unlock_irq - Unlock the PCM stream
  144. * @substream: PCM substream
  145. *
  146. * This is a counter-part of snd_pcm_stream_lock_irq().
  147. */
  148. void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
  149. {
  150. snd_pcm_stream_unlock(substream);
  151. if (!substream->pcm->nonatomic)
  152. local_irq_enable();
  153. }
  154. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
  155. unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
  156. {
  157. unsigned long flags = 0;
  158. if (!substream->pcm->nonatomic)
  159. local_irq_save(flags);
  160. snd_pcm_stream_lock(substream);
  161. return flags;
  162. }
  163. EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
  164. /**
  165. * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
  166. * @substream: PCM substream
  167. * @flags: irq flags
  168. *
  169. * This is a counter-part of snd_pcm_stream_lock_irqsave().
  170. */
  171. void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
  172. unsigned long flags)
  173. {
  174. snd_pcm_stream_unlock(substream);
  175. if (!substream->pcm->nonatomic)
  176. local_irq_restore(flags);
  177. }
  178. EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
  179. int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
  180. {
  181. struct snd_pcm *pcm = substream->pcm;
  182. struct snd_pcm_str *pstr = substream->pstr;
  183. memset(info, 0, sizeof(*info));
  184. info->card = pcm->card->number;
  185. info->device = pcm->device;
  186. info->stream = substream->stream;
  187. info->subdevice = substream->number;
  188. strlcpy(info->id, pcm->id, sizeof(info->id));
  189. strlcpy(info->name, pcm->name, sizeof(info->name));
  190. info->dev_class = pcm->dev_class;
  191. info->dev_subclass = pcm->dev_subclass;
  192. info->subdevices_count = pstr->substream_count;
  193. info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
  194. strlcpy(info->subname, substream->name, sizeof(info->subname));
  195. return 0;
  196. }
  197. int snd_pcm_info_user(struct snd_pcm_substream *substream,
  198. struct snd_pcm_info __user * _info)
  199. {
  200. struct snd_pcm_info *info;
  201. int err;
  202. info = kmalloc(sizeof(*info), GFP_KERNEL);
  203. if (! info)
  204. return -ENOMEM;
  205. err = snd_pcm_info(substream, info);
  206. if (err >= 0) {
  207. if (copy_to_user(_info, info, sizeof(*info)))
  208. err = -EFAULT;
  209. }
  210. kfree(info);
  211. return err;
  212. }
  213. static bool hw_support_mmap(struct snd_pcm_substream *substream)
  214. {
  215. if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
  216. return false;
  217. /* architecture supports dma_mmap_coherent()? */
  218. #if defined(CONFIG_ARCH_NO_COHERENT_DMA_MMAP) || !defined(CONFIG_HAS_DMA)
  219. if (!substream->ops->mmap &&
  220. substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
  221. return false;
  222. #endif
  223. return true;
  224. }
  225. static int constrain_mask_params(struct snd_pcm_substream *substream,
  226. struct snd_pcm_hw_params *params)
  227. {
  228. struct snd_pcm_hw_constraints *constrs =
  229. &substream->runtime->hw_constraints;
  230. struct snd_mask *m;
  231. unsigned int k;
  232. struct snd_mask old_mask;
  233. int changed;
  234. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
  235. m = hw_param_mask(params, k);
  236. if (snd_mask_empty(m))
  237. return -EINVAL;
  238. /* This parameter is not requested to change by a caller. */
  239. if (!(params->rmask & (1 << k)))
  240. continue;
  241. if (trace_hw_mask_param_enabled())
  242. old_mask = *m;
  243. changed = snd_mask_refine(m, constrs_mask(constrs, k));
  244. if (changed < 0)
  245. return changed;
  246. if (changed == 0)
  247. continue;
  248. /* Set corresponding flag so that the caller gets it. */
  249. trace_hw_mask_param(substream, k, 0, &old_mask, m);
  250. params->cmask |= 1 << k;
  251. }
  252. return 0;
  253. }
  254. static int constrain_interval_params(struct snd_pcm_substream *substream,
  255. struct snd_pcm_hw_params *params)
  256. {
  257. struct snd_pcm_hw_constraints *constrs =
  258. &substream->runtime->hw_constraints;
  259. struct snd_interval *i;
  260. unsigned int k;
  261. struct snd_interval old_interval;
  262. int changed;
  263. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
  264. i = hw_param_interval(params, k);
  265. if (snd_interval_empty(i))
  266. return -EINVAL;
  267. /* This parameter is not requested to change by a caller. */
  268. if (!(params->rmask & (1 << k)))
  269. continue;
  270. if (trace_hw_interval_param_enabled())
  271. old_interval = *i;
  272. changed = snd_interval_refine(i, constrs_interval(constrs, k));
  273. if (changed < 0)
  274. return changed;
  275. if (changed == 0)
  276. continue;
  277. /* Set corresponding flag so that the caller gets it. */
  278. trace_hw_interval_param(substream, k, 0, &old_interval, i);
  279. params->cmask |= 1 << k;
  280. }
  281. return 0;
  282. }
  283. static int constrain_params_by_rules(struct snd_pcm_substream *substream,
  284. struct snd_pcm_hw_params *params)
  285. {
  286. struct snd_pcm_hw_constraints *constrs =
  287. &substream->runtime->hw_constraints;
  288. unsigned int k;
  289. unsigned int *rstamps;
  290. unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
  291. unsigned int stamp;
  292. struct snd_pcm_hw_rule *r;
  293. unsigned int d;
  294. struct snd_mask old_mask;
  295. struct snd_interval old_interval;
  296. bool again;
  297. int changed, err = 0;
  298. /*
  299. * Each application of rule has own sequence number.
  300. *
  301. * Each member of 'rstamps' array represents the sequence number of
  302. * recent application of corresponding rule.
  303. */
  304. rstamps = kcalloc(constrs->rules_num, sizeof(unsigned int), GFP_KERNEL);
  305. if (!rstamps)
  306. return -ENOMEM;
  307. /*
  308. * Each member of 'vstamps' array represents the sequence number of
  309. * recent application of rule in which corresponding parameters were
  310. * changed.
  311. *
  312. * In initial state, elements corresponding to parameters requested by
  313. * a caller is 1. For unrequested parameters, corresponding members
  314. * have 0 so that the parameters are never changed anymore.
  315. */
  316. for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
  317. vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
  318. /* Due to the above design, actual sequence number starts at 2. */
  319. stamp = 2;
  320. retry:
  321. /* Apply all rules in order. */
  322. again = false;
  323. for (k = 0; k < constrs->rules_num; k++) {
  324. r = &constrs->rules[k];
  325. /*
  326. * Check condition bits of this rule. When the rule has
  327. * some condition bits, parameter without the bits is
  328. * never processed. SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP
  329. * is an example of the condition bits.
  330. */
  331. if (r->cond && !(r->cond & params->flags))
  332. continue;
  333. /*
  334. * The 'deps' array includes maximum three dependencies
  335. * to SNDRV_PCM_HW_PARAM_XXXs for this rule. The fourth
  336. * member of this array is a sentinel and should be
  337. * negative value.
  338. *
  339. * This rule should be processed in this time when dependent
  340. * parameters were changed at former applications of the other
  341. * rules.
  342. */
  343. for (d = 0; r->deps[d] >= 0; d++) {
  344. if (vstamps[r->deps[d]] > rstamps[k])
  345. break;
  346. }
  347. if (r->deps[d] < 0)
  348. continue;
  349. if (trace_hw_mask_param_enabled()) {
  350. if (hw_is_mask(r->var))
  351. old_mask = *hw_param_mask(params, r->var);
  352. }
  353. if (trace_hw_interval_param_enabled()) {
  354. if (hw_is_interval(r->var))
  355. old_interval = *hw_param_interval(params, r->var);
  356. }
  357. changed = r->func(params, r);
  358. if (changed < 0) {
  359. err = changed;
  360. goto out;
  361. }
  362. /*
  363. * When the parameter is changed, notify it to the caller
  364. * by corresponding returned bit, then preparing for next
  365. * iteration.
  366. */
  367. if (changed && r->var >= 0) {
  368. if (hw_is_mask(r->var)) {
  369. trace_hw_mask_param(substream, r->var,
  370. k + 1, &old_mask,
  371. hw_param_mask(params, r->var));
  372. }
  373. if (hw_is_interval(r->var)) {
  374. trace_hw_interval_param(substream, r->var,
  375. k + 1, &old_interval,
  376. hw_param_interval(params, r->var));
  377. }
  378. params->cmask |= (1 << r->var);
  379. vstamps[r->var] = stamp;
  380. again = true;
  381. }
  382. rstamps[k] = stamp++;
  383. }
  384. /* Iterate to evaluate all rules till no parameters are changed. */
  385. if (again)
  386. goto retry;
  387. out:
  388. kfree(rstamps);
  389. return err;
  390. }
  391. static int fixup_unreferenced_params(struct snd_pcm_substream *substream,
  392. struct snd_pcm_hw_params *params)
  393. {
  394. const struct snd_interval *i;
  395. const struct snd_mask *m;
  396. int err;
  397. if (!params->msbits) {
  398. i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
  399. if (snd_interval_single(i))
  400. params->msbits = snd_interval_value(i);
  401. }
  402. if (!params->rate_den) {
  403. i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
  404. if (snd_interval_single(i)) {
  405. params->rate_num = snd_interval_value(i);
  406. params->rate_den = 1;
  407. }
  408. }
  409. if (!params->fifo_size) {
  410. m = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
  411. i = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  412. if (snd_mask_single(m) && snd_interval_single(i)) {
  413. err = substream->ops->ioctl(substream,
  414. SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
  415. if (err < 0)
  416. return err;
  417. }
  418. }
  419. if (!params->info) {
  420. params->info = substream->runtime->hw.info;
  421. params->info &= ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
  422. SNDRV_PCM_INFO_DRAIN_TRIGGER);
  423. if (!hw_support_mmap(substream))
  424. params->info &= ~(SNDRV_PCM_INFO_MMAP |
  425. SNDRV_PCM_INFO_MMAP_VALID);
  426. }
  427. return 0;
  428. }
  429. int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
  430. struct snd_pcm_hw_params *params)
  431. {
  432. int err;
  433. params->info = 0;
  434. params->fifo_size = 0;
  435. if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
  436. params->msbits = 0;
  437. if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
  438. params->rate_num = 0;
  439. params->rate_den = 0;
  440. }
  441. err = constrain_mask_params(substream, params);
  442. if (err < 0)
  443. return err;
  444. err = constrain_interval_params(substream, params);
  445. if (err < 0)
  446. return err;
  447. err = constrain_params_by_rules(substream, params);
  448. if (err < 0)
  449. return err;
  450. params->rmask = 0;
  451. return 0;
  452. }
  453. EXPORT_SYMBOL(snd_pcm_hw_refine);
  454. static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
  455. struct snd_pcm_hw_params __user * _params)
  456. {
  457. struct snd_pcm_hw_params *params;
  458. int err;
  459. params = memdup_user(_params, sizeof(*params));
  460. if (IS_ERR(params))
  461. return PTR_ERR(params);
  462. err = snd_pcm_hw_refine(substream, params);
  463. if (err < 0)
  464. goto end;
  465. err = fixup_unreferenced_params(substream, params);
  466. if (err < 0)
  467. goto end;
  468. if (copy_to_user(_params, params, sizeof(*params)))
  469. err = -EFAULT;
  470. end:
  471. kfree(params);
  472. return err;
  473. }
  474. static int period_to_usecs(struct snd_pcm_runtime *runtime)
  475. {
  476. int usecs;
  477. if (! runtime->rate)
  478. return -1; /* invalid */
  479. /* take 75% of period time as the deadline */
  480. usecs = (750000 / runtime->rate) * runtime->period_size;
  481. usecs += ((750000 % runtime->rate) * runtime->period_size) /
  482. runtime->rate;
  483. return usecs;
  484. }
  485. static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
  486. {
  487. snd_pcm_stream_lock_irq(substream);
  488. if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
  489. substream->runtime->status->state = state;
  490. snd_pcm_stream_unlock_irq(substream);
  491. }
  492. static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
  493. int event)
  494. {
  495. #ifdef CONFIG_SND_PCM_TIMER
  496. if (substream->timer)
  497. snd_timer_notify(substream->timer, event,
  498. &substream->runtime->trigger_tstamp);
  499. #endif
  500. }
  501. /**
  502. * snd_pcm_hw_param_choose - choose a configuration defined by @params
  503. * @pcm: PCM instance
  504. * @params: the hw_params instance
  505. *
  506. * Choose one configuration from configuration space defined by @params.
  507. * The configuration chosen is that obtained fixing in this order:
  508. * first access, first format, first subformat, min channels,
  509. * min rate, min period time, max buffer size, min tick time
  510. *
  511. * Return: Zero if successful, or a negative error code on failure.
  512. */
  513. static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
  514. struct snd_pcm_hw_params *params)
  515. {
  516. static const int vars[] = {
  517. SNDRV_PCM_HW_PARAM_ACCESS,
  518. SNDRV_PCM_HW_PARAM_FORMAT,
  519. SNDRV_PCM_HW_PARAM_SUBFORMAT,
  520. SNDRV_PCM_HW_PARAM_CHANNELS,
  521. SNDRV_PCM_HW_PARAM_RATE,
  522. SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  523. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  524. SNDRV_PCM_HW_PARAM_TICK_TIME,
  525. -1
  526. };
  527. const int *v;
  528. struct snd_mask old_mask;
  529. struct snd_interval old_interval;
  530. int changed;
  531. for (v = vars; *v != -1; v++) {
  532. /* Keep old parameter to trace. */
  533. if (trace_hw_mask_param_enabled()) {
  534. if (hw_is_mask(*v))
  535. old_mask = *hw_param_mask(params, *v);
  536. }
  537. if (trace_hw_interval_param_enabled()) {
  538. if (hw_is_interval(*v))
  539. old_interval = *hw_param_interval(params, *v);
  540. }
  541. if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
  542. changed = snd_pcm_hw_param_first(pcm, params, *v, NULL);
  543. else
  544. changed = snd_pcm_hw_param_last(pcm, params, *v, NULL);
  545. if (changed < 0)
  546. return changed;
  547. if (changed == 0)
  548. continue;
  549. /* Trace the changed parameter. */
  550. if (hw_is_mask(*v)) {
  551. trace_hw_mask_param(pcm, *v, 0, &old_mask,
  552. hw_param_mask(params, *v));
  553. }
  554. if (hw_is_interval(*v)) {
  555. trace_hw_interval_param(pcm, *v, 0, &old_interval,
  556. hw_param_interval(params, *v));
  557. }
  558. }
  559. return 0;
  560. }
  561. static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
  562. struct snd_pcm_hw_params *params)
  563. {
  564. struct snd_pcm_runtime *runtime;
  565. int err, usecs;
  566. unsigned int bits;
  567. snd_pcm_uframes_t frames;
  568. if (PCM_RUNTIME_CHECK(substream))
  569. return -ENXIO;
  570. runtime = substream->runtime;
  571. snd_pcm_stream_lock_irq(substream);
  572. switch (runtime->status->state) {
  573. case SNDRV_PCM_STATE_OPEN:
  574. case SNDRV_PCM_STATE_SETUP:
  575. case SNDRV_PCM_STATE_PREPARED:
  576. break;
  577. default:
  578. snd_pcm_stream_unlock_irq(substream);
  579. return -EBADFD;
  580. }
  581. snd_pcm_stream_unlock_irq(substream);
  582. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  583. if (!substream->oss.oss)
  584. #endif
  585. if (atomic_read(&substream->mmap_count))
  586. return -EBADFD;
  587. params->rmask = ~0U;
  588. err = snd_pcm_hw_refine(substream, params);
  589. if (err < 0)
  590. goto _error;
  591. err = snd_pcm_hw_params_choose(substream, params);
  592. if (err < 0)
  593. goto _error;
  594. err = fixup_unreferenced_params(substream, params);
  595. if (err < 0)
  596. goto _error;
  597. if (substream->ops->hw_params != NULL) {
  598. err = substream->ops->hw_params(substream, params);
  599. if (err < 0)
  600. goto _error;
  601. }
  602. runtime->access = params_access(params);
  603. runtime->format = params_format(params);
  604. runtime->subformat = params_subformat(params);
  605. runtime->channels = params_channels(params);
  606. runtime->rate = params_rate(params);
  607. runtime->period_size = params_period_size(params);
  608. runtime->periods = params_periods(params);
  609. runtime->buffer_size = params_buffer_size(params);
  610. runtime->info = params->info;
  611. runtime->rate_num = params->rate_num;
  612. runtime->rate_den = params->rate_den;
  613. runtime->no_period_wakeup =
  614. (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
  615. (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
  616. bits = snd_pcm_format_physical_width(runtime->format);
  617. runtime->sample_bits = bits;
  618. bits *= runtime->channels;
  619. runtime->frame_bits = bits;
  620. frames = 1;
  621. while (bits % 8 != 0) {
  622. bits *= 2;
  623. frames *= 2;
  624. }
  625. runtime->byte_align = bits / 8;
  626. runtime->min_align = frames;
  627. /* Default sw params */
  628. runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
  629. runtime->period_step = 1;
  630. runtime->control->avail_min = runtime->period_size;
  631. runtime->start_threshold = 1;
  632. runtime->stop_threshold = runtime->buffer_size;
  633. runtime->silence_threshold = 0;
  634. runtime->silence_size = 0;
  635. runtime->boundary = runtime->buffer_size;
  636. while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
  637. runtime->boundary *= 2;
  638. snd_pcm_timer_resolution_change(substream);
  639. snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
  640. if (pm_qos_request_active(&substream->latency_pm_qos_req))
  641. pm_qos_remove_request(&substream->latency_pm_qos_req);
  642. if ((usecs = period_to_usecs(runtime)) >= 0)
  643. pm_qos_add_request(&substream->latency_pm_qos_req,
  644. PM_QOS_CPU_DMA_LATENCY, usecs);
  645. return 0;
  646. _error:
  647. /* hardware might be unusable from this time,
  648. so we force application to retry to set
  649. the correct hardware parameter settings */
  650. snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
  651. if (substream->ops->hw_free != NULL)
  652. substream->ops->hw_free(substream);
  653. return err;
  654. }
  655. static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
  656. struct snd_pcm_hw_params __user * _params)
  657. {
  658. struct snd_pcm_hw_params *params;
  659. int err;
  660. params = memdup_user(_params, sizeof(*params));
  661. if (IS_ERR(params))
  662. return PTR_ERR(params);
  663. err = snd_pcm_hw_params(substream, params);
  664. if (err < 0)
  665. goto end;
  666. if (copy_to_user(_params, params, sizeof(*params)))
  667. err = -EFAULT;
  668. end:
  669. kfree(params);
  670. return err;
  671. }
  672. static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
  673. {
  674. struct snd_pcm_runtime *runtime;
  675. int result = 0;
  676. if (PCM_RUNTIME_CHECK(substream))
  677. return -ENXIO;
  678. runtime = substream->runtime;
  679. snd_pcm_stream_lock_irq(substream);
  680. switch (runtime->status->state) {
  681. case SNDRV_PCM_STATE_SETUP:
  682. case SNDRV_PCM_STATE_PREPARED:
  683. break;
  684. default:
  685. snd_pcm_stream_unlock_irq(substream);
  686. return -EBADFD;
  687. }
  688. snd_pcm_stream_unlock_irq(substream);
  689. if (atomic_read(&substream->mmap_count))
  690. return -EBADFD;
  691. if (substream->ops->hw_free)
  692. result = substream->ops->hw_free(substream);
  693. snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
  694. pm_qos_remove_request(&substream->latency_pm_qos_req);
  695. return result;
  696. }
  697. static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
  698. struct snd_pcm_sw_params *params)
  699. {
  700. struct snd_pcm_runtime *runtime;
  701. int err;
  702. if (PCM_RUNTIME_CHECK(substream))
  703. return -ENXIO;
  704. runtime = substream->runtime;
  705. snd_pcm_stream_lock_irq(substream);
  706. if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  707. snd_pcm_stream_unlock_irq(substream);
  708. return -EBADFD;
  709. }
  710. snd_pcm_stream_unlock_irq(substream);
  711. if (params->tstamp_mode < 0 ||
  712. params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
  713. return -EINVAL;
  714. if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
  715. params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
  716. return -EINVAL;
  717. if (params->avail_min == 0)
  718. return -EINVAL;
  719. if (params->silence_size >= runtime->boundary) {
  720. if (params->silence_threshold != 0)
  721. return -EINVAL;
  722. } else {
  723. if (params->silence_size > params->silence_threshold)
  724. return -EINVAL;
  725. if (params->silence_threshold > runtime->buffer_size)
  726. return -EINVAL;
  727. }
  728. err = 0;
  729. snd_pcm_stream_lock_irq(substream);
  730. runtime->tstamp_mode = params->tstamp_mode;
  731. if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
  732. runtime->tstamp_type = params->tstamp_type;
  733. runtime->period_step = params->period_step;
  734. runtime->control->avail_min = params->avail_min;
  735. runtime->start_threshold = params->start_threshold;
  736. runtime->stop_threshold = params->stop_threshold;
  737. runtime->silence_threshold = params->silence_threshold;
  738. runtime->silence_size = params->silence_size;
  739. params->boundary = runtime->boundary;
  740. if (snd_pcm_running(substream)) {
  741. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  742. runtime->silence_size > 0)
  743. snd_pcm_playback_silence(substream, ULONG_MAX);
  744. err = snd_pcm_update_state(substream, runtime);
  745. }
  746. snd_pcm_stream_unlock_irq(substream);
  747. return err;
  748. }
  749. static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
  750. struct snd_pcm_sw_params __user * _params)
  751. {
  752. struct snd_pcm_sw_params params;
  753. int err;
  754. if (copy_from_user(&params, _params, sizeof(params)))
  755. return -EFAULT;
  756. err = snd_pcm_sw_params(substream, &params);
  757. if (copy_to_user(_params, &params, sizeof(params)))
  758. return -EFAULT;
  759. return err;
  760. }
  761. int snd_pcm_status(struct snd_pcm_substream *substream,
  762. struct snd_pcm_status *status)
  763. {
  764. struct snd_pcm_runtime *runtime = substream->runtime;
  765. snd_pcm_stream_lock_irq(substream);
  766. snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
  767. &runtime->audio_tstamp_config);
  768. /* backwards compatible behavior */
  769. if (runtime->audio_tstamp_config.type_requested ==
  770. SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
  771. if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
  772. runtime->audio_tstamp_config.type_requested =
  773. SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
  774. else
  775. runtime->audio_tstamp_config.type_requested =
  776. SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
  777. runtime->audio_tstamp_report.valid = 0;
  778. } else
  779. runtime->audio_tstamp_report.valid = 1;
  780. status->state = runtime->status->state;
  781. status->suspended_state = runtime->status->suspended_state;
  782. if (status->state == SNDRV_PCM_STATE_OPEN)
  783. goto _end;
  784. status->trigger_tstamp = runtime->trigger_tstamp;
  785. if (snd_pcm_running(substream)) {
  786. snd_pcm_update_hw_ptr(substream);
  787. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
  788. status->tstamp = runtime->status->tstamp;
  789. status->driver_tstamp = runtime->driver_tstamp;
  790. status->audio_tstamp =
  791. runtime->status->audio_tstamp;
  792. if (runtime->audio_tstamp_report.valid == 1)
  793. /* backwards compatibility, no report provided in COMPAT mode */
  794. snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
  795. &status->audio_tstamp_accuracy,
  796. &runtime->audio_tstamp_report);
  797. goto _tstamp_end;
  798. }
  799. } else {
  800. /* get tstamp only in fallback mode and only if enabled */
  801. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
  802. snd_pcm_gettime(runtime, &status->tstamp);
  803. }
  804. _tstamp_end:
  805. status->appl_ptr = runtime->control->appl_ptr;
  806. status->hw_ptr = runtime->status->hw_ptr;
  807. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  808. status->avail = snd_pcm_playback_avail(runtime);
  809. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
  810. runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  811. status->delay = runtime->buffer_size - status->avail;
  812. status->delay += runtime->delay;
  813. } else
  814. status->delay = 0;
  815. } else {
  816. status->avail = snd_pcm_capture_avail(runtime);
  817. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  818. status->delay = status->avail + runtime->delay;
  819. else
  820. status->delay = 0;
  821. }
  822. status->avail_max = runtime->avail_max;
  823. status->overrange = runtime->overrange;
  824. runtime->avail_max = 0;
  825. runtime->overrange = 0;
  826. _end:
  827. snd_pcm_stream_unlock_irq(substream);
  828. return 0;
  829. }
  830. static int snd_pcm_status_user(struct snd_pcm_substream *substream,
  831. struct snd_pcm_status __user * _status,
  832. bool ext)
  833. {
  834. struct snd_pcm_status status;
  835. int res;
  836. memset(&status, 0, sizeof(status));
  837. /*
  838. * with extension, parameters are read/write,
  839. * get audio_tstamp_data from user,
  840. * ignore rest of status structure
  841. */
  842. if (ext && get_user(status.audio_tstamp_data,
  843. (u32 __user *)(&_status->audio_tstamp_data)))
  844. return -EFAULT;
  845. res = snd_pcm_status(substream, &status);
  846. if (res < 0)
  847. return res;
  848. if (copy_to_user(_status, &status, sizeof(status)))
  849. return -EFAULT;
  850. return 0;
  851. }
  852. static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
  853. struct snd_pcm_channel_info * info)
  854. {
  855. struct snd_pcm_runtime *runtime;
  856. unsigned int channel;
  857. channel = info->channel;
  858. runtime = substream->runtime;
  859. snd_pcm_stream_lock_irq(substream);
  860. if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  861. snd_pcm_stream_unlock_irq(substream);
  862. return -EBADFD;
  863. }
  864. snd_pcm_stream_unlock_irq(substream);
  865. if (channel >= runtime->channels)
  866. return -EINVAL;
  867. memset(info, 0, sizeof(*info));
  868. info->channel = channel;
  869. return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
  870. }
  871. static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
  872. struct snd_pcm_channel_info __user * _info)
  873. {
  874. struct snd_pcm_channel_info info;
  875. int res;
  876. if (copy_from_user(&info, _info, sizeof(info)))
  877. return -EFAULT;
  878. res = snd_pcm_channel_info(substream, &info);
  879. if (res < 0)
  880. return res;
  881. if (copy_to_user(_info, &info, sizeof(info)))
  882. return -EFAULT;
  883. return 0;
  884. }
  885. static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
  886. {
  887. struct snd_pcm_runtime *runtime = substream->runtime;
  888. if (runtime->trigger_master == NULL)
  889. return;
  890. if (runtime->trigger_master == substream) {
  891. if (!runtime->trigger_tstamp_latched)
  892. snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
  893. } else {
  894. snd_pcm_trigger_tstamp(runtime->trigger_master);
  895. runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
  896. }
  897. runtime->trigger_master = NULL;
  898. }
  899. struct action_ops {
  900. int (*pre_action)(struct snd_pcm_substream *substream, int state);
  901. int (*do_action)(struct snd_pcm_substream *substream, int state);
  902. void (*undo_action)(struct snd_pcm_substream *substream, int state);
  903. void (*post_action)(struct snd_pcm_substream *substream, int state);
  904. };
  905. /*
  906. * this functions is core for handling of linked stream
  907. * Note: the stream state might be changed also on failure
  908. * Note2: call with calling stream lock + link lock
  909. */
  910. static int snd_pcm_action_group(const struct action_ops *ops,
  911. struct snd_pcm_substream *substream,
  912. int state, int do_lock)
  913. {
  914. struct snd_pcm_substream *s = NULL;
  915. struct snd_pcm_substream *s1;
  916. int res = 0, depth = 1;
  917. snd_pcm_group_for_each_entry(s, substream) {
  918. if (do_lock && s != substream) {
  919. if (s->pcm->nonatomic)
  920. mutex_lock_nested(&s->self_group.mutex, depth);
  921. else
  922. spin_lock_nested(&s->self_group.lock, depth);
  923. depth++;
  924. }
  925. res = ops->pre_action(s, state);
  926. if (res < 0)
  927. goto _unlock;
  928. }
  929. snd_pcm_group_for_each_entry(s, substream) {
  930. res = ops->do_action(s, state);
  931. if (res < 0) {
  932. if (ops->undo_action) {
  933. snd_pcm_group_for_each_entry(s1, substream) {
  934. if (s1 == s) /* failed stream */
  935. break;
  936. ops->undo_action(s1, state);
  937. }
  938. }
  939. s = NULL; /* unlock all */
  940. goto _unlock;
  941. }
  942. }
  943. snd_pcm_group_for_each_entry(s, substream) {
  944. ops->post_action(s, state);
  945. }
  946. _unlock:
  947. if (do_lock) {
  948. /* unlock streams */
  949. snd_pcm_group_for_each_entry(s1, substream) {
  950. if (s1 != substream) {
  951. if (s1->pcm->nonatomic)
  952. mutex_unlock(&s1->self_group.mutex);
  953. else
  954. spin_unlock(&s1->self_group.lock);
  955. }
  956. if (s1 == s) /* end */
  957. break;
  958. }
  959. }
  960. return res;
  961. }
  962. /*
  963. * Note: call with stream lock
  964. */
  965. static int snd_pcm_action_single(const struct action_ops *ops,
  966. struct snd_pcm_substream *substream,
  967. int state)
  968. {
  969. int res;
  970. res = ops->pre_action(substream, state);
  971. if (res < 0)
  972. return res;
  973. res = ops->do_action(substream, state);
  974. if (res == 0)
  975. ops->post_action(substream, state);
  976. else if (ops->undo_action)
  977. ops->undo_action(substream, state);
  978. return res;
  979. }
  980. /*
  981. * Note: call with stream lock
  982. */
  983. static int snd_pcm_action(const struct action_ops *ops,
  984. struct snd_pcm_substream *substream,
  985. int state)
  986. {
  987. int res;
  988. if (!snd_pcm_stream_linked(substream))
  989. return snd_pcm_action_single(ops, substream, state);
  990. if (substream->pcm->nonatomic) {
  991. if (!mutex_trylock(&substream->group->mutex)) {
  992. mutex_unlock(&substream->self_group.mutex);
  993. mutex_lock(&substream->group->mutex);
  994. mutex_lock(&substream->self_group.mutex);
  995. }
  996. res = snd_pcm_action_group(ops, substream, state, 1);
  997. mutex_unlock(&substream->group->mutex);
  998. } else {
  999. if (!spin_trylock(&substream->group->lock)) {
  1000. spin_unlock(&substream->self_group.lock);
  1001. spin_lock(&substream->group->lock);
  1002. spin_lock(&substream->self_group.lock);
  1003. }
  1004. res = snd_pcm_action_group(ops, substream, state, 1);
  1005. spin_unlock(&substream->group->lock);
  1006. }
  1007. return res;
  1008. }
  1009. /*
  1010. * Note: don't use any locks before
  1011. */
  1012. static int snd_pcm_action_lock_irq(const struct action_ops *ops,
  1013. struct snd_pcm_substream *substream,
  1014. int state)
  1015. {
  1016. int res;
  1017. snd_pcm_stream_lock_irq(substream);
  1018. res = snd_pcm_action(ops, substream, state);
  1019. snd_pcm_stream_unlock_irq(substream);
  1020. return res;
  1021. }
  1022. /*
  1023. */
  1024. static int snd_pcm_action_nonatomic(const struct action_ops *ops,
  1025. struct snd_pcm_substream *substream,
  1026. int state)
  1027. {
  1028. int res;
  1029. down_read(&snd_pcm_link_rwsem);
  1030. if (snd_pcm_stream_linked(substream))
  1031. res = snd_pcm_action_group(ops, substream, state, 0);
  1032. else
  1033. res = snd_pcm_action_single(ops, substream, state);
  1034. up_read(&snd_pcm_link_rwsem);
  1035. return res;
  1036. }
  1037. /*
  1038. * start callbacks
  1039. */
  1040. static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
  1041. {
  1042. struct snd_pcm_runtime *runtime = substream->runtime;
  1043. if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
  1044. return -EBADFD;
  1045. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  1046. !snd_pcm_playback_data(substream))
  1047. return -EPIPE;
  1048. runtime->trigger_tstamp_latched = false;
  1049. runtime->trigger_master = substream;
  1050. return 0;
  1051. }
  1052. static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
  1053. {
  1054. if (substream->runtime->trigger_master != substream)
  1055. return 0;
  1056. return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
  1057. }
  1058. static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
  1059. {
  1060. if (substream->runtime->trigger_master == substream)
  1061. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
  1062. }
  1063. static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
  1064. {
  1065. struct snd_pcm_runtime *runtime = substream->runtime;
  1066. snd_pcm_trigger_tstamp(substream);
  1067. runtime->hw_ptr_jiffies = jiffies;
  1068. runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
  1069. runtime->rate;
  1070. runtime->status->state = state;
  1071. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  1072. runtime->silence_size > 0)
  1073. snd_pcm_playback_silence(substream, ULONG_MAX);
  1074. snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
  1075. }
  1076. static const struct action_ops snd_pcm_action_start = {
  1077. .pre_action = snd_pcm_pre_start,
  1078. .do_action = snd_pcm_do_start,
  1079. .undo_action = snd_pcm_undo_start,
  1080. .post_action = snd_pcm_post_start
  1081. };
  1082. /**
  1083. * snd_pcm_start - start all linked streams
  1084. * @substream: the PCM substream instance
  1085. *
  1086. * Return: Zero if successful, or a negative error code.
  1087. * The stream lock must be acquired before calling this function.
  1088. */
  1089. int snd_pcm_start(struct snd_pcm_substream *substream)
  1090. {
  1091. return snd_pcm_action(&snd_pcm_action_start, substream,
  1092. SNDRV_PCM_STATE_RUNNING);
  1093. }
  1094. /* take the stream lock and start the streams */
  1095. static int snd_pcm_start_lock_irq(struct snd_pcm_substream *substream)
  1096. {
  1097. return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream,
  1098. SNDRV_PCM_STATE_RUNNING);
  1099. }
  1100. /*
  1101. * stop callbacks
  1102. */
  1103. static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
  1104. {
  1105. struct snd_pcm_runtime *runtime = substream->runtime;
  1106. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1107. return -EBADFD;
  1108. runtime->trigger_master = substream;
  1109. return 0;
  1110. }
  1111. static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
  1112. {
  1113. if (substream->runtime->trigger_master == substream &&
  1114. snd_pcm_running(substream))
  1115. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
  1116. return 0; /* unconditonally stop all substreams */
  1117. }
  1118. static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
  1119. {
  1120. struct snd_pcm_runtime *runtime = substream->runtime;
  1121. if (runtime->status->state != state) {
  1122. snd_pcm_trigger_tstamp(substream);
  1123. runtime->status->state = state;
  1124. snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
  1125. }
  1126. wake_up(&runtime->sleep);
  1127. wake_up(&runtime->tsleep);
  1128. }
  1129. static const struct action_ops snd_pcm_action_stop = {
  1130. .pre_action = snd_pcm_pre_stop,
  1131. .do_action = snd_pcm_do_stop,
  1132. .post_action = snd_pcm_post_stop
  1133. };
  1134. /**
  1135. * snd_pcm_stop - try to stop all running streams in the substream group
  1136. * @substream: the PCM substream instance
  1137. * @state: PCM state after stopping the stream
  1138. *
  1139. * The state of each stream is then changed to the given state unconditionally.
  1140. *
  1141. * Return: Zero if successful, or a negative error code.
  1142. */
  1143. int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
  1144. {
  1145. return snd_pcm_action(&snd_pcm_action_stop, substream, state);
  1146. }
  1147. EXPORT_SYMBOL(snd_pcm_stop);
  1148. /**
  1149. * snd_pcm_drain_done - stop the DMA only when the given stream is playback
  1150. * @substream: the PCM substream
  1151. *
  1152. * After stopping, the state is changed to SETUP.
  1153. * Unlike snd_pcm_stop(), this affects only the given stream.
  1154. *
  1155. * Return: Zero if succesful, or a negative error code.
  1156. */
  1157. int snd_pcm_drain_done(struct snd_pcm_substream *substream)
  1158. {
  1159. return snd_pcm_action_single(&snd_pcm_action_stop, substream,
  1160. SNDRV_PCM_STATE_SETUP);
  1161. }
  1162. /**
  1163. * snd_pcm_stop_xrun - stop the running streams as XRUN
  1164. * @substream: the PCM substream instance
  1165. *
  1166. * This stops the given running substream (and all linked substreams) as XRUN.
  1167. * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
  1168. *
  1169. * Return: Zero if successful, or a negative error code.
  1170. */
  1171. int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
  1172. {
  1173. unsigned long flags;
  1174. int ret = 0;
  1175. snd_pcm_stream_lock_irqsave(substream, flags);
  1176. if (snd_pcm_running(substream))
  1177. ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  1178. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1179. return ret;
  1180. }
  1181. EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
  1182. /*
  1183. * pause callbacks
  1184. */
  1185. static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
  1186. {
  1187. struct snd_pcm_runtime *runtime = substream->runtime;
  1188. if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
  1189. return -ENOSYS;
  1190. if (push) {
  1191. if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
  1192. return -EBADFD;
  1193. } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
  1194. return -EBADFD;
  1195. runtime->trigger_master = substream;
  1196. return 0;
  1197. }
  1198. static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
  1199. {
  1200. if (substream->runtime->trigger_master != substream)
  1201. return 0;
  1202. /* some drivers might use hw_ptr to recover from the pause -
  1203. update the hw_ptr now */
  1204. if (push)
  1205. snd_pcm_update_hw_ptr(substream);
  1206. /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
  1207. * a delta between the current jiffies, this gives a large enough
  1208. * delta, effectively to skip the check once.
  1209. */
  1210. substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
  1211. return substream->ops->trigger(substream,
  1212. push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
  1213. SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
  1214. }
  1215. static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
  1216. {
  1217. if (substream->runtime->trigger_master == substream)
  1218. substream->ops->trigger(substream,
  1219. push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
  1220. SNDRV_PCM_TRIGGER_PAUSE_PUSH);
  1221. }
  1222. static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
  1223. {
  1224. struct snd_pcm_runtime *runtime = substream->runtime;
  1225. snd_pcm_trigger_tstamp(substream);
  1226. if (push) {
  1227. runtime->status->state = SNDRV_PCM_STATE_PAUSED;
  1228. snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
  1229. wake_up(&runtime->sleep);
  1230. wake_up(&runtime->tsleep);
  1231. } else {
  1232. runtime->status->state = SNDRV_PCM_STATE_RUNNING;
  1233. snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
  1234. }
  1235. }
  1236. static const struct action_ops snd_pcm_action_pause = {
  1237. .pre_action = snd_pcm_pre_pause,
  1238. .do_action = snd_pcm_do_pause,
  1239. .undo_action = snd_pcm_undo_pause,
  1240. .post_action = snd_pcm_post_pause
  1241. };
  1242. /*
  1243. * Push/release the pause for all linked streams.
  1244. */
  1245. static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
  1246. {
  1247. return snd_pcm_action(&snd_pcm_action_pause, substream, push);
  1248. }
  1249. #ifdef CONFIG_PM
  1250. /* suspend */
  1251. static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
  1252. {
  1253. struct snd_pcm_runtime *runtime = substream->runtime;
  1254. if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
  1255. return -EBUSY;
  1256. runtime->trigger_master = substream;
  1257. return 0;
  1258. }
  1259. static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
  1260. {
  1261. struct snd_pcm_runtime *runtime = substream->runtime;
  1262. if (runtime->trigger_master != substream)
  1263. return 0;
  1264. if (! snd_pcm_running(substream))
  1265. return 0;
  1266. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
  1267. return 0; /* suspend unconditionally */
  1268. }
  1269. static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
  1270. {
  1271. struct snd_pcm_runtime *runtime = substream->runtime;
  1272. snd_pcm_trigger_tstamp(substream);
  1273. runtime->status->suspended_state = runtime->status->state;
  1274. runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
  1275. snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
  1276. wake_up(&runtime->sleep);
  1277. wake_up(&runtime->tsleep);
  1278. }
  1279. static const struct action_ops snd_pcm_action_suspend = {
  1280. .pre_action = snd_pcm_pre_suspend,
  1281. .do_action = snd_pcm_do_suspend,
  1282. .post_action = snd_pcm_post_suspend
  1283. };
  1284. /**
  1285. * snd_pcm_suspend - trigger SUSPEND to all linked streams
  1286. * @substream: the PCM substream
  1287. *
  1288. * After this call, all streams are changed to SUSPENDED state.
  1289. *
  1290. * Return: Zero if successful (or @substream is %NULL), or a negative error
  1291. * code.
  1292. */
  1293. int snd_pcm_suspend(struct snd_pcm_substream *substream)
  1294. {
  1295. int err;
  1296. unsigned long flags;
  1297. if (! substream)
  1298. return 0;
  1299. snd_pcm_stream_lock_irqsave(substream, flags);
  1300. err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
  1301. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1302. return err;
  1303. }
  1304. EXPORT_SYMBOL(snd_pcm_suspend);
  1305. /**
  1306. * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
  1307. * @pcm: the PCM instance
  1308. *
  1309. * After this call, all streams are changed to SUSPENDED state.
  1310. *
  1311. * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
  1312. */
  1313. int snd_pcm_suspend_all(struct snd_pcm *pcm)
  1314. {
  1315. struct snd_pcm_substream *substream;
  1316. int stream, err = 0;
  1317. if (! pcm)
  1318. return 0;
  1319. for (stream = 0; stream < 2; stream++) {
  1320. for (substream = pcm->streams[stream].substream;
  1321. substream; substream = substream->next) {
  1322. /* FIXME: the open/close code should lock this as well */
  1323. if (substream->runtime == NULL)
  1324. continue;
  1325. err = snd_pcm_suspend(substream);
  1326. if (err < 0 && err != -EBUSY)
  1327. return err;
  1328. }
  1329. }
  1330. return 0;
  1331. }
  1332. EXPORT_SYMBOL(snd_pcm_suspend_all);
  1333. /* resume */
  1334. static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
  1335. {
  1336. struct snd_pcm_runtime *runtime = substream->runtime;
  1337. if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
  1338. return -ENOSYS;
  1339. runtime->trigger_master = substream;
  1340. return 0;
  1341. }
  1342. static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
  1343. {
  1344. struct snd_pcm_runtime *runtime = substream->runtime;
  1345. if (runtime->trigger_master != substream)
  1346. return 0;
  1347. /* DMA not running previously? */
  1348. if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
  1349. (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
  1350. substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
  1351. return 0;
  1352. return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
  1353. }
  1354. static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
  1355. {
  1356. if (substream->runtime->trigger_master == substream &&
  1357. snd_pcm_running(substream))
  1358. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
  1359. }
  1360. static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
  1361. {
  1362. struct snd_pcm_runtime *runtime = substream->runtime;
  1363. snd_pcm_trigger_tstamp(substream);
  1364. runtime->status->state = runtime->status->suspended_state;
  1365. snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
  1366. }
  1367. static const struct action_ops snd_pcm_action_resume = {
  1368. .pre_action = snd_pcm_pre_resume,
  1369. .do_action = snd_pcm_do_resume,
  1370. .undo_action = snd_pcm_undo_resume,
  1371. .post_action = snd_pcm_post_resume
  1372. };
  1373. static int snd_pcm_resume(struct snd_pcm_substream *substream)
  1374. {
  1375. return snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
  1376. }
  1377. #else
  1378. static int snd_pcm_resume(struct snd_pcm_substream *substream)
  1379. {
  1380. return -ENOSYS;
  1381. }
  1382. #endif /* CONFIG_PM */
  1383. /*
  1384. * xrun ioctl
  1385. *
  1386. * Change the RUNNING stream(s) to XRUN state.
  1387. */
  1388. static int snd_pcm_xrun(struct snd_pcm_substream *substream)
  1389. {
  1390. struct snd_pcm_runtime *runtime = substream->runtime;
  1391. int result;
  1392. snd_pcm_stream_lock_irq(substream);
  1393. switch (runtime->status->state) {
  1394. case SNDRV_PCM_STATE_XRUN:
  1395. result = 0; /* already there */
  1396. break;
  1397. case SNDRV_PCM_STATE_RUNNING:
  1398. result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  1399. break;
  1400. default:
  1401. result = -EBADFD;
  1402. }
  1403. snd_pcm_stream_unlock_irq(substream);
  1404. return result;
  1405. }
  1406. /*
  1407. * reset ioctl
  1408. */
  1409. static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
  1410. {
  1411. struct snd_pcm_runtime *runtime = substream->runtime;
  1412. switch (runtime->status->state) {
  1413. case SNDRV_PCM_STATE_RUNNING:
  1414. case SNDRV_PCM_STATE_PREPARED:
  1415. case SNDRV_PCM_STATE_PAUSED:
  1416. case SNDRV_PCM_STATE_SUSPENDED:
  1417. return 0;
  1418. default:
  1419. return -EBADFD;
  1420. }
  1421. }
  1422. static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
  1423. {
  1424. struct snd_pcm_runtime *runtime = substream->runtime;
  1425. int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
  1426. if (err < 0)
  1427. return err;
  1428. runtime->hw_ptr_base = 0;
  1429. runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
  1430. runtime->status->hw_ptr % runtime->period_size;
  1431. runtime->silence_start = runtime->status->hw_ptr;
  1432. runtime->silence_filled = 0;
  1433. return 0;
  1434. }
  1435. static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
  1436. {
  1437. struct snd_pcm_runtime *runtime = substream->runtime;
  1438. runtime->control->appl_ptr = runtime->status->hw_ptr;
  1439. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  1440. runtime->silence_size > 0)
  1441. snd_pcm_playback_silence(substream, ULONG_MAX);
  1442. }
  1443. static const struct action_ops snd_pcm_action_reset = {
  1444. .pre_action = snd_pcm_pre_reset,
  1445. .do_action = snd_pcm_do_reset,
  1446. .post_action = snd_pcm_post_reset
  1447. };
  1448. static int snd_pcm_reset(struct snd_pcm_substream *substream)
  1449. {
  1450. return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
  1451. }
  1452. /*
  1453. * prepare ioctl
  1454. */
  1455. /* we use the second argument for updating f_flags */
  1456. static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
  1457. int f_flags)
  1458. {
  1459. struct snd_pcm_runtime *runtime = substream->runtime;
  1460. if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
  1461. runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
  1462. return -EBADFD;
  1463. if (snd_pcm_running(substream))
  1464. return -EBUSY;
  1465. substream->f_flags = f_flags;
  1466. return 0;
  1467. }
  1468. static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
  1469. {
  1470. int err;
  1471. err = substream->ops->prepare(substream);
  1472. if (err < 0)
  1473. return err;
  1474. return snd_pcm_do_reset(substream, 0);
  1475. }
  1476. static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
  1477. {
  1478. struct snd_pcm_runtime *runtime = substream->runtime;
  1479. runtime->control->appl_ptr = runtime->status->hw_ptr;
  1480. snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
  1481. }
  1482. static const struct action_ops snd_pcm_action_prepare = {
  1483. .pre_action = snd_pcm_pre_prepare,
  1484. .do_action = snd_pcm_do_prepare,
  1485. .post_action = snd_pcm_post_prepare
  1486. };
  1487. /**
  1488. * snd_pcm_prepare - prepare the PCM substream to be triggerable
  1489. * @substream: the PCM substream instance
  1490. * @file: file to refer f_flags
  1491. *
  1492. * Return: Zero if successful, or a negative error code.
  1493. */
  1494. static int snd_pcm_prepare(struct snd_pcm_substream *substream,
  1495. struct file *file)
  1496. {
  1497. int f_flags;
  1498. if (file)
  1499. f_flags = file->f_flags;
  1500. else
  1501. f_flags = substream->f_flags;
  1502. snd_pcm_stream_lock_irq(substream);
  1503. switch (substream->runtime->status->state) {
  1504. case SNDRV_PCM_STATE_PAUSED:
  1505. snd_pcm_pause(substream, 0);
  1506. /* fallthru */
  1507. case SNDRV_PCM_STATE_SUSPENDED:
  1508. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1509. break;
  1510. }
  1511. snd_pcm_stream_unlock_irq(substream);
  1512. return snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
  1513. substream, f_flags);
  1514. }
  1515. /*
  1516. * drain ioctl
  1517. */
  1518. static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
  1519. {
  1520. struct snd_pcm_runtime *runtime = substream->runtime;
  1521. switch (runtime->status->state) {
  1522. case SNDRV_PCM_STATE_OPEN:
  1523. case SNDRV_PCM_STATE_DISCONNECTED:
  1524. case SNDRV_PCM_STATE_SUSPENDED:
  1525. return -EBADFD;
  1526. }
  1527. runtime->trigger_master = substream;
  1528. return 0;
  1529. }
  1530. static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
  1531. {
  1532. struct snd_pcm_runtime *runtime = substream->runtime;
  1533. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1534. switch (runtime->status->state) {
  1535. case SNDRV_PCM_STATE_PREPARED:
  1536. /* start playback stream if possible */
  1537. if (! snd_pcm_playback_empty(substream)) {
  1538. snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
  1539. snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
  1540. } else {
  1541. runtime->status->state = SNDRV_PCM_STATE_SETUP;
  1542. }
  1543. break;
  1544. case SNDRV_PCM_STATE_RUNNING:
  1545. runtime->status->state = SNDRV_PCM_STATE_DRAINING;
  1546. break;
  1547. case SNDRV_PCM_STATE_XRUN:
  1548. runtime->status->state = SNDRV_PCM_STATE_SETUP;
  1549. break;
  1550. default:
  1551. break;
  1552. }
  1553. } else {
  1554. /* stop running stream */
  1555. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
  1556. int new_state = snd_pcm_capture_avail(runtime) > 0 ?
  1557. SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
  1558. snd_pcm_do_stop(substream, new_state);
  1559. snd_pcm_post_stop(substream, new_state);
  1560. }
  1561. }
  1562. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
  1563. runtime->trigger_master == substream &&
  1564. (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
  1565. return substream->ops->trigger(substream,
  1566. SNDRV_PCM_TRIGGER_DRAIN);
  1567. return 0;
  1568. }
  1569. static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
  1570. {
  1571. }
  1572. static const struct action_ops snd_pcm_action_drain_init = {
  1573. .pre_action = snd_pcm_pre_drain_init,
  1574. .do_action = snd_pcm_do_drain_init,
  1575. .post_action = snd_pcm_post_drain_init
  1576. };
  1577. static int snd_pcm_drop(struct snd_pcm_substream *substream);
  1578. /*
  1579. * Drain the stream(s).
  1580. * When the substream is linked, sync until the draining of all playback streams
  1581. * is finished.
  1582. * After this call, all streams are supposed to be either SETUP or DRAINING
  1583. * (capture only) state.
  1584. */
  1585. static int snd_pcm_drain(struct snd_pcm_substream *substream,
  1586. struct file *file)
  1587. {
  1588. struct snd_card *card;
  1589. struct snd_pcm_runtime *runtime;
  1590. struct snd_pcm_substream *s;
  1591. wait_queue_entry_t wait;
  1592. int result = 0;
  1593. int nonblock = 0;
  1594. card = substream->pcm->card;
  1595. runtime = substream->runtime;
  1596. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1597. return -EBADFD;
  1598. if (file) {
  1599. if (file->f_flags & O_NONBLOCK)
  1600. nonblock = 1;
  1601. } else if (substream->f_flags & O_NONBLOCK)
  1602. nonblock = 1;
  1603. down_read(&snd_pcm_link_rwsem);
  1604. snd_pcm_stream_lock_irq(substream);
  1605. /* resume pause */
  1606. if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
  1607. snd_pcm_pause(substream, 0);
  1608. /* pre-start/stop - all running streams are changed to DRAINING state */
  1609. result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
  1610. if (result < 0)
  1611. goto unlock;
  1612. /* in non-blocking, we don't wait in ioctl but let caller poll */
  1613. if (nonblock) {
  1614. result = -EAGAIN;
  1615. goto unlock;
  1616. }
  1617. for (;;) {
  1618. long tout;
  1619. struct snd_pcm_runtime *to_check;
  1620. if (signal_pending(current)) {
  1621. result = -ERESTARTSYS;
  1622. break;
  1623. }
  1624. /* find a substream to drain */
  1625. to_check = NULL;
  1626. snd_pcm_group_for_each_entry(s, substream) {
  1627. if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
  1628. continue;
  1629. runtime = s->runtime;
  1630. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  1631. to_check = runtime;
  1632. break;
  1633. }
  1634. }
  1635. if (!to_check)
  1636. break; /* all drained */
  1637. init_waitqueue_entry(&wait, current);
  1638. add_wait_queue(&to_check->sleep, &wait);
  1639. snd_pcm_stream_unlock_irq(substream);
  1640. up_read(&snd_pcm_link_rwsem);
  1641. if (runtime->no_period_wakeup)
  1642. tout = MAX_SCHEDULE_TIMEOUT;
  1643. else {
  1644. tout = 10;
  1645. if (runtime->rate) {
  1646. long t = runtime->period_size * 2 / runtime->rate;
  1647. tout = max(t, tout);
  1648. }
  1649. tout = msecs_to_jiffies(tout * 1000);
  1650. }
  1651. tout = schedule_timeout_interruptible(tout);
  1652. down_read(&snd_pcm_link_rwsem);
  1653. snd_pcm_stream_lock_irq(substream);
  1654. remove_wait_queue(&to_check->sleep, &wait);
  1655. if (card->shutdown) {
  1656. result = -ENODEV;
  1657. break;
  1658. }
  1659. if (tout == 0) {
  1660. if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
  1661. result = -ESTRPIPE;
  1662. else {
  1663. dev_dbg(substream->pcm->card->dev,
  1664. "playback drain error (DMA or IRQ trouble?)\n");
  1665. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1666. result = -EIO;
  1667. }
  1668. break;
  1669. }
  1670. }
  1671. unlock:
  1672. snd_pcm_stream_unlock_irq(substream);
  1673. up_read(&snd_pcm_link_rwsem);
  1674. return result;
  1675. }
  1676. /*
  1677. * drop ioctl
  1678. *
  1679. * Immediately put all linked substreams into SETUP state.
  1680. */
  1681. static int snd_pcm_drop(struct snd_pcm_substream *substream)
  1682. {
  1683. struct snd_pcm_runtime *runtime;
  1684. int result = 0;
  1685. if (PCM_RUNTIME_CHECK(substream))
  1686. return -ENXIO;
  1687. runtime = substream->runtime;
  1688. if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
  1689. runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
  1690. return -EBADFD;
  1691. snd_pcm_stream_lock_irq(substream);
  1692. /* resume pause */
  1693. if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
  1694. snd_pcm_pause(substream, 0);
  1695. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1696. /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
  1697. snd_pcm_stream_unlock_irq(substream);
  1698. return result;
  1699. }
  1700. static bool is_pcm_file(struct file *file)
  1701. {
  1702. struct inode *inode = file_inode(file);
  1703. unsigned int minor;
  1704. if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
  1705. return false;
  1706. minor = iminor(inode);
  1707. return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
  1708. snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
  1709. }
  1710. /*
  1711. * PCM link handling
  1712. */
  1713. static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
  1714. {
  1715. int res = 0;
  1716. struct snd_pcm_file *pcm_file;
  1717. struct snd_pcm_substream *substream1;
  1718. struct snd_pcm_group *group;
  1719. struct fd f = fdget(fd);
  1720. if (!f.file)
  1721. return -EBADFD;
  1722. if (!is_pcm_file(f.file)) {
  1723. res = -EBADFD;
  1724. goto _badf;
  1725. }
  1726. pcm_file = f.file->private_data;
  1727. substream1 = pcm_file->substream;
  1728. group = kmalloc(sizeof(*group), GFP_KERNEL);
  1729. if (!group) {
  1730. res = -ENOMEM;
  1731. goto _nolock;
  1732. }
  1733. down_write_nonblock(&snd_pcm_link_rwsem);
  1734. write_lock_irq(&snd_pcm_link_rwlock);
  1735. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
  1736. substream->runtime->status->state != substream1->runtime->status->state ||
  1737. substream->pcm->nonatomic != substream1->pcm->nonatomic) {
  1738. res = -EBADFD;
  1739. goto _end;
  1740. }
  1741. if (snd_pcm_stream_linked(substream1)) {
  1742. res = -EALREADY;
  1743. goto _end;
  1744. }
  1745. if (!snd_pcm_stream_linked(substream)) {
  1746. substream->group = group;
  1747. group = NULL;
  1748. spin_lock_init(&substream->group->lock);
  1749. mutex_init(&substream->group->mutex);
  1750. INIT_LIST_HEAD(&substream->group->substreams);
  1751. list_add_tail(&substream->link_list, &substream->group->substreams);
  1752. substream->group->count = 1;
  1753. }
  1754. list_add_tail(&substream1->link_list, &substream->group->substreams);
  1755. substream->group->count++;
  1756. substream1->group = substream->group;
  1757. _end:
  1758. write_unlock_irq(&snd_pcm_link_rwlock);
  1759. up_write(&snd_pcm_link_rwsem);
  1760. _nolock:
  1761. snd_card_unref(substream1->pcm->card);
  1762. kfree(group);
  1763. _badf:
  1764. fdput(f);
  1765. return res;
  1766. }
  1767. static void relink_to_local(struct snd_pcm_substream *substream)
  1768. {
  1769. substream->group = &substream->self_group;
  1770. INIT_LIST_HEAD(&substream->self_group.substreams);
  1771. list_add_tail(&substream->link_list, &substream->self_group.substreams);
  1772. }
  1773. static int snd_pcm_unlink(struct snd_pcm_substream *substream)
  1774. {
  1775. struct snd_pcm_substream *s;
  1776. int res = 0;
  1777. down_write_nonblock(&snd_pcm_link_rwsem);
  1778. write_lock_irq(&snd_pcm_link_rwlock);
  1779. if (!snd_pcm_stream_linked(substream)) {
  1780. res = -EALREADY;
  1781. goto _end;
  1782. }
  1783. list_del(&substream->link_list);
  1784. substream->group->count--;
  1785. if (substream->group->count == 1) { /* detach the last stream, too */
  1786. snd_pcm_group_for_each_entry(s, substream) {
  1787. relink_to_local(s);
  1788. break;
  1789. }
  1790. kfree(substream->group);
  1791. }
  1792. relink_to_local(substream);
  1793. _end:
  1794. write_unlock_irq(&snd_pcm_link_rwlock);
  1795. up_write(&snd_pcm_link_rwsem);
  1796. return res;
  1797. }
  1798. /*
  1799. * hw configurator
  1800. */
  1801. static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
  1802. struct snd_pcm_hw_rule *rule)
  1803. {
  1804. struct snd_interval t;
  1805. snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
  1806. hw_param_interval_c(params, rule->deps[1]), &t);
  1807. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1808. }
  1809. static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
  1810. struct snd_pcm_hw_rule *rule)
  1811. {
  1812. struct snd_interval t;
  1813. snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
  1814. hw_param_interval_c(params, rule->deps[1]), &t);
  1815. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1816. }
  1817. static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
  1818. struct snd_pcm_hw_rule *rule)
  1819. {
  1820. struct snd_interval t;
  1821. snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
  1822. hw_param_interval_c(params, rule->deps[1]),
  1823. (unsigned long) rule->private, &t);
  1824. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1825. }
  1826. static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
  1827. struct snd_pcm_hw_rule *rule)
  1828. {
  1829. struct snd_interval t;
  1830. snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
  1831. (unsigned long) rule->private,
  1832. hw_param_interval_c(params, rule->deps[1]), &t);
  1833. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1834. }
  1835. static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
  1836. struct snd_pcm_hw_rule *rule)
  1837. {
  1838. unsigned int k;
  1839. const struct snd_interval *i =
  1840. hw_param_interval_c(params, rule->deps[0]);
  1841. struct snd_mask m;
  1842. struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  1843. snd_mask_any(&m);
  1844. for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
  1845. int bits;
  1846. if (! snd_mask_test(mask, k))
  1847. continue;
  1848. bits = snd_pcm_format_physical_width(k);
  1849. if (bits <= 0)
  1850. continue; /* ignore invalid formats */
  1851. if ((unsigned)bits < i->min || (unsigned)bits > i->max)
  1852. snd_mask_reset(&m, k);
  1853. }
  1854. return snd_mask_refine(mask, &m);
  1855. }
  1856. static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
  1857. struct snd_pcm_hw_rule *rule)
  1858. {
  1859. struct snd_interval t;
  1860. unsigned int k;
  1861. t.min = UINT_MAX;
  1862. t.max = 0;
  1863. t.openmin = 0;
  1864. t.openmax = 0;
  1865. for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
  1866. int bits;
  1867. if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
  1868. continue;
  1869. bits = snd_pcm_format_physical_width(k);
  1870. if (bits <= 0)
  1871. continue; /* ignore invalid formats */
  1872. if (t.min > (unsigned)bits)
  1873. t.min = bits;
  1874. if (t.max < (unsigned)bits)
  1875. t.max = bits;
  1876. }
  1877. t.integer = 1;
  1878. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1879. }
  1880. #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
  1881. #error "Change this table"
  1882. #endif
  1883. static const unsigned int rates[] = {
  1884. 5512, 8000, 11025, 16000, 22050, 32000, 44100,
  1885. 48000, 64000, 88200, 96000, 176400, 192000
  1886. };
  1887. const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
  1888. .count = ARRAY_SIZE(rates),
  1889. .list = rates,
  1890. };
  1891. static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
  1892. struct snd_pcm_hw_rule *rule)
  1893. {
  1894. struct snd_pcm_hardware *hw = rule->private;
  1895. return snd_interval_list(hw_param_interval(params, rule->var),
  1896. snd_pcm_known_rates.count,
  1897. snd_pcm_known_rates.list, hw->rates);
  1898. }
  1899. static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
  1900. struct snd_pcm_hw_rule *rule)
  1901. {
  1902. struct snd_interval t;
  1903. struct snd_pcm_substream *substream = rule->private;
  1904. t.min = 0;
  1905. t.max = substream->buffer_bytes_max;
  1906. t.openmin = 0;
  1907. t.openmax = 0;
  1908. t.integer = 1;
  1909. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1910. }
  1911. int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
  1912. {
  1913. struct snd_pcm_runtime *runtime = substream->runtime;
  1914. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1915. int k, err;
  1916. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
  1917. snd_mask_any(constrs_mask(constrs, k));
  1918. }
  1919. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
  1920. snd_interval_any(constrs_interval(constrs, k));
  1921. }
  1922. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
  1923. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
  1924. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
  1925. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
  1926. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
  1927. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  1928. snd_pcm_hw_rule_format, NULL,
  1929. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1930. if (err < 0)
  1931. return err;
  1932. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  1933. snd_pcm_hw_rule_sample_bits, NULL,
  1934. SNDRV_PCM_HW_PARAM_FORMAT,
  1935. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1936. if (err < 0)
  1937. return err;
  1938. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  1939. snd_pcm_hw_rule_div, NULL,
  1940. SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  1941. if (err < 0)
  1942. return err;
  1943. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1944. snd_pcm_hw_rule_mul, NULL,
  1945. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  1946. if (err < 0)
  1947. return err;
  1948. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1949. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1950. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
  1951. if (err < 0)
  1952. return err;
  1953. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1954. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1955. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
  1956. if (err < 0)
  1957. return err;
  1958. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  1959. snd_pcm_hw_rule_div, NULL,
  1960. SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1961. if (err < 0)
  1962. return err;
  1963. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1964. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1965. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
  1966. if (err < 0)
  1967. return err;
  1968. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1969. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1970. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
  1971. if (err < 0)
  1972. return err;
  1973. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
  1974. snd_pcm_hw_rule_div, NULL,
  1975. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
  1976. if (err < 0)
  1977. return err;
  1978. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1979. snd_pcm_hw_rule_div, NULL,
  1980. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
  1981. if (err < 0)
  1982. return err;
  1983. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1984. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1985. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1986. if (err < 0)
  1987. return err;
  1988. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1989. snd_pcm_hw_rule_muldivk, (void*) 1000000,
  1990. SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
  1991. if (err < 0)
  1992. return err;
  1993. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1994. snd_pcm_hw_rule_mul, NULL,
  1995. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
  1996. if (err < 0)
  1997. return err;
  1998. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1999. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  2000. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  2001. if (err < 0)
  2002. return err;
  2003. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  2004. snd_pcm_hw_rule_muldivk, (void*) 1000000,
  2005. SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
  2006. if (err < 0)
  2007. return err;
  2008. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  2009. snd_pcm_hw_rule_muldivk, (void*) 8,
  2010. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  2011. if (err < 0)
  2012. return err;
  2013. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  2014. snd_pcm_hw_rule_muldivk, (void*) 8,
  2015. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  2016. if (err < 0)
  2017. return err;
  2018. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  2019. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  2020. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
  2021. if (err < 0)
  2022. return err;
  2023. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
  2024. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  2025. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
  2026. if (err < 0)
  2027. return err;
  2028. return 0;
  2029. }
  2030. int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
  2031. {
  2032. struct snd_pcm_runtime *runtime = substream->runtime;
  2033. struct snd_pcm_hardware *hw = &runtime->hw;
  2034. int err;
  2035. unsigned int mask = 0;
  2036. if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
  2037. mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  2038. if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
  2039. mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
  2040. if (hw_support_mmap(substream)) {
  2041. if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
  2042. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
  2043. if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
  2044. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
  2045. if (hw->info & SNDRV_PCM_INFO_COMPLEX)
  2046. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
  2047. }
  2048. err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
  2049. if (err < 0)
  2050. return err;
  2051. err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
  2052. if (err < 0)
  2053. return err;
  2054. err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
  2055. if (err < 0)
  2056. return err;
  2057. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
  2058. hw->channels_min, hw->channels_max);
  2059. if (err < 0)
  2060. return err;
  2061. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
  2062. hw->rate_min, hw->rate_max);
  2063. if (err < 0)
  2064. return err;
  2065. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  2066. hw->period_bytes_min, hw->period_bytes_max);
  2067. if (err < 0)
  2068. return err;
  2069. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
  2070. hw->periods_min, hw->periods_max);
  2071. if (err < 0)
  2072. return err;
  2073. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  2074. hw->period_bytes_min, hw->buffer_bytes_max);
  2075. if (err < 0)
  2076. return err;
  2077. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  2078. snd_pcm_hw_rule_buffer_bytes_max, substream,
  2079. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
  2080. if (err < 0)
  2081. return err;
  2082. /* FIXME: remove */
  2083. if (runtime->dma_bytes) {
  2084. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
  2085. if (err < 0)
  2086. return err;
  2087. }
  2088. if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
  2089. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  2090. snd_pcm_hw_rule_rate, hw,
  2091. SNDRV_PCM_HW_PARAM_RATE, -1);
  2092. if (err < 0)
  2093. return err;
  2094. }
  2095. /* FIXME: this belong to lowlevel */
  2096. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
  2097. return 0;
  2098. }
  2099. static void pcm_release_private(struct snd_pcm_substream *substream)
  2100. {
  2101. snd_pcm_unlink(substream);
  2102. }
  2103. void snd_pcm_release_substream(struct snd_pcm_substream *substream)
  2104. {
  2105. substream->ref_count--;
  2106. if (substream->ref_count > 0)
  2107. return;
  2108. snd_pcm_drop(substream);
  2109. if (substream->hw_opened) {
  2110. if (substream->ops->hw_free &&
  2111. substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  2112. substream->ops->hw_free(substream);
  2113. substream->ops->close(substream);
  2114. substream->hw_opened = 0;
  2115. }
  2116. if (pm_qos_request_active(&substream->latency_pm_qos_req))
  2117. pm_qos_remove_request(&substream->latency_pm_qos_req);
  2118. if (substream->pcm_release) {
  2119. substream->pcm_release(substream);
  2120. substream->pcm_release = NULL;
  2121. }
  2122. snd_pcm_detach_substream(substream);
  2123. }
  2124. EXPORT_SYMBOL(snd_pcm_release_substream);
  2125. int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
  2126. struct file *file,
  2127. struct snd_pcm_substream **rsubstream)
  2128. {
  2129. struct snd_pcm_substream *substream;
  2130. int err;
  2131. err = snd_pcm_attach_substream(pcm, stream, file, &substream);
  2132. if (err < 0)
  2133. return err;
  2134. if (substream->ref_count > 1) {
  2135. *rsubstream = substream;
  2136. return 0;
  2137. }
  2138. err = snd_pcm_hw_constraints_init(substream);
  2139. if (err < 0) {
  2140. pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
  2141. goto error;
  2142. }
  2143. if ((err = substream->ops->open(substream)) < 0)
  2144. goto error;
  2145. substream->hw_opened = 1;
  2146. err = snd_pcm_hw_constraints_complete(substream);
  2147. if (err < 0) {
  2148. pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
  2149. goto error;
  2150. }
  2151. *rsubstream = substream;
  2152. return 0;
  2153. error:
  2154. snd_pcm_release_substream(substream);
  2155. return err;
  2156. }
  2157. EXPORT_SYMBOL(snd_pcm_open_substream);
  2158. static int snd_pcm_open_file(struct file *file,
  2159. struct snd_pcm *pcm,
  2160. int stream)
  2161. {
  2162. struct snd_pcm_file *pcm_file;
  2163. struct snd_pcm_substream *substream;
  2164. int err;
  2165. err = snd_pcm_open_substream(pcm, stream, file, &substream);
  2166. if (err < 0)
  2167. return err;
  2168. pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
  2169. if (pcm_file == NULL) {
  2170. snd_pcm_release_substream(substream);
  2171. return -ENOMEM;
  2172. }
  2173. pcm_file->substream = substream;
  2174. if (substream->ref_count == 1) {
  2175. substream->file = pcm_file;
  2176. substream->pcm_release = pcm_release_private;
  2177. }
  2178. file->private_data = pcm_file;
  2179. return 0;
  2180. }
  2181. static int snd_pcm_playback_open(struct inode *inode, struct file *file)
  2182. {
  2183. struct snd_pcm *pcm;
  2184. int err = nonseekable_open(inode, file);
  2185. if (err < 0)
  2186. return err;
  2187. pcm = snd_lookup_minor_data(iminor(inode),
  2188. SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
  2189. err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
  2190. if (pcm)
  2191. snd_card_unref(pcm->card);
  2192. return err;
  2193. }
  2194. static int snd_pcm_capture_open(struct inode *inode, struct file *file)
  2195. {
  2196. struct snd_pcm *pcm;
  2197. int err = nonseekable_open(inode, file);
  2198. if (err < 0)
  2199. return err;
  2200. pcm = snd_lookup_minor_data(iminor(inode),
  2201. SNDRV_DEVICE_TYPE_PCM_CAPTURE);
  2202. err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
  2203. if (pcm)
  2204. snd_card_unref(pcm->card);
  2205. return err;
  2206. }
  2207. static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
  2208. {
  2209. int err;
  2210. wait_queue_entry_t wait;
  2211. if (pcm == NULL) {
  2212. err = -ENODEV;
  2213. goto __error1;
  2214. }
  2215. err = snd_card_file_add(pcm->card, file);
  2216. if (err < 0)
  2217. goto __error1;
  2218. if (!try_module_get(pcm->card->module)) {
  2219. err = -EFAULT;
  2220. goto __error2;
  2221. }
  2222. init_waitqueue_entry(&wait, current);
  2223. add_wait_queue(&pcm->open_wait, &wait);
  2224. mutex_lock(&pcm->open_mutex);
  2225. while (1) {
  2226. err = snd_pcm_open_file(file, pcm, stream);
  2227. if (err >= 0)
  2228. break;
  2229. if (err == -EAGAIN) {
  2230. if (file->f_flags & O_NONBLOCK) {
  2231. err = -EBUSY;
  2232. break;
  2233. }
  2234. } else
  2235. break;
  2236. set_current_state(TASK_INTERRUPTIBLE);
  2237. mutex_unlock(&pcm->open_mutex);
  2238. schedule();
  2239. mutex_lock(&pcm->open_mutex);
  2240. if (pcm->card->shutdown) {
  2241. err = -ENODEV;
  2242. break;
  2243. }
  2244. if (signal_pending(current)) {
  2245. err = -ERESTARTSYS;
  2246. break;
  2247. }
  2248. }
  2249. remove_wait_queue(&pcm->open_wait, &wait);
  2250. mutex_unlock(&pcm->open_mutex);
  2251. if (err < 0)
  2252. goto __error;
  2253. return err;
  2254. __error:
  2255. module_put(pcm->card->module);
  2256. __error2:
  2257. snd_card_file_remove(pcm->card, file);
  2258. __error1:
  2259. return err;
  2260. }
  2261. static int snd_pcm_release(struct inode *inode, struct file *file)
  2262. {
  2263. struct snd_pcm *pcm;
  2264. struct snd_pcm_substream *substream;
  2265. struct snd_pcm_file *pcm_file;
  2266. pcm_file = file->private_data;
  2267. substream = pcm_file->substream;
  2268. if (snd_BUG_ON(!substream))
  2269. return -ENXIO;
  2270. pcm = substream->pcm;
  2271. mutex_lock(&pcm->open_mutex);
  2272. snd_pcm_release_substream(substream);
  2273. kfree(pcm_file);
  2274. mutex_unlock(&pcm->open_mutex);
  2275. wake_up(&pcm->open_wait);
  2276. module_put(pcm->card->module);
  2277. snd_card_file_remove(pcm->card, file);
  2278. return 0;
  2279. }
  2280. /* check and update PCM state; return 0 or a negative error
  2281. * call this inside PCM lock
  2282. */
  2283. static int do_pcm_hwsync(struct snd_pcm_substream *substream)
  2284. {
  2285. switch (substream->runtime->status->state) {
  2286. case SNDRV_PCM_STATE_DRAINING:
  2287. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  2288. return -EBADFD;
  2289. /* Fall through */
  2290. case SNDRV_PCM_STATE_RUNNING:
  2291. return snd_pcm_update_hw_ptr(substream);
  2292. case SNDRV_PCM_STATE_PREPARED:
  2293. case SNDRV_PCM_STATE_PAUSED:
  2294. return 0;
  2295. case SNDRV_PCM_STATE_SUSPENDED:
  2296. return -ESTRPIPE;
  2297. case SNDRV_PCM_STATE_XRUN:
  2298. return -EPIPE;
  2299. default:
  2300. return -EBADFD;
  2301. }
  2302. }
  2303. /* increase the appl_ptr; returns the processed frames or a negative error */
  2304. static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
  2305. snd_pcm_uframes_t frames,
  2306. snd_pcm_sframes_t avail)
  2307. {
  2308. struct snd_pcm_runtime *runtime = substream->runtime;
  2309. snd_pcm_sframes_t appl_ptr;
  2310. int ret;
  2311. if (avail <= 0)
  2312. return 0;
  2313. if (frames > (snd_pcm_uframes_t)avail)
  2314. frames = avail;
  2315. appl_ptr = runtime->control->appl_ptr + frames;
  2316. if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
  2317. appl_ptr -= runtime->boundary;
  2318. ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
  2319. return ret < 0 ? ret : frames;
  2320. }
  2321. /* decrease the appl_ptr; returns the processed frames or zero for error */
  2322. static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
  2323. snd_pcm_uframes_t frames,
  2324. snd_pcm_sframes_t avail)
  2325. {
  2326. struct snd_pcm_runtime *runtime = substream->runtime;
  2327. snd_pcm_sframes_t appl_ptr;
  2328. int ret;
  2329. if (avail <= 0)
  2330. return 0;
  2331. if (frames > (snd_pcm_uframes_t)avail)
  2332. frames = avail;
  2333. appl_ptr = runtime->control->appl_ptr - frames;
  2334. if (appl_ptr < 0)
  2335. appl_ptr += runtime->boundary;
  2336. ret = pcm_lib_apply_appl_ptr(substream, appl_ptr);
  2337. /* NOTE: we return zero for errors because PulseAudio gets depressed
  2338. * upon receiving an error from rewind ioctl and stops processing
  2339. * any longer. Returning zero means that no rewind is done, so
  2340. * it's not absolutely wrong to answer like that.
  2341. */
  2342. return ret < 0 ? 0 : frames;
  2343. }
  2344. static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
  2345. snd_pcm_uframes_t frames)
  2346. {
  2347. struct snd_pcm_runtime *runtime = substream->runtime;
  2348. snd_pcm_sframes_t ret;
  2349. if (frames == 0)
  2350. return 0;
  2351. snd_pcm_stream_lock_irq(substream);
  2352. ret = do_pcm_hwsync(substream);
  2353. if (!ret)
  2354. ret = rewind_appl_ptr(substream, frames,
  2355. snd_pcm_playback_hw_avail(runtime));
  2356. snd_pcm_stream_unlock_irq(substream);
  2357. return ret;
  2358. }
  2359. static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
  2360. snd_pcm_uframes_t frames)
  2361. {
  2362. struct snd_pcm_runtime *runtime = substream->runtime;
  2363. snd_pcm_sframes_t ret;
  2364. if (frames == 0)
  2365. return 0;
  2366. snd_pcm_stream_lock_irq(substream);
  2367. ret = do_pcm_hwsync(substream);
  2368. if (!ret)
  2369. ret = rewind_appl_ptr(substream, frames,
  2370. snd_pcm_capture_hw_avail(runtime));
  2371. snd_pcm_stream_unlock_irq(substream);
  2372. return ret;
  2373. }
  2374. static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
  2375. snd_pcm_uframes_t frames)
  2376. {
  2377. struct snd_pcm_runtime *runtime = substream->runtime;
  2378. snd_pcm_sframes_t ret;
  2379. if (frames == 0)
  2380. return 0;
  2381. snd_pcm_stream_lock_irq(substream);
  2382. ret = do_pcm_hwsync(substream);
  2383. if (!ret)
  2384. ret = forward_appl_ptr(substream, frames,
  2385. snd_pcm_playback_avail(runtime));
  2386. snd_pcm_stream_unlock_irq(substream);
  2387. return ret;
  2388. }
  2389. static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
  2390. snd_pcm_uframes_t frames)
  2391. {
  2392. struct snd_pcm_runtime *runtime = substream->runtime;
  2393. snd_pcm_sframes_t ret;
  2394. if (frames == 0)
  2395. return 0;
  2396. snd_pcm_stream_lock_irq(substream);
  2397. ret = do_pcm_hwsync(substream);
  2398. if (!ret)
  2399. ret = forward_appl_ptr(substream, frames,
  2400. snd_pcm_capture_avail(runtime));
  2401. snd_pcm_stream_unlock_irq(substream);
  2402. return ret;
  2403. }
  2404. static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
  2405. {
  2406. int err;
  2407. snd_pcm_stream_lock_irq(substream);
  2408. err = do_pcm_hwsync(substream);
  2409. snd_pcm_stream_unlock_irq(substream);
  2410. return err;
  2411. }
  2412. static snd_pcm_sframes_t snd_pcm_delay(struct snd_pcm_substream *substream)
  2413. {
  2414. struct snd_pcm_runtime *runtime = substream->runtime;
  2415. int err;
  2416. snd_pcm_sframes_t n = 0;
  2417. snd_pcm_stream_lock_irq(substream);
  2418. err = do_pcm_hwsync(substream);
  2419. if (!err) {
  2420. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  2421. n = snd_pcm_playback_hw_avail(runtime);
  2422. else
  2423. n = snd_pcm_capture_avail(runtime);
  2424. n += runtime->delay;
  2425. }
  2426. snd_pcm_stream_unlock_irq(substream);
  2427. return err < 0 ? err : n;
  2428. }
  2429. static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
  2430. struct snd_pcm_sync_ptr __user *_sync_ptr)
  2431. {
  2432. struct snd_pcm_runtime *runtime = substream->runtime;
  2433. struct snd_pcm_sync_ptr sync_ptr;
  2434. volatile struct snd_pcm_mmap_status *status;
  2435. volatile struct snd_pcm_mmap_control *control;
  2436. int err;
  2437. memset(&sync_ptr, 0, sizeof(sync_ptr));
  2438. if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
  2439. return -EFAULT;
  2440. if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
  2441. return -EFAULT;
  2442. status = runtime->status;
  2443. control = runtime->control;
  2444. if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
  2445. err = snd_pcm_hwsync(substream);
  2446. if (err < 0)
  2447. return err;
  2448. }
  2449. snd_pcm_stream_lock_irq(substream);
  2450. if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL)) {
  2451. err = pcm_lib_apply_appl_ptr(substream,
  2452. sync_ptr.c.control.appl_ptr);
  2453. if (err < 0) {
  2454. snd_pcm_stream_unlock_irq(substream);
  2455. return err;
  2456. }
  2457. } else {
  2458. sync_ptr.c.control.appl_ptr = control->appl_ptr;
  2459. }
  2460. if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
  2461. control->avail_min = sync_ptr.c.control.avail_min;
  2462. else
  2463. sync_ptr.c.control.avail_min = control->avail_min;
  2464. sync_ptr.s.status.state = status->state;
  2465. sync_ptr.s.status.hw_ptr = status->hw_ptr;
  2466. sync_ptr.s.status.tstamp = status->tstamp;
  2467. sync_ptr.s.status.suspended_state = status->suspended_state;
  2468. snd_pcm_stream_unlock_irq(substream);
  2469. if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
  2470. return -EFAULT;
  2471. return 0;
  2472. }
  2473. static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
  2474. {
  2475. struct snd_pcm_runtime *runtime = substream->runtime;
  2476. int arg;
  2477. if (get_user(arg, _arg))
  2478. return -EFAULT;
  2479. if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
  2480. return -EINVAL;
  2481. runtime->tstamp_type = arg;
  2482. return 0;
  2483. }
  2484. static int snd_pcm_xferi_frames_ioctl(struct snd_pcm_substream *substream,
  2485. struct snd_xferi __user *_xferi)
  2486. {
  2487. struct snd_xferi xferi;
  2488. struct snd_pcm_runtime *runtime = substream->runtime;
  2489. snd_pcm_sframes_t result;
  2490. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2491. return -EBADFD;
  2492. if (put_user(0, &_xferi->result))
  2493. return -EFAULT;
  2494. if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
  2495. return -EFAULT;
  2496. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  2497. result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
  2498. else
  2499. result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
  2500. __put_user(result, &_xferi->result);
  2501. return result < 0 ? result : 0;
  2502. }
  2503. static int snd_pcm_xfern_frames_ioctl(struct snd_pcm_substream *substream,
  2504. struct snd_xfern __user *_xfern)
  2505. {
  2506. struct snd_xfern xfern;
  2507. struct snd_pcm_runtime *runtime = substream->runtime;
  2508. void *bufs;
  2509. snd_pcm_sframes_t result;
  2510. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2511. return -EBADFD;
  2512. if (runtime->channels > 128)
  2513. return -EINVAL;
  2514. if (put_user(0, &_xfern->result))
  2515. return -EFAULT;
  2516. if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
  2517. return -EFAULT;
  2518. bufs = memdup_user(xfern.bufs, sizeof(void *) * runtime->channels);
  2519. if (IS_ERR(bufs))
  2520. return PTR_ERR(bufs);
  2521. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  2522. result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
  2523. else
  2524. result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
  2525. kfree(bufs);
  2526. __put_user(result, &_xfern->result);
  2527. return result < 0 ? result : 0;
  2528. }
  2529. static int snd_pcm_rewind_ioctl(struct snd_pcm_substream *substream,
  2530. snd_pcm_uframes_t __user *_frames)
  2531. {
  2532. snd_pcm_uframes_t frames;
  2533. snd_pcm_sframes_t result;
  2534. if (get_user(frames, _frames))
  2535. return -EFAULT;
  2536. if (put_user(0, _frames))
  2537. return -EFAULT;
  2538. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  2539. result = snd_pcm_playback_rewind(substream, frames);
  2540. else
  2541. result = snd_pcm_capture_rewind(substream, frames);
  2542. __put_user(result, _frames);
  2543. return result < 0 ? result : 0;
  2544. }
  2545. static int snd_pcm_forward_ioctl(struct snd_pcm_substream *substream,
  2546. snd_pcm_uframes_t __user *_frames)
  2547. {
  2548. snd_pcm_uframes_t frames;
  2549. snd_pcm_sframes_t result;
  2550. if (get_user(frames, _frames))
  2551. return -EFAULT;
  2552. if (put_user(0, _frames))
  2553. return -EFAULT;
  2554. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  2555. result = snd_pcm_playback_forward(substream, frames);
  2556. else
  2557. result = snd_pcm_capture_forward(substream, frames);
  2558. __put_user(result, _frames);
  2559. return result < 0 ? result : 0;
  2560. }
  2561. static int snd_pcm_common_ioctl(struct file *file,
  2562. struct snd_pcm_substream *substream,
  2563. unsigned int cmd, void __user *arg)
  2564. {
  2565. struct snd_pcm_file *pcm_file = file->private_data;
  2566. int res;
  2567. if (PCM_RUNTIME_CHECK(substream))
  2568. return -ENXIO;
  2569. res = snd_power_wait(substream->pcm->card, SNDRV_CTL_POWER_D0);
  2570. if (res < 0)
  2571. return res;
  2572. switch (cmd) {
  2573. case SNDRV_PCM_IOCTL_PVERSION:
  2574. return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
  2575. case SNDRV_PCM_IOCTL_INFO:
  2576. return snd_pcm_info_user(substream, arg);
  2577. case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
  2578. return 0;
  2579. case SNDRV_PCM_IOCTL_TTSTAMP:
  2580. return snd_pcm_tstamp(substream, arg);
  2581. case SNDRV_PCM_IOCTL_USER_PVERSION:
  2582. if (get_user(pcm_file->user_pversion,
  2583. (unsigned int __user *)arg))
  2584. return -EFAULT;
  2585. return 0;
  2586. case SNDRV_PCM_IOCTL_HW_REFINE:
  2587. return snd_pcm_hw_refine_user(substream, arg);
  2588. case SNDRV_PCM_IOCTL_HW_PARAMS:
  2589. return snd_pcm_hw_params_user(substream, arg);
  2590. case SNDRV_PCM_IOCTL_HW_FREE:
  2591. return snd_pcm_hw_free(substream);
  2592. case SNDRV_PCM_IOCTL_SW_PARAMS:
  2593. return snd_pcm_sw_params_user(substream, arg);
  2594. case SNDRV_PCM_IOCTL_STATUS:
  2595. return snd_pcm_status_user(substream, arg, false);
  2596. case SNDRV_PCM_IOCTL_STATUS_EXT:
  2597. return snd_pcm_status_user(substream, arg, true);
  2598. case SNDRV_PCM_IOCTL_CHANNEL_INFO:
  2599. return snd_pcm_channel_info_user(substream, arg);
  2600. case SNDRV_PCM_IOCTL_PREPARE:
  2601. return snd_pcm_prepare(substream, file);
  2602. case SNDRV_PCM_IOCTL_RESET:
  2603. return snd_pcm_reset(substream);
  2604. case SNDRV_PCM_IOCTL_START:
  2605. return snd_pcm_start_lock_irq(substream);
  2606. case SNDRV_PCM_IOCTL_LINK:
  2607. return snd_pcm_link(substream, (int)(unsigned long) arg);
  2608. case SNDRV_PCM_IOCTL_UNLINK:
  2609. return snd_pcm_unlink(substream);
  2610. case SNDRV_PCM_IOCTL_RESUME:
  2611. return snd_pcm_resume(substream);
  2612. case SNDRV_PCM_IOCTL_XRUN:
  2613. return snd_pcm_xrun(substream);
  2614. case SNDRV_PCM_IOCTL_HWSYNC:
  2615. return snd_pcm_hwsync(substream);
  2616. case SNDRV_PCM_IOCTL_DELAY:
  2617. {
  2618. snd_pcm_sframes_t delay = snd_pcm_delay(substream);
  2619. snd_pcm_sframes_t __user *res = arg;
  2620. if (delay < 0)
  2621. return delay;
  2622. if (put_user(delay, res))
  2623. return -EFAULT;
  2624. return 0;
  2625. }
  2626. case SNDRV_PCM_IOCTL_SYNC_PTR:
  2627. return snd_pcm_sync_ptr(substream, arg);
  2628. #ifdef CONFIG_SND_SUPPORT_OLD_API
  2629. case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
  2630. return snd_pcm_hw_refine_old_user(substream, arg);
  2631. case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
  2632. return snd_pcm_hw_params_old_user(substream, arg);
  2633. #endif
  2634. case SNDRV_PCM_IOCTL_DRAIN:
  2635. return snd_pcm_drain(substream, file);
  2636. case SNDRV_PCM_IOCTL_DROP:
  2637. return snd_pcm_drop(substream);
  2638. case SNDRV_PCM_IOCTL_PAUSE:
  2639. return snd_pcm_action_lock_irq(&snd_pcm_action_pause,
  2640. substream,
  2641. (int)(unsigned long)arg);
  2642. case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
  2643. case SNDRV_PCM_IOCTL_READI_FRAMES:
  2644. return snd_pcm_xferi_frames_ioctl(substream, arg);
  2645. case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
  2646. case SNDRV_PCM_IOCTL_READN_FRAMES:
  2647. return snd_pcm_xfern_frames_ioctl(substream, arg);
  2648. case SNDRV_PCM_IOCTL_REWIND:
  2649. return snd_pcm_rewind_ioctl(substream, arg);
  2650. case SNDRV_PCM_IOCTL_FORWARD:
  2651. return snd_pcm_forward_ioctl(substream, arg);
  2652. }
  2653. pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
  2654. return -ENOTTY;
  2655. }
  2656. static long snd_pcm_ioctl(struct file *file, unsigned int cmd,
  2657. unsigned long arg)
  2658. {
  2659. struct snd_pcm_file *pcm_file;
  2660. pcm_file = file->private_data;
  2661. if (((cmd >> 8) & 0xff) != 'A')
  2662. return -ENOTTY;
  2663. return snd_pcm_common_ioctl(file, pcm_file->substream, cmd,
  2664. (void __user *)arg);
  2665. }
  2666. /**
  2667. * snd_pcm_kernel_ioctl - Execute PCM ioctl in the kernel-space
  2668. * @substream: PCM substream
  2669. * @cmd: IOCTL cmd
  2670. * @arg: IOCTL argument
  2671. *
  2672. * The function is provided primarily for OSS layer and USB gadget drivers,
  2673. * and it allows only the limited set of ioctls (hw_params, sw_params,
  2674. * prepare, start, drain, drop, forward).
  2675. */
  2676. int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
  2677. unsigned int cmd, void *arg)
  2678. {
  2679. snd_pcm_uframes_t *frames = arg;
  2680. snd_pcm_sframes_t result;
  2681. switch (cmd) {
  2682. case SNDRV_PCM_IOCTL_FORWARD:
  2683. {
  2684. /* provided only for OSS; capture-only and no value returned */
  2685. if (substream->stream != SNDRV_PCM_STREAM_CAPTURE)
  2686. return -EINVAL;
  2687. result = snd_pcm_capture_forward(substream, *frames);
  2688. return result < 0 ? result : 0;
  2689. }
  2690. case SNDRV_PCM_IOCTL_HW_PARAMS:
  2691. return snd_pcm_hw_params(substream, arg);
  2692. case SNDRV_PCM_IOCTL_SW_PARAMS:
  2693. return snd_pcm_sw_params(substream, arg);
  2694. case SNDRV_PCM_IOCTL_PREPARE:
  2695. return snd_pcm_prepare(substream, NULL);
  2696. case SNDRV_PCM_IOCTL_START:
  2697. return snd_pcm_start_lock_irq(substream);
  2698. case SNDRV_PCM_IOCTL_DRAIN:
  2699. return snd_pcm_drain(substream, NULL);
  2700. case SNDRV_PCM_IOCTL_DROP:
  2701. return snd_pcm_drop(substream);
  2702. case SNDRV_PCM_IOCTL_DELAY:
  2703. {
  2704. result = snd_pcm_delay(substream);
  2705. if (result < 0)
  2706. return result;
  2707. *frames = result;
  2708. return 0;
  2709. }
  2710. default:
  2711. return -EINVAL;
  2712. }
  2713. }
  2714. EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
  2715. static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
  2716. loff_t * offset)
  2717. {
  2718. struct snd_pcm_file *pcm_file;
  2719. struct snd_pcm_substream *substream;
  2720. struct snd_pcm_runtime *runtime;
  2721. snd_pcm_sframes_t result;
  2722. pcm_file = file->private_data;
  2723. substream = pcm_file->substream;
  2724. if (PCM_RUNTIME_CHECK(substream))
  2725. return -ENXIO;
  2726. runtime = substream->runtime;
  2727. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2728. return -EBADFD;
  2729. if (!frame_aligned(runtime, count))
  2730. return -EINVAL;
  2731. count = bytes_to_frames(runtime, count);
  2732. result = snd_pcm_lib_read(substream, buf, count);
  2733. if (result > 0)
  2734. result = frames_to_bytes(runtime, result);
  2735. return result;
  2736. }
  2737. static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
  2738. size_t count, loff_t * offset)
  2739. {
  2740. struct snd_pcm_file *pcm_file;
  2741. struct snd_pcm_substream *substream;
  2742. struct snd_pcm_runtime *runtime;
  2743. snd_pcm_sframes_t result;
  2744. pcm_file = file->private_data;
  2745. substream = pcm_file->substream;
  2746. if (PCM_RUNTIME_CHECK(substream))
  2747. return -ENXIO;
  2748. runtime = substream->runtime;
  2749. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2750. return -EBADFD;
  2751. if (!frame_aligned(runtime, count))
  2752. return -EINVAL;
  2753. count = bytes_to_frames(runtime, count);
  2754. result = snd_pcm_lib_write(substream, buf, count);
  2755. if (result > 0)
  2756. result = frames_to_bytes(runtime, result);
  2757. return result;
  2758. }
  2759. static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
  2760. {
  2761. struct snd_pcm_file *pcm_file;
  2762. struct snd_pcm_substream *substream;
  2763. struct snd_pcm_runtime *runtime;
  2764. snd_pcm_sframes_t result;
  2765. unsigned long i;
  2766. void __user **bufs;
  2767. snd_pcm_uframes_t frames;
  2768. pcm_file = iocb->ki_filp->private_data;
  2769. substream = pcm_file->substream;
  2770. if (PCM_RUNTIME_CHECK(substream))
  2771. return -ENXIO;
  2772. runtime = substream->runtime;
  2773. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2774. return -EBADFD;
  2775. if (!iter_is_iovec(to))
  2776. return -EINVAL;
  2777. if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
  2778. return -EINVAL;
  2779. if (!frame_aligned(runtime, to->iov->iov_len))
  2780. return -EINVAL;
  2781. frames = bytes_to_samples(runtime, to->iov->iov_len);
  2782. bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
  2783. if (bufs == NULL)
  2784. return -ENOMEM;
  2785. for (i = 0; i < to->nr_segs; ++i)
  2786. bufs[i] = to->iov[i].iov_base;
  2787. result = snd_pcm_lib_readv(substream, bufs, frames);
  2788. if (result > 0)
  2789. result = frames_to_bytes(runtime, result);
  2790. kfree(bufs);
  2791. return result;
  2792. }
  2793. static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
  2794. {
  2795. struct snd_pcm_file *pcm_file;
  2796. struct snd_pcm_substream *substream;
  2797. struct snd_pcm_runtime *runtime;
  2798. snd_pcm_sframes_t result;
  2799. unsigned long i;
  2800. void __user **bufs;
  2801. snd_pcm_uframes_t frames;
  2802. pcm_file = iocb->ki_filp->private_data;
  2803. substream = pcm_file->substream;
  2804. if (PCM_RUNTIME_CHECK(substream))
  2805. return -ENXIO;
  2806. runtime = substream->runtime;
  2807. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2808. return -EBADFD;
  2809. if (!iter_is_iovec(from))
  2810. return -EINVAL;
  2811. if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
  2812. !frame_aligned(runtime, from->iov->iov_len))
  2813. return -EINVAL;
  2814. frames = bytes_to_samples(runtime, from->iov->iov_len);
  2815. bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
  2816. if (bufs == NULL)
  2817. return -ENOMEM;
  2818. for (i = 0; i < from->nr_segs; ++i)
  2819. bufs[i] = from->iov[i].iov_base;
  2820. result = snd_pcm_lib_writev(substream, bufs, frames);
  2821. if (result > 0)
  2822. result = frames_to_bytes(runtime, result);
  2823. kfree(bufs);
  2824. return result;
  2825. }
  2826. static __poll_t snd_pcm_playback_poll(struct file *file, poll_table * wait)
  2827. {
  2828. struct snd_pcm_file *pcm_file;
  2829. struct snd_pcm_substream *substream;
  2830. struct snd_pcm_runtime *runtime;
  2831. __poll_t mask;
  2832. snd_pcm_uframes_t avail;
  2833. pcm_file = file->private_data;
  2834. substream = pcm_file->substream;
  2835. if (PCM_RUNTIME_CHECK(substream))
  2836. return EPOLLOUT | EPOLLWRNORM | EPOLLERR;
  2837. runtime = substream->runtime;
  2838. poll_wait(file, &runtime->sleep, wait);
  2839. snd_pcm_stream_lock_irq(substream);
  2840. avail = snd_pcm_playback_avail(runtime);
  2841. switch (runtime->status->state) {
  2842. case SNDRV_PCM_STATE_RUNNING:
  2843. case SNDRV_PCM_STATE_PREPARED:
  2844. case SNDRV_PCM_STATE_PAUSED:
  2845. if (avail >= runtime->control->avail_min) {
  2846. mask = EPOLLOUT | EPOLLWRNORM;
  2847. break;
  2848. }
  2849. /* Fall through */
  2850. case SNDRV_PCM_STATE_DRAINING:
  2851. mask = 0;
  2852. break;
  2853. default:
  2854. mask = EPOLLOUT | EPOLLWRNORM | EPOLLERR;
  2855. break;
  2856. }
  2857. snd_pcm_stream_unlock_irq(substream);
  2858. return mask;
  2859. }
  2860. static __poll_t snd_pcm_capture_poll(struct file *file, poll_table * wait)
  2861. {
  2862. struct snd_pcm_file *pcm_file;
  2863. struct snd_pcm_substream *substream;
  2864. struct snd_pcm_runtime *runtime;
  2865. __poll_t mask;
  2866. snd_pcm_uframes_t avail;
  2867. pcm_file = file->private_data;
  2868. substream = pcm_file->substream;
  2869. if (PCM_RUNTIME_CHECK(substream))
  2870. return EPOLLIN | EPOLLRDNORM | EPOLLERR;
  2871. runtime = substream->runtime;
  2872. poll_wait(file, &runtime->sleep, wait);
  2873. snd_pcm_stream_lock_irq(substream);
  2874. avail = snd_pcm_capture_avail(runtime);
  2875. switch (runtime->status->state) {
  2876. case SNDRV_PCM_STATE_RUNNING:
  2877. case SNDRV_PCM_STATE_PREPARED:
  2878. case SNDRV_PCM_STATE_PAUSED:
  2879. if (avail >= runtime->control->avail_min) {
  2880. mask = EPOLLIN | EPOLLRDNORM;
  2881. break;
  2882. }
  2883. mask = 0;
  2884. break;
  2885. case SNDRV_PCM_STATE_DRAINING:
  2886. if (avail > 0) {
  2887. mask = EPOLLIN | EPOLLRDNORM;
  2888. break;
  2889. }
  2890. /* Fall through */
  2891. default:
  2892. mask = EPOLLIN | EPOLLRDNORM | EPOLLERR;
  2893. break;
  2894. }
  2895. snd_pcm_stream_unlock_irq(substream);
  2896. return mask;
  2897. }
  2898. /*
  2899. * mmap support
  2900. */
  2901. /*
  2902. * Only on coherent architectures, we can mmap the status and the control records
  2903. * for effcient data transfer. On others, we have to use HWSYNC ioctl...
  2904. */
  2905. #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
  2906. /*
  2907. * mmap status record
  2908. */
  2909. static int snd_pcm_mmap_status_fault(struct vm_fault *vmf)
  2910. {
  2911. struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
  2912. struct snd_pcm_runtime *runtime;
  2913. if (substream == NULL)
  2914. return VM_FAULT_SIGBUS;
  2915. runtime = substream->runtime;
  2916. vmf->page = virt_to_page(runtime->status);
  2917. get_page(vmf->page);
  2918. return 0;
  2919. }
  2920. static const struct vm_operations_struct snd_pcm_vm_ops_status =
  2921. {
  2922. .fault = snd_pcm_mmap_status_fault,
  2923. };
  2924. static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
  2925. struct vm_area_struct *area)
  2926. {
  2927. long size;
  2928. if (!(area->vm_flags & VM_READ))
  2929. return -EINVAL;
  2930. size = area->vm_end - area->vm_start;
  2931. if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
  2932. return -EINVAL;
  2933. area->vm_ops = &snd_pcm_vm_ops_status;
  2934. area->vm_private_data = substream;
  2935. area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  2936. return 0;
  2937. }
  2938. /*
  2939. * mmap control record
  2940. */
  2941. static int snd_pcm_mmap_control_fault(struct vm_fault *vmf)
  2942. {
  2943. struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
  2944. struct snd_pcm_runtime *runtime;
  2945. if (substream == NULL)
  2946. return VM_FAULT_SIGBUS;
  2947. runtime = substream->runtime;
  2948. vmf->page = virt_to_page(runtime->control);
  2949. get_page(vmf->page);
  2950. return 0;
  2951. }
  2952. static const struct vm_operations_struct snd_pcm_vm_ops_control =
  2953. {
  2954. .fault = snd_pcm_mmap_control_fault,
  2955. };
  2956. static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
  2957. struct vm_area_struct *area)
  2958. {
  2959. long size;
  2960. if (!(area->vm_flags & VM_READ))
  2961. return -EINVAL;
  2962. size = area->vm_end - area->vm_start;
  2963. if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
  2964. return -EINVAL;
  2965. area->vm_ops = &snd_pcm_vm_ops_control;
  2966. area->vm_private_data = substream;
  2967. area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  2968. return 0;
  2969. }
  2970. static bool pcm_status_mmap_allowed(struct snd_pcm_file *pcm_file)
  2971. {
  2972. if (pcm_file->no_compat_mmap)
  2973. return false;
  2974. /* See pcm_control_mmap_allowed() below.
  2975. * Since older alsa-lib requires both status and control mmaps to be
  2976. * coupled, we have to disable the status mmap for old alsa-lib, too.
  2977. */
  2978. if (pcm_file->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 14) &&
  2979. (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR))
  2980. return false;
  2981. return true;
  2982. }
  2983. static bool pcm_control_mmap_allowed(struct snd_pcm_file *pcm_file)
  2984. {
  2985. if (pcm_file->no_compat_mmap)
  2986. return false;
  2987. /* Disallow the control mmap when SYNC_APPLPTR flag is set;
  2988. * it enforces the user-space to fall back to snd_pcm_sync_ptr(),
  2989. * thus it effectively assures the manual update of appl_ptr.
  2990. */
  2991. if (pcm_file->substream->runtime->hw.info & SNDRV_PCM_INFO_SYNC_APPLPTR)
  2992. return false;
  2993. return true;
  2994. }
  2995. #else /* ! coherent mmap */
  2996. /*
  2997. * don't support mmap for status and control records.
  2998. */
  2999. #define pcm_status_mmap_allowed(pcm_file) false
  3000. #define pcm_control_mmap_allowed(pcm_file) false
  3001. static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
  3002. struct vm_area_struct *area)
  3003. {
  3004. return -ENXIO;
  3005. }
  3006. static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
  3007. struct vm_area_struct *area)
  3008. {
  3009. return -ENXIO;
  3010. }
  3011. #endif /* coherent mmap */
  3012. static inline struct page *
  3013. snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
  3014. {
  3015. void *vaddr = substream->runtime->dma_area + ofs;
  3016. return virt_to_page(vaddr);
  3017. }
  3018. /*
  3019. * fault callback for mmapping a RAM page
  3020. */
  3021. static int snd_pcm_mmap_data_fault(struct vm_fault *vmf)
  3022. {
  3023. struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
  3024. struct snd_pcm_runtime *runtime;
  3025. unsigned long offset;
  3026. struct page * page;
  3027. size_t dma_bytes;
  3028. if (substream == NULL)
  3029. return VM_FAULT_SIGBUS;
  3030. runtime = substream->runtime;
  3031. offset = vmf->pgoff << PAGE_SHIFT;
  3032. dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
  3033. if (offset > dma_bytes - PAGE_SIZE)
  3034. return VM_FAULT_SIGBUS;
  3035. if (substream->ops->page)
  3036. page = substream->ops->page(substream, offset);
  3037. else
  3038. page = snd_pcm_default_page_ops(substream, offset);
  3039. if (!page)
  3040. return VM_FAULT_SIGBUS;
  3041. get_page(page);
  3042. vmf->page = page;
  3043. return 0;
  3044. }
  3045. static const struct vm_operations_struct snd_pcm_vm_ops_data = {
  3046. .open = snd_pcm_mmap_data_open,
  3047. .close = snd_pcm_mmap_data_close,
  3048. };
  3049. static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
  3050. .open = snd_pcm_mmap_data_open,
  3051. .close = snd_pcm_mmap_data_close,
  3052. .fault = snd_pcm_mmap_data_fault,
  3053. };
  3054. /*
  3055. * mmap the DMA buffer on RAM
  3056. */
  3057. /**
  3058. * snd_pcm_lib_default_mmap - Default PCM data mmap function
  3059. * @substream: PCM substream
  3060. * @area: VMA
  3061. *
  3062. * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
  3063. * this function is invoked implicitly.
  3064. */
  3065. int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
  3066. struct vm_area_struct *area)
  3067. {
  3068. area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  3069. #ifdef CONFIG_GENERIC_ALLOCATOR
  3070. if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
  3071. area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
  3072. return remap_pfn_range(area, area->vm_start,
  3073. substream->dma_buffer.addr >> PAGE_SHIFT,
  3074. area->vm_end - area->vm_start, area->vm_page_prot);
  3075. }
  3076. #endif /* CONFIG_GENERIC_ALLOCATOR */
  3077. #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
  3078. if (IS_ENABLED(CONFIG_HAS_DMA) && !substream->ops->page &&
  3079. substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
  3080. return dma_mmap_coherent(substream->dma_buffer.dev.dev,
  3081. area,
  3082. substream->runtime->dma_area,
  3083. substream->runtime->dma_addr,
  3084. substream->runtime->dma_bytes);
  3085. #endif /* CONFIG_X86 */
  3086. /* mmap with fault handler */
  3087. area->vm_ops = &snd_pcm_vm_ops_data_fault;
  3088. return 0;
  3089. }
  3090. EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
  3091. /*
  3092. * mmap the DMA buffer on I/O memory area
  3093. */
  3094. #if SNDRV_PCM_INFO_MMAP_IOMEM
  3095. /**
  3096. * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
  3097. * @substream: PCM substream
  3098. * @area: VMA
  3099. *
  3100. * When your hardware uses the iomapped pages as the hardware buffer and
  3101. * wants to mmap it, pass this function as mmap pcm_ops. Note that this
  3102. * is supposed to work only on limited architectures.
  3103. */
  3104. int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
  3105. struct vm_area_struct *area)
  3106. {
  3107. struct snd_pcm_runtime *runtime = substream->runtime;
  3108. area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
  3109. return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
  3110. }
  3111. EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
  3112. #endif /* SNDRV_PCM_INFO_MMAP */
  3113. /*
  3114. * mmap DMA buffer
  3115. */
  3116. int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
  3117. struct vm_area_struct *area)
  3118. {
  3119. struct snd_pcm_runtime *runtime;
  3120. long size;
  3121. unsigned long offset;
  3122. size_t dma_bytes;
  3123. int err;
  3124. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  3125. if (!(area->vm_flags & (VM_WRITE|VM_READ)))
  3126. return -EINVAL;
  3127. } else {
  3128. if (!(area->vm_flags & VM_READ))
  3129. return -EINVAL;
  3130. }
  3131. runtime = substream->runtime;
  3132. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  3133. return -EBADFD;
  3134. if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
  3135. return -ENXIO;
  3136. if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
  3137. runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  3138. return -EINVAL;
  3139. size = area->vm_end - area->vm_start;
  3140. offset = area->vm_pgoff << PAGE_SHIFT;
  3141. dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
  3142. if ((size_t)size > dma_bytes)
  3143. return -EINVAL;
  3144. if (offset > dma_bytes - size)
  3145. return -EINVAL;
  3146. area->vm_ops = &snd_pcm_vm_ops_data;
  3147. area->vm_private_data = substream;
  3148. if (substream->ops->mmap)
  3149. err = substream->ops->mmap(substream, area);
  3150. else
  3151. err = snd_pcm_lib_default_mmap(substream, area);
  3152. if (!err)
  3153. atomic_inc(&substream->mmap_count);
  3154. return err;
  3155. }
  3156. EXPORT_SYMBOL(snd_pcm_mmap_data);
  3157. static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
  3158. {
  3159. struct snd_pcm_file * pcm_file;
  3160. struct snd_pcm_substream *substream;
  3161. unsigned long offset;
  3162. pcm_file = file->private_data;
  3163. substream = pcm_file->substream;
  3164. if (PCM_RUNTIME_CHECK(substream))
  3165. return -ENXIO;
  3166. offset = area->vm_pgoff << PAGE_SHIFT;
  3167. switch (offset) {
  3168. case SNDRV_PCM_MMAP_OFFSET_STATUS:
  3169. if (!pcm_status_mmap_allowed(pcm_file))
  3170. return -ENXIO;
  3171. return snd_pcm_mmap_status(substream, file, area);
  3172. case SNDRV_PCM_MMAP_OFFSET_CONTROL:
  3173. if (!pcm_control_mmap_allowed(pcm_file))
  3174. return -ENXIO;
  3175. return snd_pcm_mmap_control(substream, file, area);
  3176. default:
  3177. return snd_pcm_mmap_data(substream, file, area);
  3178. }
  3179. return 0;
  3180. }
  3181. static int snd_pcm_fasync(int fd, struct file * file, int on)
  3182. {
  3183. struct snd_pcm_file * pcm_file;
  3184. struct snd_pcm_substream *substream;
  3185. struct snd_pcm_runtime *runtime;
  3186. pcm_file = file->private_data;
  3187. substream = pcm_file->substream;
  3188. if (PCM_RUNTIME_CHECK(substream))
  3189. return -ENXIO;
  3190. runtime = substream->runtime;
  3191. return fasync_helper(fd, file, on, &runtime->fasync);
  3192. }
  3193. /*
  3194. * ioctl32 compat
  3195. */
  3196. #ifdef CONFIG_COMPAT
  3197. #include "pcm_compat.c"
  3198. #else
  3199. #define snd_pcm_ioctl_compat NULL
  3200. #endif
  3201. /*
  3202. * To be removed helpers to keep binary compatibility
  3203. */
  3204. #ifdef CONFIG_SND_SUPPORT_OLD_API
  3205. #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
  3206. #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
  3207. static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
  3208. struct snd_pcm_hw_params_old *oparams)
  3209. {
  3210. unsigned int i;
  3211. memset(params, 0, sizeof(*params));
  3212. params->flags = oparams->flags;
  3213. for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
  3214. params->masks[i].bits[0] = oparams->masks[i];
  3215. memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
  3216. params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
  3217. params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
  3218. params->info = oparams->info;
  3219. params->msbits = oparams->msbits;
  3220. params->rate_num = oparams->rate_num;
  3221. params->rate_den = oparams->rate_den;
  3222. params->fifo_size = oparams->fifo_size;
  3223. }
  3224. static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
  3225. struct snd_pcm_hw_params *params)
  3226. {
  3227. unsigned int i;
  3228. memset(oparams, 0, sizeof(*oparams));
  3229. oparams->flags = params->flags;
  3230. for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
  3231. oparams->masks[i] = params->masks[i].bits[0];
  3232. memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
  3233. oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
  3234. oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
  3235. oparams->info = params->info;
  3236. oparams->msbits = params->msbits;
  3237. oparams->rate_num = params->rate_num;
  3238. oparams->rate_den = params->rate_den;
  3239. oparams->fifo_size = params->fifo_size;
  3240. }
  3241. static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
  3242. struct snd_pcm_hw_params_old __user * _oparams)
  3243. {
  3244. struct snd_pcm_hw_params *params;
  3245. struct snd_pcm_hw_params_old *oparams = NULL;
  3246. int err;
  3247. params = kmalloc(sizeof(*params), GFP_KERNEL);
  3248. if (!params)
  3249. return -ENOMEM;
  3250. oparams = memdup_user(_oparams, sizeof(*oparams));
  3251. if (IS_ERR(oparams)) {
  3252. err = PTR_ERR(oparams);
  3253. goto out;
  3254. }
  3255. snd_pcm_hw_convert_from_old_params(params, oparams);
  3256. err = snd_pcm_hw_refine(substream, params);
  3257. if (err < 0)
  3258. goto out_old;
  3259. err = fixup_unreferenced_params(substream, params);
  3260. if (err < 0)
  3261. goto out_old;
  3262. snd_pcm_hw_convert_to_old_params(oparams, params);
  3263. if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
  3264. err = -EFAULT;
  3265. out_old:
  3266. kfree(oparams);
  3267. out:
  3268. kfree(params);
  3269. return err;
  3270. }
  3271. static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
  3272. struct snd_pcm_hw_params_old __user * _oparams)
  3273. {
  3274. struct snd_pcm_hw_params *params;
  3275. struct snd_pcm_hw_params_old *oparams = NULL;
  3276. int err;
  3277. params = kmalloc(sizeof(*params), GFP_KERNEL);
  3278. if (!params)
  3279. return -ENOMEM;
  3280. oparams = memdup_user(_oparams, sizeof(*oparams));
  3281. if (IS_ERR(oparams)) {
  3282. err = PTR_ERR(oparams);
  3283. goto out;
  3284. }
  3285. snd_pcm_hw_convert_from_old_params(params, oparams);
  3286. err = snd_pcm_hw_params(substream, params);
  3287. if (err < 0)
  3288. goto out_old;
  3289. snd_pcm_hw_convert_to_old_params(oparams, params);
  3290. if (copy_to_user(_oparams, oparams, sizeof(*oparams)))
  3291. err = -EFAULT;
  3292. out_old:
  3293. kfree(oparams);
  3294. out:
  3295. kfree(params);
  3296. return err;
  3297. }
  3298. #endif /* CONFIG_SND_SUPPORT_OLD_API */
  3299. #ifndef CONFIG_MMU
  3300. static unsigned long snd_pcm_get_unmapped_area(struct file *file,
  3301. unsigned long addr,
  3302. unsigned long len,
  3303. unsigned long pgoff,
  3304. unsigned long flags)
  3305. {
  3306. struct snd_pcm_file *pcm_file = file->private_data;
  3307. struct snd_pcm_substream *substream = pcm_file->substream;
  3308. struct snd_pcm_runtime *runtime = substream->runtime;
  3309. unsigned long offset = pgoff << PAGE_SHIFT;
  3310. switch (offset) {
  3311. case SNDRV_PCM_MMAP_OFFSET_STATUS:
  3312. return (unsigned long)runtime->status;
  3313. case SNDRV_PCM_MMAP_OFFSET_CONTROL:
  3314. return (unsigned long)runtime->control;
  3315. default:
  3316. return (unsigned long)runtime->dma_area + offset;
  3317. }
  3318. }
  3319. #else
  3320. # define snd_pcm_get_unmapped_area NULL
  3321. #endif
  3322. /*
  3323. * Register section
  3324. */
  3325. const struct file_operations snd_pcm_f_ops[2] = {
  3326. {
  3327. .owner = THIS_MODULE,
  3328. .write = snd_pcm_write,
  3329. .write_iter = snd_pcm_writev,
  3330. .open = snd_pcm_playback_open,
  3331. .release = snd_pcm_release,
  3332. .llseek = no_llseek,
  3333. .poll = snd_pcm_playback_poll,
  3334. .unlocked_ioctl = snd_pcm_ioctl,
  3335. .compat_ioctl = snd_pcm_ioctl_compat,
  3336. .mmap = snd_pcm_mmap,
  3337. .fasync = snd_pcm_fasync,
  3338. .get_unmapped_area = snd_pcm_get_unmapped_area,
  3339. },
  3340. {
  3341. .owner = THIS_MODULE,
  3342. .read = snd_pcm_read,
  3343. .read_iter = snd_pcm_readv,
  3344. .open = snd_pcm_capture_open,
  3345. .release = snd_pcm_release,
  3346. .llseek = no_llseek,
  3347. .poll = snd_pcm_capture_poll,
  3348. .unlocked_ioctl = snd_pcm_ioctl,
  3349. .compat_ioctl = snd_pcm_ioctl_compat,
  3350. .mmap = snd_pcm_mmap,
  3351. .fasync = snd_pcm_fasync,
  3352. .get_unmapped_area = snd_pcm_get_unmapped_area,
  3353. }
  3354. };