pcm_native.c 94 KB

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