pcm_native.c 102 KB

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