pcm_native.c 117 KB

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