pcm_native.c 92 KB

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