pcm_native.c 104 KB

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