pcm_native.c 93 KB

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