pcm_native.c 103 KB

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