pcm_native.c 92 KB

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