pcm_native.c 91 KB

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