pcm_native.c 95 KB

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