pcm_native.c 101 KB

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