pcm_native.c 115 KB

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