pcm_native.c 105 KB

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