pcm_native.c 99 KB

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