pcm_native.c 95 KB

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