pcm_native.c 96 KB

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