pcm_native.c 91 KB

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