pcm_native.c 98 KB

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