pcm_native.c 94 KB

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