pcm_native.c 101 KB

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