pcm_native.c 101 KB

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