pcm_native.c 102 KB

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