pcm_native.c 96 KB

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