pcm_native.c 105 KB

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