pcm_native.c 99 KB

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