pcm_native.c 91 KB

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