pcm_native.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366
  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. snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
  523. } else
  524. snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
  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. snd_timestamp_now(&runtime->trigger_tstamp, runtime->tstamp_timespec);
  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. *
  793. * Start all linked streams.
  794. */
  795. int snd_pcm_start(snd_pcm_substream_t *substream)
  796. {
  797. return snd_pcm_action(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
  798. }
  799. /*
  800. * stop callbacks
  801. */
  802. static int snd_pcm_pre_stop(snd_pcm_substream_t *substream, int state)
  803. {
  804. snd_pcm_runtime_t *runtime = substream->runtime;
  805. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  806. return -EBADFD;
  807. runtime->trigger_master = substream;
  808. return 0;
  809. }
  810. static int snd_pcm_do_stop(snd_pcm_substream_t *substream, int state)
  811. {
  812. if (substream->runtime->trigger_master == substream &&
  813. snd_pcm_running(substream))
  814. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
  815. return 0; /* unconditonally stop all substreams */
  816. }
  817. static void snd_pcm_post_stop(snd_pcm_substream_t *substream, int state)
  818. {
  819. snd_pcm_runtime_t *runtime = substream->runtime;
  820. if (runtime->status->state != state) {
  821. snd_pcm_trigger_tstamp(substream);
  822. if (substream->timer)
  823. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP, &runtime->trigger_tstamp);
  824. runtime->status->state = state;
  825. snd_pcm_tick_set(substream, 0);
  826. }
  827. wake_up(&runtime->sleep);
  828. }
  829. static struct action_ops snd_pcm_action_stop = {
  830. .pre_action = snd_pcm_pre_stop,
  831. .do_action = snd_pcm_do_stop,
  832. .post_action = snd_pcm_post_stop
  833. };
  834. /**
  835. * snd_pcm_stop
  836. *
  837. * Try to stop all running streams in the substream group.
  838. * The state of each stream is changed to the given value after that unconditionally.
  839. */
  840. int snd_pcm_stop(snd_pcm_substream_t *substream, int state)
  841. {
  842. return snd_pcm_action(&snd_pcm_action_stop, substream, state);
  843. }
  844. /**
  845. * snd_pcm_drain_done
  846. *
  847. * Stop the DMA only when the given stream is playback.
  848. * The state is changed to SETUP.
  849. * Unlike snd_pcm_stop(), this affects only the given stream.
  850. */
  851. int snd_pcm_drain_done(snd_pcm_substream_t *substream)
  852. {
  853. return snd_pcm_action_single(&snd_pcm_action_stop, substream, SNDRV_PCM_STATE_SETUP);
  854. }
  855. /*
  856. * pause callbacks
  857. */
  858. static int snd_pcm_pre_pause(snd_pcm_substream_t *substream, int push)
  859. {
  860. snd_pcm_runtime_t *runtime = substream->runtime;
  861. if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
  862. return -ENOSYS;
  863. if (push) {
  864. if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
  865. return -EBADFD;
  866. } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
  867. return -EBADFD;
  868. runtime->trigger_master = substream;
  869. return 0;
  870. }
  871. static int snd_pcm_do_pause(snd_pcm_substream_t *substream, int push)
  872. {
  873. if (substream->runtime->trigger_master != substream)
  874. return 0;
  875. return substream->ops->trigger(substream,
  876. push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
  877. SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
  878. }
  879. static void snd_pcm_undo_pause(snd_pcm_substream_t *substream, int push)
  880. {
  881. if (substream->runtime->trigger_master == substream)
  882. substream->ops->trigger(substream,
  883. push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
  884. SNDRV_PCM_TRIGGER_PAUSE_PUSH);
  885. }
  886. static void snd_pcm_post_pause(snd_pcm_substream_t *substream, int push)
  887. {
  888. snd_pcm_runtime_t *runtime = substream->runtime;
  889. snd_pcm_trigger_tstamp(substream);
  890. if (push) {
  891. runtime->status->state = SNDRV_PCM_STATE_PAUSED;
  892. if (substream->timer)
  893. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MPAUSE, &runtime->trigger_tstamp);
  894. snd_pcm_tick_set(substream, 0);
  895. wake_up(&runtime->sleep);
  896. } else {
  897. runtime->status->state = SNDRV_PCM_STATE_RUNNING;
  898. if (runtime->sleep_min)
  899. snd_pcm_tick_prepare(substream);
  900. if (substream->timer)
  901. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MCONTINUE, &runtime->trigger_tstamp);
  902. }
  903. }
  904. static struct action_ops snd_pcm_action_pause = {
  905. .pre_action = snd_pcm_pre_pause,
  906. .do_action = snd_pcm_do_pause,
  907. .undo_action = snd_pcm_undo_pause,
  908. .post_action = snd_pcm_post_pause
  909. };
  910. /*
  911. * Push/release the pause for all linked streams.
  912. */
  913. static int snd_pcm_pause(snd_pcm_substream_t *substream, int push)
  914. {
  915. return snd_pcm_action(&snd_pcm_action_pause, substream, push);
  916. }
  917. #ifdef CONFIG_PM
  918. /* suspend */
  919. static int snd_pcm_pre_suspend(snd_pcm_substream_t *substream, int state)
  920. {
  921. snd_pcm_runtime_t *runtime = substream->runtime;
  922. if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
  923. return -EBUSY;
  924. runtime->trigger_master = substream;
  925. return 0;
  926. }
  927. static int snd_pcm_do_suspend(snd_pcm_substream_t *substream, int state)
  928. {
  929. snd_pcm_runtime_t *runtime = substream->runtime;
  930. if (runtime->trigger_master != substream)
  931. return 0;
  932. if (! snd_pcm_running(substream))
  933. return 0;
  934. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
  935. return 0; /* suspend unconditionally */
  936. }
  937. static void snd_pcm_post_suspend(snd_pcm_substream_t *substream, int state)
  938. {
  939. snd_pcm_runtime_t *runtime = substream->runtime;
  940. snd_pcm_trigger_tstamp(substream);
  941. if (substream->timer)
  942. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MPAUSE, &runtime->trigger_tstamp);
  943. runtime->status->suspended_state = runtime->status->state;
  944. runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
  945. snd_pcm_tick_set(substream, 0);
  946. wake_up(&runtime->sleep);
  947. }
  948. static struct action_ops snd_pcm_action_suspend = {
  949. .pre_action = snd_pcm_pre_suspend,
  950. .do_action = snd_pcm_do_suspend,
  951. .post_action = snd_pcm_post_suspend
  952. };
  953. /**
  954. * snd_pcm_suspend
  955. *
  956. * Trigger SUSPEND to all linked streams.
  957. * After this call, all streams are changed to SUSPENDED state.
  958. */
  959. int snd_pcm_suspend(snd_pcm_substream_t *substream)
  960. {
  961. int err;
  962. unsigned long flags;
  963. snd_pcm_stream_lock_irqsave(substream, flags);
  964. err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
  965. snd_pcm_stream_unlock_irqrestore(substream, flags);
  966. return err;
  967. }
  968. /**
  969. * snd_pcm_suspend_all
  970. *
  971. * Trigger SUSPEND to all substreams in the given pcm.
  972. * After this call, all streams are changed to SUSPENDED state.
  973. */
  974. int snd_pcm_suspend_all(snd_pcm_t *pcm)
  975. {
  976. snd_pcm_substream_t *substream;
  977. int stream, err = 0;
  978. for (stream = 0; stream < 2; stream++) {
  979. for (substream = pcm->streams[stream].substream; substream; substream = substream->next) {
  980. /* FIXME: the open/close code should lock this as well */
  981. if (substream->runtime == NULL)
  982. continue;
  983. err = snd_pcm_suspend(substream);
  984. if (err < 0 && err != -EBUSY)
  985. return err;
  986. }
  987. }
  988. return 0;
  989. }
  990. /* resume */
  991. static int snd_pcm_pre_resume(snd_pcm_substream_t *substream, int state)
  992. {
  993. snd_pcm_runtime_t *runtime = substream->runtime;
  994. if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
  995. return -ENOSYS;
  996. runtime->trigger_master = substream;
  997. return 0;
  998. }
  999. static int snd_pcm_do_resume(snd_pcm_substream_t *substream, int state)
  1000. {
  1001. snd_pcm_runtime_t *runtime = substream->runtime;
  1002. if (runtime->trigger_master != substream)
  1003. return 0;
  1004. /* DMA not running previously? */
  1005. if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
  1006. (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
  1007. substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
  1008. return 0;
  1009. return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
  1010. }
  1011. static void snd_pcm_undo_resume(snd_pcm_substream_t *substream, int state)
  1012. {
  1013. if (substream->runtime->trigger_master == substream &&
  1014. snd_pcm_running(substream))
  1015. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
  1016. }
  1017. static void snd_pcm_post_resume(snd_pcm_substream_t *substream, int state)
  1018. {
  1019. snd_pcm_runtime_t *runtime = substream->runtime;
  1020. snd_pcm_trigger_tstamp(substream);
  1021. if (substream->timer)
  1022. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MCONTINUE, &runtime->trigger_tstamp);
  1023. runtime->status->state = runtime->status->suspended_state;
  1024. if (runtime->sleep_min)
  1025. snd_pcm_tick_prepare(substream);
  1026. }
  1027. static struct action_ops snd_pcm_action_resume = {
  1028. .pre_action = snd_pcm_pre_resume,
  1029. .do_action = snd_pcm_do_resume,
  1030. .undo_action = snd_pcm_undo_resume,
  1031. .post_action = snd_pcm_post_resume
  1032. };
  1033. static int snd_pcm_resume(snd_pcm_substream_t *substream)
  1034. {
  1035. snd_card_t *card = substream->pcm->card;
  1036. int res;
  1037. snd_power_lock(card);
  1038. if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
  1039. res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
  1040. snd_power_unlock(card);
  1041. return res;
  1042. }
  1043. #else
  1044. static int snd_pcm_resume(snd_pcm_substream_t *substream)
  1045. {
  1046. return -ENOSYS;
  1047. }
  1048. #endif /* CONFIG_PM */
  1049. /*
  1050. * xrun ioctl
  1051. *
  1052. * Change the RUNNING stream(s) to XRUN state.
  1053. */
  1054. static int snd_pcm_xrun(snd_pcm_substream_t *substream)
  1055. {
  1056. snd_card_t *card = substream->pcm->card;
  1057. snd_pcm_runtime_t *runtime = substream->runtime;
  1058. int result;
  1059. snd_power_lock(card);
  1060. if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1061. result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
  1062. if (result < 0)
  1063. goto _unlock;
  1064. }
  1065. snd_pcm_stream_lock_irq(substream);
  1066. switch (runtime->status->state) {
  1067. case SNDRV_PCM_STATE_XRUN:
  1068. result = 0; /* already there */
  1069. break;
  1070. case SNDRV_PCM_STATE_RUNNING:
  1071. result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  1072. break;
  1073. default:
  1074. result = -EBADFD;
  1075. }
  1076. snd_pcm_stream_unlock_irq(substream);
  1077. _unlock:
  1078. snd_power_unlock(card);
  1079. return result;
  1080. }
  1081. /*
  1082. * reset ioctl
  1083. */
  1084. static int snd_pcm_pre_reset(snd_pcm_substream_t * substream, int state)
  1085. {
  1086. snd_pcm_runtime_t *runtime = substream->runtime;
  1087. switch (runtime->status->state) {
  1088. case SNDRV_PCM_STATE_RUNNING:
  1089. case SNDRV_PCM_STATE_PREPARED:
  1090. case SNDRV_PCM_STATE_PAUSED:
  1091. case SNDRV_PCM_STATE_SUSPENDED:
  1092. return 0;
  1093. default:
  1094. return -EBADFD;
  1095. }
  1096. }
  1097. static int snd_pcm_do_reset(snd_pcm_substream_t * substream, int state)
  1098. {
  1099. snd_pcm_runtime_t *runtime = substream->runtime;
  1100. int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
  1101. if (err < 0)
  1102. return err;
  1103. // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
  1104. runtime->hw_ptr_base = 0;
  1105. runtime->hw_ptr_interrupt = runtime->status->hw_ptr - runtime->status->hw_ptr % runtime->period_size;
  1106. runtime->silence_start = runtime->status->hw_ptr;
  1107. runtime->silence_filled = 0;
  1108. return 0;
  1109. }
  1110. static void snd_pcm_post_reset(snd_pcm_substream_t * substream, int state)
  1111. {
  1112. snd_pcm_runtime_t *runtime = substream->runtime;
  1113. runtime->control->appl_ptr = runtime->status->hw_ptr;
  1114. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  1115. runtime->silence_size > 0)
  1116. snd_pcm_playback_silence(substream, ULONG_MAX);
  1117. }
  1118. static struct action_ops snd_pcm_action_reset = {
  1119. .pre_action = snd_pcm_pre_reset,
  1120. .do_action = snd_pcm_do_reset,
  1121. .post_action = snd_pcm_post_reset
  1122. };
  1123. static int snd_pcm_reset(snd_pcm_substream_t *substream)
  1124. {
  1125. return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
  1126. }
  1127. /*
  1128. * prepare ioctl
  1129. */
  1130. static int snd_pcm_pre_prepare(snd_pcm_substream_t * substream, int state)
  1131. {
  1132. snd_pcm_runtime_t *runtime = substream->runtime;
  1133. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1134. return -EBADFD;
  1135. if (snd_pcm_running(substream))
  1136. return -EBUSY;
  1137. return 0;
  1138. }
  1139. static int snd_pcm_do_prepare(snd_pcm_substream_t * substream, int state)
  1140. {
  1141. int err;
  1142. err = substream->ops->prepare(substream);
  1143. if (err < 0)
  1144. return err;
  1145. return snd_pcm_do_reset(substream, 0);
  1146. }
  1147. static void snd_pcm_post_prepare(snd_pcm_substream_t * substream, int state)
  1148. {
  1149. snd_pcm_runtime_t *runtime = substream->runtime;
  1150. runtime->control->appl_ptr = runtime->status->hw_ptr;
  1151. runtime->status->state = SNDRV_PCM_STATE_PREPARED;
  1152. }
  1153. static struct action_ops snd_pcm_action_prepare = {
  1154. .pre_action = snd_pcm_pre_prepare,
  1155. .do_action = snd_pcm_do_prepare,
  1156. .post_action = snd_pcm_post_prepare
  1157. };
  1158. /**
  1159. * snd_pcm_prepare
  1160. */
  1161. int snd_pcm_prepare(snd_pcm_substream_t *substream)
  1162. {
  1163. int res;
  1164. snd_card_t *card = substream->pcm->card;
  1165. snd_power_lock(card);
  1166. if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
  1167. res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare, substream, 0);
  1168. snd_power_unlock(card);
  1169. return res;
  1170. }
  1171. /*
  1172. * drain ioctl
  1173. */
  1174. static int snd_pcm_pre_drain_init(snd_pcm_substream_t * substream, int state)
  1175. {
  1176. if (substream->ffile->f_flags & O_NONBLOCK)
  1177. return -EAGAIN;
  1178. substream->runtime->trigger_master = substream;
  1179. return 0;
  1180. }
  1181. static int snd_pcm_do_drain_init(snd_pcm_substream_t * substream, int state)
  1182. {
  1183. snd_pcm_runtime_t *runtime = substream->runtime;
  1184. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1185. switch (runtime->status->state) {
  1186. case SNDRV_PCM_STATE_PREPARED:
  1187. /* start playback stream if possible */
  1188. if (! snd_pcm_playback_empty(substream)) {
  1189. snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
  1190. snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
  1191. }
  1192. break;
  1193. case SNDRV_PCM_STATE_RUNNING:
  1194. runtime->status->state = SNDRV_PCM_STATE_DRAINING;
  1195. break;
  1196. default:
  1197. break;
  1198. }
  1199. } else {
  1200. /* stop running stream */
  1201. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
  1202. int state = snd_pcm_capture_avail(runtime) > 0 ?
  1203. SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
  1204. snd_pcm_do_stop(substream, state);
  1205. snd_pcm_post_stop(substream, state);
  1206. }
  1207. }
  1208. return 0;
  1209. }
  1210. static void snd_pcm_post_drain_init(snd_pcm_substream_t * substream, int state)
  1211. {
  1212. }
  1213. static struct action_ops snd_pcm_action_drain_init = {
  1214. .pre_action = snd_pcm_pre_drain_init,
  1215. .do_action = snd_pcm_do_drain_init,
  1216. .post_action = snd_pcm_post_drain_init
  1217. };
  1218. struct drain_rec {
  1219. snd_pcm_substream_t *substream;
  1220. wait_queue_t wait;
  1221. snd_pcm_uframes_t stop_threshold;
  1222. };
  1223. static int snd_pcm_drop(snd_pcm_substream_t *substream);
  1224. /*
  1225. * Drain the stream(s).
  1226. * When the substream is linked, sync until the draining of all playback streams
  1227. * is finished.
  1228. * After this call, all streams are supposed to be either SETUP or DRAINING
  1229. * (capture only) state.
  1230. */
  1231. static int snd_pcm_drain(snd_pcm_substream_t *substream)
  1232. {
  1233. snd_card_t *card;
  1234. snd_pcm_runtime_t *runtime;
  1235. struct list_head *pos;
  1236. int result = 0;
  1237. int i, num_drecs;
  1238. struct drain_rec *drec, drec_tmp, *d;
  1239. snd_assert(substream != NULL, return -ENXIO);
  1240. card = substream->pcm->card;
  1241. runtime = substream->runtime;
  1242. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1243. return -EBADFD;
  1244. snd_power_lock(card);
  1245. if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1246. result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
  1247. if (result < 0) {
  1248. snd_power_unlock(card);
  1249. return result;
  1250. }
  1251. }
  1252. /* allocate temporary record for drain sync */
  1253. down_read(&snd_pcm_link_rwsem);
  1254. if (snd_pcm_stream_linked(substream)) {
  1255. drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
  1256. if (! drec) {
  1257. up_read(&snd_pcm_link_rwsem);
  1258. snd_power_unlock(card);
  1259. return -ENOMEM;
  1260. }
  1261. } else
  1262. drec = &drec_tmp;
  1263. /* count only playback streams */
  1264. num_drecs = 0;
  1265. snd_pcm_group_for_each(pos, substream) {
  1266. snd_pcm_substream_t *s = snd_pcm_group_substream_entry(pos);
  1267. runtime = s->runtime;
  1268. if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1269. d = &drec[num_drecs++];
  1270. d->substream = s;
  1271. init_waitqueue_entry(&d->wait, current);
  1272. add_wait_queue(&runtime->sleep, &d->wait);
  1273. /* stop_threshold fixup to avoid endless loop when
  1274. * stop_threshold > buffer_size
  1275. */
  1276. d->stop_threshold = runtime->stop_threshold;
  1277. if (runtime->stop_threshold > runtime->buffer_size)
  1278. runtime->stop_threshold = runtime->buffer_size;
  1279. }
  1280. }
  1281. up_read(&snd_pcm_link_rwsem);
  1282. if (! num_drecs)
  1283. goto _error;
  1284. snd_pcm_stream_lock_irq(substream);
  1285. /* resume pause */
  1286. if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
  1287. snd_pcm_pause(substream, 0);
  1288. /* pre-start/stop - all running streams are changed to DRAINING state */
  1289. result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
  1290. if (result < 0) {
  1291. snd_pcm_stream_unlock_irq(substream);
  1292. goto _error;
  1293. }
  1294. for (;;) {
  1295. long tout;
  1296. if (signal_pending(current)) {
  1297. result = -ERESTARTSYS;
  1298. break;
  1299. }
  1300. /* all finished? */
  1301. for (i = 0; i < num_drecs; i++) {
  1302. runtime = drec[i].substream->runtime;
  1303. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
  1304. break;
  1305. }
  1306. if (i == num_drecs)
  1307. break; /* yes, all drained */
  1308. set_current_state(TASK_INTERRUPTIBLE);
  1309. snd_pcm_stream_unlock_irq(substream);
  1310. snd_power_unlock(card);
  1311. tout = schedule_timeout(10 * HZ);
  1312. snd_power_lock(card);
  1313. snd_pcm_stream_lock_irq(substream);
  1314. if (tout == 0) {
  1315. if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
  1316. result = -ESTRPIPE;
  1317. else {
  1318. snd_printd("playback drain error (DMA or IRQ trouble?)\n");
  1319. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1320. result = -EIO;
  1321. }
  1322. break;
  1323. }
  1324. }
  1325. snd_pcm_stream_unlock_irq(substream);
  1326. _error:
  1327. for (i = 0; i < num_drecs; i++) {
  1328. d = &drec[i];
  1329. runtime = d->substream->runtime;
  1330. remove_wait_queue(&runtime->sleep, &d->wait);
  1331. runtime->stop_threshold = d->stop_threshold;
  1332. }
  1333. if (drec != &drec_tmp)
  1334. kfree(drec);
  1335. snd_power_unlock(card);
  1336. return result;
  1337. }
  1338. /*
  1339. * drop ioctl
  1340. *
  1341. * Immediately put all linked substreams into SETUP state.
  1342. */
  1343. static int snd_pcm_drop(snd_pcm_substream_t *substream)
  1344. {
  1345. snd_pcm_runtime_t *runtime;
  1346. snd_card_t *card;
  1347. int result = 0;
  1348. snd_assert(substream != NULL, return -ENXIO);
  1349. runtime = substream->runtime;
  1350. card = substream->pcm->card;
  1351. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1352. return -EBADFD;
  1353. snd_power_lock(card);
  1354. if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1355. result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
  1356. if (result < 0)
  1357. goto _unlock;
  1358. }
  1359. snd_pcm_stream_lock_irq(substream);
  1360. /* resume pause */
  1361. if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
  1362. snd_pcm_pause(substream, 0);
  1363. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1364. /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
  1365. snd_pcm_stream_unlock_irq(substream);
  1366. _unlock:
  1367. snd_power_unlock(card);
  1368. return result;
  1369. }
  1370. /* WARNING: Don't forget to fput back the file */
  1371. extern int snd_major;
  1372. static struct file *snd_pcm_file_fd(int fd)
  1373. {
  1374. struct file *file;
  1375. struct inode *inode;
  1376. unsigned short minor;
  1377. file = fget(fd);
  1378. if (!file)
  1379. return NULL;
  1380. inode = file->f_dentry->d_inode;
  1381. if (!S_ISCHR(inode->i_mode) ||
  1382. imajor(inode) != snd_major) {
  1383. fput(file);
  1384. return NULL;
  1385. }
  1386. minor = iminor(inode);
  1387. if (minor >= 256 ||
  1388. minor % SNDRV_MINOR_DEVICES < SNDRV_MINOR_PCM_PLAYBACK) {
  1389. fput(file);
  1390. return NULL;
  1391. }
  1392. return file;
  1393. }
  1394. /*
  1395. * PCM link handling
  1396. */
  1397. static int snd_pcm_link(snd_pcm_substream_t *substream, int fd)
  1398. {
  1399. int res = 0;
  1400. struct file *file;
  1401. snd_pcm_file_t *pcm_file;
  1402. snd_pcm_substream_t *substream1;
  1403. file = snd_pcm_file_fd(fd);
  1404. if (!file)
  1405. return -EBADFD;
  1406. pcm_file = file->private_data;
  1407. substream1 = pcm_file->substream;
  1408. down_write(&snd_pcm_link_rwsem);
  1409. write_lock_irq(&snd_pcm_link_rwlock);
  1410. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
  1411. substream->runtime->status->state != substream1->runtime->status->state) {
  1412. res = -EBADFD;
  1413. goto _end;
  1414. }
  1415. if (snd_pcm_stream_linked(substream1)) {
  1416. res = -EALREADY;
  1417. goto _end;
  1418. }
  1419. if (!snd_pcm_stream_linked(substream)) {
  1420. substream->group = kmalloc(sizeof(snd_pcm_group_t), GFP_ATOMIC);
  1421. if (substream->group == NULL) {
  1422. res = -ENOMEM;
  1423. goto _end;
  1424. }
  1425. spin_lock_init(&substream->group->lock);
  1426. INIT_LIST_HEAD(&substream->group->substreams);
  1427. list_add_tail(&substream->link_list, &substream->group->substreams);
  1428. substream->group->count = 1;
  1429. }
  1430. list_add_tail(&substream1->link_list, &substream->group->substreams);
  1431. substream->group->count++;
  1432. substream1->group = substream->group;
  1433. _end:
  1434. write_unlock_irq(&snd_pcm_link_rwlock);
  1435. up_write(&snd_pcm_link_rwsem);
  1436. fput(file);
  1437. return res;
  1438. }
  1439. static void relink_to_local(snd_pcm_substream_t *substream)
  1440. {
  1441. substream->group = &substream->self_group;
  1442. INIT_LIST_HEAD(&substream->self_group.substreams);
  1443. list_add_tail(&substream->link_list, &substream->self_group.substreams);
  1444. }
  1445. static int snd_pcm_unlink(snd_pcm_substream_t *substream)
  1446. {
  1447. struct list_head *pos;
  1448. int res = 0;
  1449. down_write(&snd_pcm_link_rwsem);
  1450. write_lock_irq(&snd_pcm_link_rwlock);
  1451. if (!snd_pcm_stream_linked(substream)) {
  1452. res = -EALREADY;
  1453. goto _end;
  1454. }
  1455. list_del(&substream->link_list);
  1456. substream->group->count--;
  1457. if (substream->group->count == 1) { /* detach the last stream, too */
  1458. snd_pcm_group_for_each(pos, substream) {
  1459. relink_to_local(snd_pcm_group_substream_entry(pos));
  1460. break;
  1461. }
  1462. kfree(substream->group);
  1463. }
  1464. relink_to_local(substream);
  1465. _end:
  1466. write_unlock_irq(&snd_pcm_link_rwlock);
  1467. up_write(&snd_pcm_link_rwsem);
  1468. return res;
  1469. }
  1470. /*
  1471. * hw configurator
  1472. */
  1473. static int snd_pcm_hw_rule_mul(snd_pcm_hw_params_t *params,
  1474. snd_pcm_hw_rule_t *rule)
  1475. {
  1476. snd_interval_t t;
  1477. snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
  1478. hw_param_interval_c(params, rule->deps[1]), &t);
  1479. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1480. }
  1481. static int snd_pcm_hw_rule_div(snd_pcm_hw_params_t *params,
  1482. snd_pcm_hw_rule_t *rule)
  1483. {
  1484. snd_interval_t t;
  1485. snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
  1486. hw_param_interval_c(params, rule->deps[1]), &t);
  1487. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1488. }
  1489. static int snd_pcm_hw_rule_muldivk(snd_pcm_hw_params_t *params,
  1490. snd_pcm_hw_rule_t *rule)
  1491. {
  1492. snd_interval_t t;
  1493. snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
  1494. hw_param_interval_c(params, rule->deps[1]),
  1495. (unsigned long) rule->private, &t);
  1496. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1497. }
  1498. static int snd_pcm_hw_rule_mulkdiv(snd_pcm_hw_params_t *params,
  1499. snd_pcm_hw_rule_t *rule)
  1500. {
  1501. snd_interval_t t;
  1502. snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
  1503. (unsigned long) rule->private,
  1504. hw_param_interval_c(params, rule->deps[1]), &t);
  1505. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1506. }
  1507. static int snd_pcm_hw_rule_format(snd_pcm_hw_params_t *params,
  1508. snd_pcm_hw_rule_t *rule)
  1509. {
  1510. unsigned int k;
  1511. snd_interval_t *i = hw_param_interval(params, rule->deps[0]);
  1512. snd_mask_t m;
  1513. snd_mask_t *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  1514. snd_mask_any(&m);
  1515. for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
  1516. int bits;
  1517. if (! snd_mask_test(mask, k))
  1518. continue;
  1519. bits = snd_pcm_format_physical_width(k);
  1520. if (bits <= 0)
  1521. continue; /* ignore invalid formats */
  1522. if ((unsigned)bits < i->min || (unsigned)bits > i->max)
  1523. snd_mask_reset(&m, k);
  1524. }
  1525. return snd_mask_refine(mask, &m);
  1526. }
  1527. static int snd_pcm_hw_rule_sample_bits(snd_pcm_hw_params_t *params,
  1528. snd_pcm_hw_rule_t *rule)
  1529. {
  1530. snd_interval_t t;
  1531. unsigned int k;
  1532. t.min = UINT_MAX;
  1533. t.max = 0;
  1534. t.openmin = 0;
  1535. t.openmax = 0;
  1536. for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
  1537. int bits;
  1538. if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
  1539. continue;
  1540. bits = snd_pcm_format_physical_width(k);
  1541. if (bits <= 0)
  1542. continue; /* ignore invalid formats */
  1543. if (t.min > (unsigned)bits)
  1544. t.min = bits;
  1545. if (t.max < (unsigned)bits)
  1546. t.max = bits;
  1547. }
  1548. t.integer = 1;
  1549. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1550. }
  1551. #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
  1552. #error "Change this table"
  1553. #endif
  1554. static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
  1555. 48000, 64000, 88200, 96000, 176400, 192000 };
  1556. static int snd_pcm_hw_rule_rate(snd_pcm_hw_params_t *params,
  1557. snd_pcm_hw_rule_t *rule)
  1558. {
  1559. snd_pcm_hardware_t *hw = rule->private;
  1560. return snd_interval_list(hw_param_interval(params, rule->var),
  1561. ARRAY_SIZE(rates), rates, hw->rates);
  1562. }
  1563. static int snd_pcm_hw_rule_buffer_bytes_max(snd_pcm_hw_params_t *params,
  1564. snd_pcm_hw_rule_t *rule)
  1565. {
  1566. snd_interval_t t;
  1567. snd_pcm_substream_t *substream = rule->private;
  1568. t.min = 0;
  1569. t.max = substream->buffer_bytes_max;
  1570. t.openmin = 0;
  1571. t.openmax = 0;
  1572. t.integer = 1;
  1573. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1574. }
  1575. int snd_pcm_hw_constraints_init(snd_pcm_substream_t *substream)
  1576. {
  1577. snd_pcm_runtime_t *runtime = substream->runtime;
  1578. snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
  1579. int k, err;
  1580. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
  1581. snd_mask_any(constrs_mask(constrs, k));
  1582. }
  1583. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
  1584. snd_interval_any(constrs_interval(constrs, k));
  1585. }
  1586. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
  1587. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
  1588. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
  1589. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
  1590. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
  1591. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  1592. snd_pcm_hw_rule_format, NULL,
  1593. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1594. if (err < 0)
  1595. return err;
  1596. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  1597. snd_pcm_hw_rule_sample_bits, NULL,
  1598. SNDRV_PCM_HW_PARAM_FORMAT,
  1599. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1600. if (err < 0)
  1601. return err;
  1602. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  1603. snd_pcm_hw_rule_div, NULL,
  1604. SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  1605. if (err < 0)
  1606. return err;
  1607. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1608. snd_pcm_hw_rule_mul, NULL,
  1609. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  1610. if (err < 0)
  1611. return err;
  1612. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1613. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1614. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
  1615. if (err < 0)
  1616. return err;
  1617. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1618. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1619. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
  1620. if (err < 0)
  1621. return err;
  1622. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  1623. snd_pcm_hw_rule_div, NULL,
  1624. SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1625. if (err < 0)
  1626. return err;
  1627. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1628. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1629. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
  1630. if (err < 0)
  1631. return err;
  1632. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1633. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1634. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
  1635. if (err < 0)
  1636. return err;
  1637. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
  1638. snd_pcm_hw_rule_div, NULL,
  1639. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
  1640. if (err < 0)
  1641. return err;
  1642. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1643. snd_pcm_hw_rule_div, NULL,
  1644. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
  1645. if (err < 0)
  1646. return err;
  1647. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1648. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1649. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1650. if (err < 0)
  1651. return err;
  1652. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1653. snd_pcm_hw_rule_muldivk, (void*) 1000000,
  1654. SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
  1655. if (err < 0)
  1656. return err;
  1657. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1658. snd_pcm_hw_rule_mul, NULL,
  1659. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
  1660. if (err < 0)
  1661. return err;
  1662. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1663. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1664. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1665. if (err < 0)
  1666. return err;
  1667. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1668. snd_pcm_hw_rule_muldivk, (void*) 1000000,
  1669. SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
  1670. if (err < 0)
  1671. return err;
  1672. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1673. snd_pcm_hw_rule_muldivk, (void*) 8,
  1674. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1675. if (err < 0)
  1676. return err;
  1677. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1678. snd_pcm_hw_rule_muldivk, (void*) 8,
  1679. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1680. if (err < 0)
  1681. return err;
  1682. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1683. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1684. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
  1685. if (err < 0)
  1686. return err;
  1687. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
  1688. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1689. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
  1690. if (err < 0)
  1691. return err;
  1692. return 0;
  1693. }
  1694. int snd_pcm_hw_constraints_complete(snd_pcm_substream_t *substream)
  1695. {
  1696. snd_pcm_runtime_t *runtime = substream->runtime;
  1697. snd_pcm_hardware_t *hw = &runtime->hw;
  1698. int err;
  1699. unsigned int mask = 0;
  1700. if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
  1701. mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  1702. if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
  1703. mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
  1704. if (hw->info & SNDRV_PCM_INFO_MMAP) {
  1705. if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
  1706. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
  1707. if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
  1708. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
  1709. if (hw->info & SNDRV_PCM_INFO_COMPLEX)
  1710. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
  1711. }
  1712. err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
  1713. snd_assert(err >= 0, return -EINVAL);
  1714. err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
  1715. snd_assert(err >= 0, return -EINVAL);
  1716. err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
  1717. snd_assert(err >= 0, return -EINVAL);
  1718. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
  1719. hw->channels_min, hw->channels_max);
  1720. snd_assert(err >= 0, return -EINVAL);
  1721. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
  1722. hw->rate_min, hw->rate_max);
  1723. snd_assert(err >= 0, return -EINVAL);
  1724. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1725. hw->period_bytes_min, hw->period_bytes_max);
  1726. snd_assert(err >= 0, return -EINVAL);
  1727. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
  1728. hw->periods_min, hw->periods_max);
  1729. snd_assert(err >= 0, return -EINVAL);
  1730. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1731. hw->period_bytes_min, hw->buffer_bytes_max);
  1732. snd_assert(err >= 0, return -EINVAL);
  1733. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1734. snd_pcm_hw_rule_buffer_bytes_max, substream,
  1735. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
  1736. if (err < 0)
  1737. return err;
  1738. /* FIXME: remove */
  1739. if (runtime->dma_bytes) {
  1740. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
  1741. snd_assert(err >= 0, return -EINVAL);
  1742. }
  1743. if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
  1744. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1745. snd_pcm_hw_rule_rate, hw,
  1746. SNDRV_PCM_HW_PARAM_RATE, -1);
  1747. if (err < 0)
  1748. return err;
  1749. }
  1750. /* FIXME: this belong to lowlevel */
  1751. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
  1752. 1000000 / HZ, 1000000 / HZ);
  1753. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
  1754. return 0;
  1755. }
  1756. static void snd_pcm_add_file(snd_pcm_str_t *str,
  1757. snd_pcm_file_t *pcm_file)
  1758. {
  1759. pcm_file->next = str->files;
  1760. str->files = pcm_file;
  1761. }
  1762. static void snd_pcm_remove_file(snd_pcm_str_t *str,
  1763. snd_pcm_file_t *pcm_file)
  1764. {
  1765. snd_pcm_file_t * pcm_file1;
  1766. if (str->files == pcm_file) {
  1767. str->files = pcm_file->next;
  1768. } else {
  1769. pcm_file1 = str->files;
  1770. while (pcm_file1 && pcm_file1->next != pcm_file)
  1771. pcm_file1 = pcm_file1->next;
  1772. if (pcm_file1 != NULL)
  1773. pcm_file1->next = pcm_file->next;
  1774. }
  1775. }
  1776. static int snd_pcm_release_file(snd_pcm_file_t * pcm_file)
  1777. {
  1778. snd_pcm_substream_t *substream;
  1779. snd_pcm_runtime_t *runtime;
  1780. snd_pcm_str_t * str;
  1781. snd_assert(pcm_file != NULL, return -ENXIO);
  1782. substream = pcm_file->substream;
  1783. snd_assert(substream != NULL, return -ENXIO);
  1784. runtime = substream->runtime;
  1785. str = substream->pstr;
  1786. snd_pcm_unlink(substream);
  1787. if (substream->open_flag) {
  1788. if (substream->ops->hw_free != NULL)
  1789. substream->ops->hw_free(substream);
  1790. substream->ops->close(substream);
  1791. substream->open_flag = 0;
  1792. }
  1793. substream->ffile = NULL;
  1794. snd_pcm_remove_file(str, pcm_file);
  1795. snd_pcm_release_substream(substream);
  1796. kfree(pcm_file);
  1797. return 0;
  1798. }
  1799. static int snd_pcm_open_file(struct file *file,
  1800. snd_pcm_t *pcm,
  1801. int stream,
  1802. snd_pcm_file_t **rpcm_file)
  1803. {
  1804. int err = 0;
  1805. snd_pcm_file_t *pcm_file;
  1806. snd_pcm_substream_t *substream;
  1807. snd_pcm_str_t *str;
  1808. snd_assert(rpcm_file != NULL, return -EINVAL);
  1809. *rpcm_file = NULL;
  1810. pcm_file = kcalloc(1, sizeof(*pcm_file), GFP_KERNEL);
  1811. if (pcm_file == NULL) {
  1812. return -ENOMEM;
  1813. }
  1814. if ((err = snd_pcm_open_substream(pcm, stream, &substream)) < 0) {
  1815. kfree(pcm_file);
  1816. return err;
  1817. }
  1818. str = substream->pstr;
  1819. substream->file = pcm_file;
  1820. substream->no_mmap_ctrl = 0;
  1821. pcm_file->substream = substream;
  1822. snd_pcm_add_file(str, pcm_file);
  1823. err = snd_pcm_hw_constraints_init(substream);
  1824. if (err < 0) {
  1825. snd_printd("snd_pcm_hw_constraints_init failed\n");
  1826. snd_pcm_release_file(pcm_file);
  1827. return err;
  1828. }
  1829. if ((err = substream->ops->open(substream)) < 0) {
  1830. snd_pcm_release_file(pcm_file);
  1831. return err;
  1832. }
  1833. substream->open_flag = 1;
  1834. err = snd_pcm_hw_constraints_complete(substream);
  1835. if (err < 0) {
  1836. snd_printd("snd_pcm_hw_constraints_complete failed\n");
  1837. substream->ops->close(substream);
  1838. snd_pcm_release_file(pcm_file);
  1839. return err;
  1840. }
  1841. substream->ffile = file;
  1842. file->private_data = pcm_file;
  1843. *rpcm_file = pcm_file;
  1844. return 0;
  1845. }
  1846. static int snd_pcm_open(struct inode *inode, struct file *file)
  1847. {
  1848. int cardnum = SNDRV_MINOR_CARD(iminor(inode));
  1849. int device = SNDRV_MINOR_DEVICE(iminor(inode));
  1850. int err;
  1851. snd_pcm_t *pcm;
  1852. snd_pcm_file_t *pcm_file;
  1853. wait_queue_t wait;
  1854. snd_runtime_check(device >= SNDRV_MINOR_PCM_PLAYBACK && device < SNDRV_MINOR_DEVICES, return -ENXIO);
  1855. pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + (device % SNDRV_MINOR_PCMS)];
  1856. if (pcm == NULL) {
  1857. err = -ENODEV;
  1858. goto __error1;
  1859. }
  1860. err = snd_card_file_add(pcm->card, file);
  1861. if (err < 0)
  1862. goto __error1;
  1863. if (!try_module_get(pcm->card->module)) {
  1864. err = -EFAULT;
  1865. goto __error2;
  1866. }
  1867. init_waitqueue_entry(&wait, current);
  1868. add_wait_queue(&pcm->open_wait, &wait);
  1869. down(&pcm->open_mutex);
  1870. while (1) {
  1871. err = snd_pcm_open_file(file, pcm, device >= SNDRV_MINOR_PCM_CAPTURE ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK, &pcm_file);
  1872. if (err >= 0)
  1873. break;
  1874. if (err == -EAGAIN) {
  1875. if (file->f_flags & O_NONBLOCK) {
  1876. err = -EBUSY;
  1877. break;
  1878. }
  1879. } else
  1880. break;
  1881. set_current_state(TASK_INTERRUPTIBLE);
  1882. up(&pcm->open_mutex);
  1883. schedule();
  1884. down(&pcm->open_mutex);
  1885. if (signal_pending(current)) {
  1886. err = -ERESTARTSYS;
  1887. break;
  1888. }
  1889. }
  1890. remove_wait_queue(&pcm->open_wait, &wait);
  1891. up(&pcm->open_mutex);
  1892. if (err < 0)
  1893. goto __error;
  1894. return err;
  1895. __error:
  1896. module_put(pcm->card->module);
  1897. __error2:
  1898. snd_card_file_remove(pcm->card, file);
  1899. __error1:
  1900. return err;
  1901. }
  1902. static int snd_pcm_release(struct inode *inode, struct file *file)
  1903. {
  1904. snd_pcm_t *pcm;
  1905. snd_pcm_substream_t *substream;
  1906. snd_pcm_file_t *pcm_file;
  1907. pcm_file = file->private_data;
  1908. substream = pcm_file->substream;
  1909. snd_assert(substream != NULL, return -ENXIO);
  1910. snd_assert(!atomic_read(&substream->runtime->mmap_count), );
  1911. pcm = substream->pcm;
  1912. snd_pcm_drop(substream);
  1913. fasync_helper(-1, file, 0, &substream->runtime->fasync);
  1914. down(&pcm->open_mutex);
  1915. snd_pcm_release_file(pcm_file);
  1916. up(&pcm->open_mutex);
  1917. wake_up(&pcm->open_wait);
  1918. module_put(pcm->card->module);
  1919. snd_card_file_remove(pcm->card, file);
  1920. return 0;
  1921. }
  1922. static snd_pcm_sframes_t snd_pcm_playback_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
  1923. {
  1924. snd_pcm_runtime_t *runtime = substream->runtime;
  1925. snd_pcm_sframes_t appl_ptr;
  1926. snd_pcm_sframes_t ret;
  1927. snd_pcm_sframes_t hw_avail;
  1928. if (frames == 0)
  1929. return 0;
  1930. snd_pcm_stream_lock_irq(substream);
  1931. switch (runtime->status->state) {
  1932. case SNDRV_PCM_STATE_PREPARED:
  1933. break;
  1934. case SNDRV_PCM_STATE_DRAINING:
  1935. case SNDRV_PCM_STATE_RUNNING:
  1936. if (snd_pcm_update_hw_ptr(substream) >= 0)
  1937. break;
  1938. /* Fall through */
  1939. case SNDRV_PCM_STATE_XRUN:
  1940. ret = -EPIPE;
  1941. goto __end;
  1942. default:
  1943. ret = -EBADFD;
  1944. goto __end;
  1945. }
  1946. hw_avail = snd_pcm_playback_hw_avail(runtime);
  1947. if (hw_avail <= 0) {
  1948. ret = 0;
  1949. goto __end;
  1950. }
  1951. if (frames > (snd_pcm_uframes_t)hw_avail)
  1952. frames = hw_avail;
  1953. else
  1954. frames -= frames % runtime->xfer_align;
  1955. appl_ptr = runtime->control->appl_ptr - frames;
  1956. if (appl_ptr < 0)
  1957. appl_ptr += runtime->boundary;
  1958. runtime->control->appl_ptr = appl_ptr;
  1959. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
  1960. runtime->sleep_min)
  1961. snd_pcm_tick_prepare(substream);
  1962. ret = frames;
  1963. __end:
  1964. snd_pcm_stream_unlock_irq(substream);
  1965. return ret;
  1966. }
  1967. static snd_pcm_sframes_t snd_pcm_capture_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
  1968. {
  1969. snd_pcm_runtime_t *runtime = substream->runtime;
  1970. snd_pcm_sframes_t appl_ptr;
  1971. snd_pcm_sframes_t ret;
  1972. snd_pcm_sframes_t hw_avail;
  1973. if (frames == 0)
  1974. return 0;
  1975. snd_pcm_stream_lock_irq(substream);
  1976. switch (runtime->status->state) {
  1977. case SNDRV_PCM_STATE_PREPARED:
  1978. case SNDRV_PCM_STATE_DRAINING:
  1979. break;
  1980. case SNDRV_PCM_STATE_RUNNING:
  1981. if (snd_pcm_update_hw_ptr(substream) >= 0)
  1982. break;
  1983. /* Fall through */
  1984. case SNDRV_PCM_STATE_XRUN:
  1985. ret = -EPIPE;
  1986. goto __end;
  1987. default:
  1988. ret = -EBADFD;
  1989. goto __end;
  1990. }
  1991. hw_avail = snd_pcm_capture_hw_avail(runtime);
  1992. if (hw_avail <= 0) {
  1993. ret = 0;
  1994. goto __end;
  1995. }
  1996. if (frames > (snd_pcm_uframes_t)hw_avail)
  1997. frames = hw_avail;
  1998. else
  1999. frames -= frames % runtime->xfer_align;
  2000. appl_ptr = runtime->control->appl_ptr - frames;
  2001. if (appl_ptr < 0)
  2002. appl_ptr += runtime->boundary;
  2003. runtime->control->appl_ptr = appl_ptr;
  2004. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
  2005. runtime->sleep_min)
  2006. snd_pcm_tick_prepare(substream);
  2007. ret = frames;
  2008. __end:
  2009. snd_pcm_stream_unlock_irq(substream);
  2010. return ret;
  2011. }
  2012. static snd_pcm_sframes_t snd_pcm_playback_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
  2013. {
  2014. snd_pcm_runtime_t *runtime = substream->runtime;
  2015. snd_pcm_sframes_t appl_ptr;
  2016. snd_pcm_sframes_t ret;
  2017. snd_pcm_sframes_t avail;
  2018. if (frames == 0)
  2019. return 0;
  2020. snd_pcm_stream_lock_irq(substream);
  2021. switch (runtime->status->state) {
  2022. case SNDRV_PCM_STATE_PREPARED:
  2023. case SNDRV_PCM_STATE_PAUSED:
  2024. break;
  2025. case SNDRV_PCM_STATE_DRAINING:
  2026. case SNDRV_PCM_STATE_RUNNING:
  2027. if (snd_pcm_update_hw_ptr(substream) >= 0)
  2028. break;
  2029. /* Fall through */
  2030. case SNDRV_PCM_STATE_XRUN:
  2031. ret = -EPIPE;
  2032. goto __end;
  2033. default:
  2034. ret = -EBADFD;
  2035. goto __end;
  2036. }
  2037. avail = snd_pcm_playback_avail(runtime);
  2038. if (avail <= 0) {
  2039. ret = 0;
  2040. goto __end;
  2041. }
  2042. if (frames > (snd_pcm_uframes_t)avail)
  2043. frames = avail;
  2044. else
  2045. frames -= frames % runtime->xfer_align;
  2046. appl_ptr = runtime->control->appl_ptr + frames;
  2047. if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
  2048. appl_ptr -= runtime->boundary;
  2049. runtime->control->appl_ptr = appl_ptr;
  2050. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
  2051. runtime->sleep_min)
  2052. snd_pcm_tick_prepare(substream);
  2053. ret = frames;
  2054. __end:
  2055. snd_pcm_stream_unlock_irq(substream);
  2056. return ret;
  2057. }
  2058. static snd_pcm_sframes_t snd_pcm_capture_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
  2059. {
  2060. snd_pcm_runtime_t *runtime = substream->runtime;
  2061. snd_pcm_sframes_t appl_ptr;
  2062. snd_pcm_sframes_t ret;
  2063. snd_pcm_sframes_t avail;
  2064. if (frames == 0)
  2065. return 0;
  2066. snd_pcm_stream_lock_irq(substream);
  2067. switch (runtime->status->state) {
  2068. case SNDRV_PCM_STATE_PREPARED:
  2069. case SNDRV_PCM_STATE_DRAINING:
  2070. case SNDRV_PCM_STATE_PAUSED:
  2071. break;
  2072. case SNDRV_PCM_STATE_RUNNING:
  2073. if (snd_pcm_update_hw_ptr(substream) >= 0)
  2074. break;
  2075. /* Fall through */
  2076. case SNDRV_PCM_STATE_XRUN:
  2077. ret = -EPIPE;
  2078. goto __end;
  2079. default:
  2080. ret = -EBADFD;
  2081. goto __end;
  2082. }
  2083. avail = snd_pcm_capture_avail(runtime);
  2084. if (avail <= 0) {
  2085. ret = 0;
  2086. goto __end;
  2087. }
  2088. if (frames > (snd_pcm_uframes_t)avail)
  2089. frames = avail;
  2090. else
  2091. frames -= frames % runtime->xfer_align;
  2092. appl_ptr = runtime->control->appl_ptr + frames;
  2093. if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
  2094. appl_ptr -= runtime->boundary;
  2095. runtime->control->appl_ptr = appl_ptr;
  2096. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
  2097. runtime->sleep_min)
  2098. snd_pcm_tick_prepare(substream);
  2099. ret = frames;
  2100. __end:
  2101. snd_pcm_stream_unlock_irq(substream);
  2102. return ret;
  2103. }
  2104. static int snd_pcm_hwsync(snd_pcm_substream_t *substream)
  2105. {
  2106. snd_pcm_runtime_t *runtime = substream->runtime;
  2107. int err;
  2108. snd_pcm_stream_lock_irq(substream);
  2109. switch (runtime->status->state) {
  2110. case SNDRV_PCM_STATE_DRAINING:
  2111. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  2112. goto __badfd;
  2113. case SNDRV_PCM_STATE_RUNNING:
  2114. if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
  2115. break;
  2116. /* Fall through */
  2117. case SNDRV_PCM_STATE_PREPARED:
  2118. case SNDRV_PCM_STATE_SUSPENDED:
  2119. err = 0;
  2120. break;
  2121. case SNDRV_PCM_STATE_XRUN:
  2122. err = -EPIPE;
  2123. break;
  2124. default:
  2125. __badfd:
  2126. err = -EBADFD;
  2127. break;
  2128. }
  2129. snd_pcm_stream_unlock_irq(substream);
  2130. return err;
  2131. }
  2132. static int snd_pcm_delay(snd_pcm_substream_t *substream, snd_pcm_sframes_t __user *res)
  2133. {
  2134. snd_pcm_runtime_t *runtime = substream->runtime;
  2135. int err;
  2136. snd_pcm_sframes_t n = 0;
  2137. snd_pcm_stream_lock_irq(substream);
  2138. switch (runtime->status->state) {
  2139. case SNDRV_PCM_STATE_DRAINING:
  2140. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  2141. goto __badfd;
  2142. case SNDRV_PCM_STATE_RUNNING:
  2143. if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
  2144. break;
  2145. /* Fall through */
  2146. case SNDRV_PCM_STATE_PREPARED:
  2147. case SNDRV_PCM_STATE_SUSPENDED:
  2148. err = 0;
  2149. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  2150. n = snd_pcm_playback_hw_avail(runtime);
  2151. else
  2152. n = snd_pcm_capture_avail(runtime);
  2153. break;
  2154. case SNDRV_PCM_STATE_XRUN:
  2155. err = -EPIPE;
  2156. break;
  2157. default:
  2158. __badfd:
  2159. err = -EBADFD;
  2160. break;
  2161. }
  2162. snd_pcm_stream_unlock_irq(substream);
  2163. if (!err)
  2164. if (put_user(n, res))
  2165. err = -EFAULT;
  2166. return err;
  2167. }
  2168. static int snd_pcm_sync_ptr(snd_pcm_substream_t *substream, struct sndrv_pcm_sync_ptr __user *_sync_ptr)
  2169. {
  2170. snd_pcm_runtime_t *runtime = substream->runtime;
  2171. struct sndrv_pcm_sync_ptr sync_ptr;
  2172. volatile struct sndrv_pcm_mmap_status *status;
  2173. volatile struct sndrv_pcm_mmap_control *control;
  2174. int err;
  2175. memset(&sync_ptr, 0, sizeof(sync_ptr));
  2176. if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
  2177. return -EFAULT;
  2178. if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct sndrv_pcm_mmap_control)))
  2179. return -EFAULT;
  2180. status = runtime->status;
  2181. control = runtime->control;
  2182. if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
  2183. err = snd_pcm_hwsync(substream);
  2184. if (err < 0)
  2185. return err;
  2186. }
  2187. snd_pcm_stream_lock_irq(substream);
  2188. if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
  2189. control->appl_ptr = sync_ptr.c.control.appl_ptr;
  2190. else
  2191. sync_ptr.c.control.appl_ptr = control->appl_ptr;
  2192. if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
  2193. control->avail_min = sync_ptr.c.control.avail_min;
  2194. else
  2195. sync_ptr.c.control.avail_min = control->avail_min;
  2196. sync_ptr.s.status.state = status->state;
  2197. sync_ptr.s.status.hw_ptr = status->hw_ptr;
  2198. sync_ptr.s.status.tstamp = status->tstamp;
  2199. sync_ptr.s.status.suspended_state = status->suspended_state;
  2200. snd_pcm_stream_unlock_irq(substream);
  2201. if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
  2202. return -EFAULT;
  2203. return 0;
  2204. }
  2205. static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
  2206. unsigned int cmd, void __user *arg);
  2207. static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
  2208. unsigned int cmd, void __user *arg);
  2209. static int snd_pcm_common_ioctl1(snd_pcm_substream_t *substream,
  2210. unsigned int cmd, void __user *arg)
  2211. {
  2212. snd_assert(substream != NULL, return -ENXIO);
  2213. switch (cmd) {
  2214. case SNDRV_PCM_IOCTL_PVERSION:
  2215. return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
  2216. case SNDRV_PCM_IOCTL_INFO:
  2217. return snd_pcm_info_user(substream, arg);
  2218. case SNDRV_PCM_IOCTL_TSTAMP:
  2219. {
  2220. int xarg;
  2221. if (get_user(xarg, (int __user *)arg))
  2222. return -EFAULT;
  2223. substream->runtime->tstamp_timespec = xarg ? 1 : 0;
  2224. return 0;
  2225. }
  2226. case SNDRV_PCM_IOCTL_HW_REFINE:
  2227. return snd_pcm_hw_refine_user(substream, arg);
  2228. case SNDRV_PCM_IOCTL_HW_PARAMS:
  2229. return snd_pcm_hw_params_user(substream, arg);
  2230. case SNDRV_PCM_IOCTL_HW_FREE:
  2231. return snd_pcm_hw_free(substream);
  2232. case SNDRV_PCM_IOCTL_SW_PARAMS:
  2233. return snd_pcm_sw_params_user(substream, arg);
  2234. case SNDRV_PCM_IOCTL_STATUS:
  2235. return snd_pcm_status_user(substream, arg);
  2236. case SNDRV_PCM_IOCTL_CHANNEL_INFO:
  2237. return snd_pcm_channel_info_user(substream, arg);
  2238. case SNDRV_PCM_IOCTL_PREPARE:
  2239. return snd_pcm_prepare(substream);
  2240. case SNDRV_PCM_IOCTL_RESET:
  2241. return snd_pcm_reset(substream);
  2242. case SNDRV_PCM_IOCTL_START:
  2243. return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
  2244. case SNDRV_PCM_IOCTL_LINK:
  2245. return snd_pcm_link(substream, (int)(unsigned long) arg);
  2246. case SNDRV_PCM_IOCTL_UNLINK:
  2247. return snd_pcm_unlink(substream);
  2248. case SNDRV_PCM_IOCTL_RESUME:
  2249. return snd_pcm_resume(substream);
  2250. case SNDRV_PCM_IOCTL_XRUN:
  2251. return snd_pcm_xrun(substream);
  2252. case SNDRV_PCM_IOCTL_HWSYNC:
  2253. return snd_pcm_hwsync(substream);
  2254. case SNDRV_PCM_IOCTL_DELAY:
  2255. return snd_pcm_delay(substream, arg);
  2256. case SNDRV_PCM_IOCTL_SYNC_PTR:
  2257. return snd_pcm_sync_ptr(substream, arg);
  2258. case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
  2259. return snd_pcm_hw_refine_old_user(substream, arg);
  2260. case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
  2261. return snd_pcm_hw_params_old_user(substream, arg);
  2262. case SNDRV_PCM_IOCTL_DRAIN:
  2263. return snd_pcm_drain(substream);
  2264. case SNDRV_PCM_IOCTL_DROP:
  2265. return snd_pcm_drop(substream);
  2266. }
  2267. snd_printd("unknown ioctl = 0x%x\n", cmd);
  2268. return -ENOTTY;
  2269. }
  2270. static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
  2271. unsigned int cmd, void __user *arg)
  2272. {
  2273. snd_assert(substream != NULL, return -ENXIO);
  2274. snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
  2275. switch (cmd) {
  2276. case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
  2277. {
  2278. snd_xferi_t xferi;
  2279. snd_xferi_t __user *_xferi = arg;
  2280. snd_pcm_runtime_t *runtime = substream->runtime;
  2281. snd_pcm_sframes_t result;
  2282. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2283. return -EBADFD;
  2284. if (put_user(0, &_xferi->result))
  2285. return -EFAULT;
  2286. if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
  2287. return -EFAULT;
  2288. result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
  2289. __put_user(result, &_xferi->result);
  2290. return result < 0 ? result : 0;
  2291. }
  2292. case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
  2293. {
  2294. snd_xfern_t xfern;
  2295. snd_xfern_t __user *_xfern = arg;
  2296. snd_pcm_runtime_t *runtime = substream->runtime;
  2297. void __user **bufs;
  2298. snd_pcm_sframes_t result;
  2299. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2300. return -EBADFD;
  2301. if (runtime->channels > 128)
  2302. return -EINVAL;
  2303. if (put_user(0, &_xfern->result))
  2304. return -EFAULT;
  2305. if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
  2306. return -EFAULT;
  2307. bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
  2308. if (bufs == NULL)
  2309. return -ENOMEM;
  2310. if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
  2311. kfree(bufs);
  2312. return -EFAULT;
  2313. }
  2314. result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
  2315. kfree(bufs);
  2316. __put_user(result, &_xfern->result);
  2317. return result < 0 ? result : 0;
  2318. }
  2319. case SNDRV_PCM_IOCTL_REWIND:
  2320. {
  2321. snd_pcm_uframes_t frames;
  2322. snd_pcm_uframes_t __user *_frames = arg;
  2323. snd_pcm_sframes_t result;
  2324. if (get_user(frames, _frames))
  2325. return -EFAULT;
  2326. if (put_user(0, _frames))
  2327. return -EFAULT;
  2328. result = snd_pcm_playback_rewind(substream, frames);
  2329. __put_user(result, _frames);
  2330. return result < 0 ? result : 0;
  2331. }
  2332. case SNDRV_PCM_IOCTL_FORWARD:
  2333. {
  2334. snd_pcm_uframes_t frames;
  2335. snd_pcm_uframes_t __user *_frames = arg;
  2336. snd_pcm_sframes_t result;
  2337. if (get_user(frames, _frames))
  2338. return -EFAULT;
  2339. if (put_user(0, _frames))
  2340. return -EFAULT;
  2341. result = snd_pcm_playback_forward(substream, frames);
  2342. __put_user(result, _frames);
  2343. return result < 0 ? result : 0;
  2344. }
  2345. case SNDRV_PCM_IOCTL_PAUSE:
  2346. {
  2347. int res;
  2348. snd_pcm_stream_lock_irq(substream);
  2349. res = snd_pcm_pause(substream, (int)(unsigned long)arg);
  2350. snd_pcm_stream_unlock_irq(substream);
  2351. return res;
  2352. }
  2353. }
  2354. return snd_pcm_common_ioctl1(substream, cmd, arg);
  2355. }
  2356. static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
  2357. unsigned int cmd, void __user *arg)
  2358. {
  2359. snd_assert(substream != NULL, return -ENXIO);
  2360. snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
  2361. switch (cmd) {
  2362. case SNDRV_PCM_IOCTL_READI_FRAMES:
  2363. {
  2364. snd_xferi_t xferi;
  2365. snd_xferi_t __user *_xferi = arg;
  2366. snd_pcm_runtime_t *runtime = substream->runtime;
  2367. snd_pcm_sframes_t result;
  2368. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2369. return -EBADFD;
  2370. if (put_user(0, &_xferi->result))
  2371. return -EFAULT;
  2372. if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
  2373. return -EFAULT;
  2374. result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
  2375. __put_user(result, &_xferi->result);
  2376. return result < 0 ? result : 0;
  2377. }
  2378. case SNDRV_PCM_IOCTL_READN_FRAMES:
  2379. {
  2380. snd_xfern_t xfern;
  2381. snd_xfern_t __user *_xfern = arg;
  2382. snd_pcm_runtime_t *runtime = substream->runtime;
  2383. void *bufs;
  2384. snd_pcm_sframes_t result;
  2385. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2386. return -EBADFD;
  2387. if (runtime->channels > 128)
  2388. return -EINVAL;
  2389. if (put_user(0, &_xfern->result))
  2390. return -EFAULT;
  2391. if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
  2392. return -EFAULT;
  2393. bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
  2394. if (bufs == NULL)
  2395. return -ENOMEM;
  2396. if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
  2397. kfree(bufs);
  2398. return -EFAULT;
  2399. }
  2400. result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
  2401. kfree(bufs);
  2402. __put_user(result, &_xfern->result);
  2403. return result < 0 ? result : 0;
  2404. }
  2405. case SNDRV_PCM_IOCTL_REWIND:
  2406. {
  2407. snd_pcm_uframes_t frames;
  2408. snd_pcm_uframes_t __user *_frames = arg;
  2409. snd_pcm_sframes_t result;
  2410. if (get_user(frames, _frames))
  2411. return -EFAULT;
  2412. if (put_user(0, _frames))
  2413. return -EFAULT;
  2414. result = snd_pcm_capture_rewind(substream, frames);
  2415. __put_user(result, _frames);
  2416. return result < 0 ? result : 0;
  2417. }
  2418. case SNDRV_PCM_IOCTL_FORWARD:
  2419. {
  2420. snd_pcm_uframes_t frames;
  2421. snd_pcm_uframes_t __user *_frames = arg;
  2422. snd_pcm_sframes_t result;
  2423. if (get_user(frames, _frames))
  2424. return -EFAULT;
  2425. if (put_user(0, _frames))
  2426. return -EFAULT;
  2427. result = snd_pcm_capture_forward(substream, frames);
  2428. __put_user(result, _frames);
  2429. return result < 0 ? result : 0;
  2430. }
  2431. }
  2432. return snd_pcm_common_ioctl1(substream, cmd, arg);
  2433. }
  2434. static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2435. {
  2436. snd_pcm_file_t *pcm_file;
  2437. pcm_file = file->private_data;
  2438. if (((cmd >> 8) & 0xff) != 'A')
  2439. return -ENOTTY;
  2440. return snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
  2441. }
  2442. static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2443. {
  2444. snd_pcm_file_t *pcm_file;
  2445. pcm_file = file->private_data;
  2446. if (((cmd >> 8) & 0xff) != 'A')
  2447. return -ENOTTY;
  2448. return snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
  2449. }
  2450. int snd_pcm_kernel_playback_ioctl(snd_pcm_substream_t *substream,
  2451. unsigned int cmd, void *arg)
  2452. {
  2453. mm_segment_t fs;
  2454. int result;
  2455. fs = snd_enter_user();
  2456. result = snd_pcm_playback_ioctl1(substream, cmd, (void __user *)arg);
  2457. snd_leave_user(fs);
  2458. return result;
  2459. }
  2460. int snd_pcm_kernel_capture_ioctl(snd_pcm_substream_t *substream,
  2461. unsigned int cmd, void *arg)
  2462. {
  2463. mm_segment_t fs;
  2464. int result;
  2465. fs = snd_enter_user();
  2466. result = snd_pcm_capture_ioctl1(substream, cmd, (void __user *)arg);
  2467. snd_leave_user(fs);
  2468. return result;
  2469. }
  2470. int snd_pcm_kernel_ioctl(snd_pcm_substream_t *substream,
  2471. unsigned int cmd, void *arg)
  2472. {
  2473. switch (substream->stream) {
  2474. case SNDRV_PCM_STREAM_PLAYBACK:
  2475. return snd_pcm_kernel_playback_ioctl(substream, cmd, arg);
  2476. case SNDRV_PCM_STREAM_CAPTURE:
  2477. return snd_pcm_kernel_capture_ioctl(substream, cmd, arg);
  2478. default:
  2479. return -EINVAL;
  2480. }
  2481. }
  2482. static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, loff_t * offset)
  2483. {
  2484. snd_pcm_file_t *pcm_file;
  2485. snd_pcm_substream_t *substream;
  2486. snd_pcm_runtime_t *runtime;
  2487. snd_pcm_sframes_t result;
  2488. pcm_file = file->private_data;
  2489. substream = pcm_file->substream;
  2490. snd_assert(substream != NULL, return -ENXIO);
  2491. runtime = substream->runtime;
  2492. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2493. return -EBADFD;
  2494. if (!frame_aligned(runtime, count))
  2495. return -EINVAL;
  2496. count = bytes_to_frames(runtime, count);
  2497. result = snd_pcm_lib_read(substream, buf, count);
  2498. if (result > 0)
  2499. result = frames_to_bytes(runtime, result);
  2500. return result;
  2501. }
  2502. static ssize_t snd_pcm_write(struct file *file, const char __user *buf, size_t count, loff_t * offset)
  2503. {
  2504. snd_pcm_file_t *pcm_file;
  2505. snd_pcm_substream_t *substream;
  2506. snd_pcm_runtime_t *runtime;
  2507. snd_pcm_sframes_t result;
  2508. pcm_file = file->private_data;
  2509. substream = pcm_file->substream;
  2510. snd_assert(substream != NULL, result = -ENXIO; goto end);
  2511. runtime = substream->runtime;
  2512. if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  2513. result = -EBADFD;
  2514. goto end;
  2515. }
  2516. if (!frame_aligned(runtime, count)) {
  2517. result = -EINVAL;
  2518. goto end;
  2519. }
  2520. count = bytes_to_frames(runtime, count);
  2521. result = snd_pcm_lib_write(substream, buf, count);
  2522. if (result > 0)
  2523. result = frames_to_bytes(runtime, result);
  2524. end:
  2525. return result;
  2526. }
  2527. static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
  2528. unsigned long count, loff_t * offset)
  2529. {
  2530. snd_pcm_file_t *pcm_file;
  2531. snd_pcm_substream_t *substream;
  2532. snd_pcm_runtime_t *runtime;
  2533. snd_pcm_sframes_t result;
  2534. unsigned long i;
  2535. void __user **bufs;
  2536. snd_pcm_uframes_t frames;
  2537. pcm_file = file->private_data;
  2538. substream = pcm_file->substream;
  2539. snd_assert(substream != NULL, return -ENXIO);
  2540. runtime = substream->runtime;
  2541. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2542. return -EBADFD;
  2543. if (count > 1024 || count != runtime->channels)
  2544. return -EINVAL;
  2545. if (!frame_aligned(runtime, _vector->iov_len))
  2546. return -EINVAL;
  2547. frames = bytes_to_samples(runtime, _vector->iov_len);
  2548. bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
  2549. if (bufs == NULL)
  2550. return -ENOMEM;
  2551. for (i = 0; i < count; ++i)
  2552. bufs[i] = _vector[i].iov_base;
  2553. result = snd_pcm_lib_readv(substream, bufs, frames);
  2554. if (result > 0)
  2555. result = frames_to_bytes(runtime, result);
  2556. kfree(bufs);
  2557. return result;
  2558. }
  2559. static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
  2560. unsigned long count, loff_t * offset)
  2561. {
  2562. snd_pcm_file_t *pcm_file;
  2563. snd_pcm_substream_t *substream;
  2564. snd_pcm_runtime_t *runtime;
  2565. snd_pcm_sframes_t result;
  2566. unsigned long i;
  2567. void __user **bufs;
  2568. snd_pcm_uframes_t frames;
  2569. pcm_file = file->private_data;
  2570. substream = pcm_file->substream;
  2571. snd_assert(substream != NULL, result = -ENXIO; goto end);
  2572. runtime = substream->runtime;
  2573. if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  2574. result = -EBADFD;
  2575. goto end;
  2576. }
  2577. if (count > 128 || count != runtime->channels ||
  2578. !frame_aligned(runtime, _vector->iov_len)) {
  2579. result = -EINVAL;
  2580. goto end;
  2581. }
  2582. frames = bytes_to_samples(runtime, _vector->iov_len);
  2583. bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
  2584. if (bufs == NULL)
  2585. return -ENOMEM;
  2586. for (i = 0; i < count; ++i)
  2587. bufs[i] = _vector[i].iov_base;
  2588. result = snd_pcm_lib_writev(substream, bufs, frames);
  2589. if (result > 0)
  2590. result = frames_to_bytes(runtime, result);
  2591. kfree(bufs);
  2592. end:
  2593. return result;
  2594. }
  2595. static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
  2596. {
  2597. snd_pcm_file_t *pcm_file;
  2598. snd_pcm_substream_t *substream;
  2599. snd_pcm_runtime_t *runtime;
  2600. unsigned int mask;
  2601. snd_pcm_uframes_t avail;
  2602. pcm_file = file->private_data;
  2603. substream = pcm_file->substream;
  2604. snd_assert(substream != NULL, return -ENXIO);
  2605. runtime = substream->runtime;
  2606. poll_wait(file, &runtime->sleep, wait);
  2607. snd_pcm_stream_lock_irq(substream);
  2608. avail = snd_pcm_playback_avail(runtime);
  2609. switch (runtime->status->state) {
  2610. case SNDRV_PCM_STATE_RUNNING:
  2611. case SNDRV_PCM_STATE_PREPARED:
  2612. case SNDRV_PCM_STATE_PAUSED:
  2613. if (avail >= runtime->control->avail_min) {
  2614. mask = POLLOUT | POLLWRNORM;
  2615. break;
  2616. }
  2617. /* Fall through */
  2618. case SNDRV_PCM_STATE_DRAINING:
  2619. mask = 0;
  2620. break;
  2621. default:
  2622. mask = POLLOUT | POLLWRNORM | POLLERR;
  2623. break;
  2624. }
  2625. snd_pcm_stream_unlock_irq(substream);
  2626. return mask;
  2627. }
  2628. static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
  2629. {
  2630. snd_pcm_file_t *pcm_file;
  2631. snd_pcm_substream_t *substream;
  2632. snd_pcm_runtime_t *runtime;
  2633. unsigned int mask;
  2634. snd_pcm_uframes_t avail;
  2635. pcm_file = file->private_data;
  2636. substream = pcm_file->substream;
  2637. snd_assert(substream != NULL, return -ENXIO);
  2638. runtime = substream->runtime;
  2639. poll_wait(file, &runtime->sleep, wait);
  2640. snd_pcm_stream_lock_irq(substream);
  2641. avail = snd_pcm_capture_avail(runtime);
  2642. switch (runtime->status->state) {
  2643. case SNDRV_PCM_STATE_RUNNING:
  2644. case SNDRV_PCM_STATE_PREPARED:
  2645. case SNDRV_PCM_STATE_PAUSED:
  2646. if (avail >= runtime->control->avail_min) {
  2647. mask = POLLIN | POLLRDNORM;
  2648. break;
  2649. }
  2650. mask = 0;
  2651. break;
  2652. case SNDRV_PCM_STATE_DRAINING:
  2653. if (avail > 0) {
  2654. mask = POLLIN | POLLRDNORM;
  2655. break;
  2656. }
  2657. /* Fall through */
  2658. default:
  2659. mask = POLLIN | POLLRDNORM | POLLERR;
  2660. break;
  2661. }
  2662. snd_pcm_stream_unlock_irq(substream);
  2663. return mask;
  2664. }
  2665. /*
  2666. * mmap support
  2667. */
  2668. /*
  2669. * Only on coherent architectures, we can mmap the status and the control records
  2670. * for effcient data transfer. On others, we have to use HWSYNC ioctl...
  2671. */
  2672. #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
  2673. /*
  2674. * mmap status record
  2675. */
  2676. static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area, unsigned long address, int *type)
  2677. {
  2678. snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
  2679. snd_pcm_runtime_t *runtime;
  2680. struct page * page;
  2681. if (substream == NULL)
  2682. return NOPAGE_OOM;
  2683. runtime = substream->runtime;
  2684. page = virt_to_page(runtime->status);
  2685. if (!PageReserved(page))
  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. if (!PageReserved(page))
  2725. get_page(page);
  2726. if (type)
  2727. *type = VM_FAULT_MINOR;
  2728. return page;
  2729. }
  2730. static struct vm_operations_struct snd_pcm_vm_ops_control =
  2731. {
  2732. .nopage = snd_pcm_mmap_control_nopage,
  2733. };
  2734. static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
  2735. struct vm_area_struct *area)
  2736. {
  2737. snd_pcm_runtime_t *runtime;
  2738. long size;
  2739. if (!(area->vm_flags & VM_READ))
  2740. return -EINVAL;
  2741. runtime = substream->runtime;
  2742. snd_assert(runtime != NULL, return -EAGAIN);
  2743. size = area->vm_end - area->vm_start;
  2744. if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t)))
  2745. return -EINVAL;
  2746. area->vm_ops = &snd_pcm_vm_ops_control;
  2747. area->vm_private_data = substream;
  2748. area->vm_flags |= VM_RESERVED;
  2749. return 0;
  2750. }
  2751. #else /* ! coherent mmap */
  2752. /*
  2753. * don't support mmap for status and control records.
  2754. */
  2755. static int snd_pcm_mmap_status(snd_pcm_substream_t *substream, struct file *file,
  2756. struct vm_area_struct *area)
  2757. {
  2758. return -ENXIO;
  2759. }
  2760. static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
  2761. struct vm_area_struct *area)
  2762. {
  2763. return -ENXIO;
  2764. }
  2765. #endif /* coherent mmap */
  2766. /*
  2767. * nopage callback for mmapping a RAM page
  2768. */
  2769. static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area, unsigned long address, int *type)
  2770. {
  2771. snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
  2772. snd_pcm_runtime_t *runtime;
  2773. unsigned long offset;
  2774. struct page * page;
  2775. void *vaddr;
  2776. size_t dma_bytes;
  2777. if (substream == NULL)
  2778. return NOPAGE_OOM;
  2779. runtime = substream->runtime;
  2780. offset = area->vm_pgoff << PAGE_SHIFT;
  2781. offset += address - area->vm_start;
  2782. snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
  2783. dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
  2784. if (offset > dma_bytes - PAGE_SIZE)
  2785. return NOPAGE_SIGBUS;
  2786. if (substream->ops->page) {
  2787. page = substream->ops->page(substream, offset);
  2788. if (! page)
  2789. return NOPAGE_OOM;
  2790. } else {
  2791. vaddr = runtime->dma_area + offset;
  2792. page = virt_to_page(vaddr);
  2793. }
  2794. if (!PageReserved(page))
  2795. get_page(page);
  2796. if (type)
  2797. *type = VM_FAULT_MINOR;
  2798. return page;
  2799. }
  2800. static struct vm_operations_struct snd_pcm_vm_ops_data =
  2801. {
  2802. .open = snd_pcm_mmap_data_open,
  2803. .close = snd_pcm_mmap_data_close,
  2804. .nopage = snd_pcm_mmap_data_nopage,
  2805. };
  2806. /*
  2807. * mmap the DMA buffer on RAM
  2808. */
  2809. static int snd_pcm_default_mmap(snd_pcm_substream_t *substream, struct vm_area_struct *area)
  2810. {
  2811. area->vm_ops = &snd_pcm_vm_ops_data;
  2812. area->vm_private_data = substream;
  2813. area->vm_flags |= VM_RESERVED;
  2814. atomic_inc(&substream->runtime->mmap_count);
  2815. return 0;
  2816. }
  2817. /*
  2818. * mmap the DMA buffer on I/O memory area
  2819. */
  2820. #if SNDRV_PCM_INFO_MMAP_IOMEM
  2821. static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
  2822. {
  2823. .open = snd_pcm_mmap_data_open,
  2824. .close = snd_pcm_mmap_data_close,
  2825. };
  2826. int snd_pcm_lib_mmap_iomem(snd_pcm_substream_t *substream, struct vm_area_struct *area)
  2827. {
  2828. long size;
  2829. unsigned long offset;
  2830. #ifdef pgprot_noncached
  2831. area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
  2832. #endif
  2833. area->vm_ops = &snd_pcm_vm_ops_data_mmio;
  2834. area->vm_private_data = substream;
  2835. area->vm_flags |= VM_IO;
  2836. size = area->vm_end - area->vm_start;
  2837. offset = area->vm_pgoff << PAGE_SHIFT;
  2838. if (io_remap_pfn_range(area, area->vm_start,
  2839. (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
  2840. size, area->vm_page_prot))
  2841. return -EAGAIN;
  2842. atomic_inc(&substream->runtime->mmap_count);
  2843. return 0;
  2844. }
  2845. #endif /* SNDRV_PCM_INFO_MMAP */
  2846. /*
  2847. * mmap DMA buffer
  2848. */
  2849. int snd_pcm_mmap_data(snd_pcm_substream_t *substream, struct file *file,
  2850. struct vm_area_struct *area)
  2851. {
  2852. snd_pcm_runtime_t *runtime;
  2853. long size;
  2854. unsigned long offset;
  2855. size_t dma_bytes;
  2856. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2857. if (!(area->vm_flags & (VM_WRITE|VM_READ)))
  2858. return -EINVAL;
  2859. } else {
  2860. if (!(area->vm_flags & VM_READ))
  2861. return -EINVAL;
  2862. }
  2863. runtime = substream->runtime;
  2864. snd_assert(runtime != NULL, return -EAGAIN);
  2865. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2866. return -EBADFD;
  2867. if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
  2868. return -ENXIO;
  2869. if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
  2870. runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  2871. return -EINVAL;
  2872. size = area->vm_end - area->vm_start;
  2873. offset = area->vm_pgoff << PAGE_SHIFT;
  2874. dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
  2875. if ((size_t)size > dma_bytes)
  2876. return -EINVAL;
  2877. if (offset > dma_bytes - size)
  2878. return -EINVAL;
  2879. if (substream->ops->mmap)
  2880. return substream->ops->mmap(substream, area);
  2881. else
  2882. return snd_pcm_default_mmap(substream, area);
  2883. }
  2884. static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
  2885. {
  2886. snd_pcm_file_t * pcm_file;
  2887. snd_pcm_substream_t *substream;
  2888. unsigned long offset;
  2889. pcm_file = file->private_data;
  2890. substream = pcm_file->substream;
  2891. snd_assert(substream != NULL, return -ENXIO);
  2892. offset = area->vm_pgoff << PAGE_SHIFT;
  2893. switch (offset) {
  2894. case SNDRV_PCM_MMAP_OFFSET_STATUS:
  2895. if (substream->no_mmap_ctrl)
  2896. return -ENXIO;
  2897. return snd_pcm_mmap_status(substream, file, area);
  2898. case SNDRV_PCM_MMAP_OFFSET_CONTROL:
  2899. if (substream->no_mmap_ctrl)
  2900. return -ENXIO;
  2901. return snd_pcm_mmap_control(substream, file, area);
  2902. default:
  2903. return snd_pcm_mmap_data(substream, file, area);
  2904. }
  2905. return 0;
  2906. }
  2907. static int snd_pcm_fasync(int fd, struct file * file, int on)
  2908. {
  2909. snd_pcm_file_t * pcm_file;
  2910. snd_pcm_substream_t *substream;
  2911. snd_pcm_runtime_t *runtime;
  2912. int err;
  2913. pcm_file = file->private_data;
  2914. substream = pcm_file->substream;
  2915. snd_assert(substream != NULL, return -ENXIO);
  2916. runtime = substream->runtime;
  2917. err = fasync_helper(fd, file, on, &runtime->fasync);
  2918. if (err < 0)
  2919. return err;
  2920. return 0;
  2921. }
  2922. /*
  2923. * ioctl32 compat
  2924. */
  2925. #ifdef CONFIG_COMPAT
  2926. #include "pcm_compat.c"
  2927. #else
  2928. #define snd_pcm_ioctl_compat NULL
  2929. #endif
  2930. /*
  2931. * To be removed helpers to keep binary compatibility
  2932. */
  2933. #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
  2934. #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
  2935. static void snd_pcm_hw_convert_from_old_params(snd_pcm_hw_params_t *params, struct sndrv_pcm_hw_params_old *oparams)
  2936. {
  2937. unsigned int i;
  2938. memset(params, 0, sizeof(*params));
  2939. params->flags = oparams->flags;
  2940. for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
  2941. params->masks[i].bits[0] = oparams->masks[i];
  2942. memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
  2943. params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
  2944. params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
  2945. params->info = oparams->info;
  2946. params->msbits = oparams->msbits;
  2947. params->rate_num = oparams->rate_num;
  2948. params->rate_den = oparams->rate_den;
  2949. params->fifo_size = oparams->fifo_size;
  2950. }
  2951. static void snd_pcm_hw_convert_to_old_params(struct sndrv_pcm_hw_params_old *oparams, snd_pcm_hw_params_t *params)
  2952. {
  2953. unsigned int i;
  2954. memset(oparams, 0, sizeof(*oparams));
  2955. oparams->flags = params->flags;
  2956. for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
  2957. oparams->masks[i] = params->masks[i].bits[0];
  2958. memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
  2959. oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
  2960. oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
  2961. oparams->info = params->info;
  2962. oparams->msbits = params->msbits;
  2963. oparams->rate_num = params->rate_num;
  2964. oparams->rate_den = params->rate_den;
  2965. oparams->fifo_size = params->fifo_size;
  2966. }
  2967. static int snd_pcm_hw_refine_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old __user * _oparams)
  2968. {
  2969. snd_pcm_hw_params_t *params;
  2970. struct sndrv_pcm_hw_params_old *oparams = NULL;
  2971. int err;
  2972. params = kmalloc(sizeof(*params), GFP_KERNEL);
  2973. if (!params) {
  2974. err = -ENOMEM;
  2975. goto out;
  2976. }
  2977. oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
  2978. if (!oparams) {
  2979. err = -ENOMEM;
  2980. goto out;
  2981. }
  2982. if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
  2983. err = -EFAULT;
  2984. goto out;
  2985. }
  2986. snd_pcm_hw_convert_from_old_params(params, oparams);
  2987. err = snd_pcm_hw_refine(substream, params);
  2988. snd_pcm_hw_convert_to_old_params(oparams, params);
  2989. if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
  2990. if (!err)
  2991. err = -EFAULT;
  2992. }
  2993. out:
  2994. kfree(params);
  2995. kfree(oparams);
  2996. return err;
  2997. }
  2998. static int snd_pcm_hw_params_old_user(snd_pcm_substream_t * substream, struct sndrv_pcm_hw_params_old __user * _oparams)
  2999. {
  3000. snd_pcm_hw_params_t *params;
  3001. struct sndrv_pcm_hw_params_old *oparams = NULL;
  3002. int err;
  3003. params = kmalloc(sizeof(*params), GFP_KERNEL);
  3004. if (!params) {
  3005. err = -ENOMEM;
  3006. goto out;
  3007. }
  3008. oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
  3009. if (!oparams) {
  3010. err = -ENOMEM;
  3011. goto out;
  3012. }
  3013. if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
  3014. err = -EFAULT;
  3015. goto out;
  3016. }
  3017. snd_pcm_hw_convert_from_old_params(params, oparams);
  3018. err = snd_pcm_hw_params(substream, params);
  3019. snd_pcm_hw_convert_to_old_params(oparams, params);
  3020. if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
  3021. if (!err)
  3022. err = -EFAULT;
  3023. }
  3024. out:
  3025. kfree(params);
  3026. kfree(oparams);
  3027. return err;
  3028. }
  3029. /*
  3030. * Register section
  3031. */
  3032. static struct file_operations snd_pcm_f_ops_playback = {
  3033. .owner = THIS_MODULE,
  3034. .write = snd_pcm_write,
  3035. .writev = snd_pcm_writev,
  3036. .open = snd_pcm_open,
  3037. .release = snd_pcm_release,
  3038. .poll = snd_pcm_playback_poll,
  3039. .unlocked_ioctl = snd_pcm_playback_ioctl,
  3040. .compat_ioctl = snd_pcm_ioctl_compat,
  3041. .mmap = snd_pcm_mmap,
  3042. .fasync = snd_pcm_fasync,
  3043. };
  3044. static struct file_operations snd_pcm_f_ops_capture = {
  3045. .owner = THIS_MODULE,
  3046. .read = snd_pcm_read,
  3047. .readv = snd_pcm_readv,
  3048. .open = snd_pcm_open,
  3049. .release = snd_pcm_release,
  3050. .poll = snd_pcm_capture_poll,
  3051. .unlocked_ioctl = snd_pcm_capture_ioctl,
  3052. .compat_ioctl = snd_pcm_ioctl_compat,
  3053. .mmap = snd_pcm_mmap,
  3054. .fasync = snd_pcm_fasync,
  3055. };
  3056. snd_minor_t snd_pcm_reg[2] =
  3057. {
  3058. {
  3059. .comment = "digital audio playback",
  3060. .f_ops = &snd_pcm_f_ops_playback,
  3061. },
  3062. {
  3063. .comment = "digital audio capture",
  3064. .f_ops = &snd_pcm_f_ops_capture,
  3065. }
  3066. };