pcm_native.c 103 KB

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