pcm_native.c 95 KB

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