pcm_native.c 92 KB

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