pcm_native.c 92 KB

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