pcm_native.c 94 KB

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