pcm_native.c 101 KB

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