pcm_native.c 104 KB

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