pcm_native.c 101 KB

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