pcm_native.c 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707
  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_entry_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. struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
  1759. struct snd_mask m;
  1760. struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  1761. snd_mask_any(&m);
  1762. for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
  1763. int bits;
  1764. if (! snd_mask_test(mask, k))
  1765. continue;
  1766. bits = snd_pcm_format_physical_width(k);
  1767. if (bits <= 0)
  1768. continue; /* ignore invalid formats */
  1769. if ((unsigned)bits < i->min || (unsigned)bits > i->max)
  1770. snd_mask_reset(&m, k);
  1771. }
  1772. return snd_mask_refine(mask, &m);
  1773. }
  1774. static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
  1775. struct snd_pcm_hw_rule *rule)
  1776. {
  1777. struct snd_interval t;
  1778. unsigned int k;
  1779. t.min = UINT_MAX;
  1780. t.max = 0;
  1781. t.openmin = 0;
  1782. t.openmax = 0;
  1783. for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
  1784. int bits;
  1785. if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
  1786. continue;
  1787. bits = snd_pcm_format_physical_width(k);
  1788. if (bits <= 0)
  1789. continue; /* ignore invalid formats */
  1790. if (t.min > (unsigned)bits)
  1791. t.min = bits;
  1792. if (t.max < (unsigned)bits)
  1793. t.max = bits;
  1794. }
  1795. t.integer = 1;
  1796. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1797. }
  1798. #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
  1799. #error "Change this table"
  1800. #endif
  1801. static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
  1802. 48000, 64000, 88200, 96000, 176400, 192000 };
  1803. const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
  1804. .count = ARRAY_SIZE(rates),
  1805. .list = rates,
  1806. };
  1807. static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
  1808. struct snd_pcm_hw_rule *rule)
  1809. {
  1810. struct snd_pcm_hardware *hw = rule->private;
  1811. return snd_interval_list(hw_param_interval(params, rule->var),
  1812. snd_pcm_known_rates.count,
  1813. snd_pcm_known_rates.list, hw->rates);
  1814. }
  1815. static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
  1816. struct snd_pcm_hw_rule *rule)
  1817. {
  1818. struct snd_interval t;
  1819. struct snd_pcm_substream *substream = rule->private;
  1820. t.min = 0;
  1821. t.max = substream->buffer_bytes_max;
  1822. t.openmin = 0;
  1823. t.openmax = 0;
  1824. t.integer = 1;
  1825. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1826. }
  1827. int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
  1828. {
  1829. struct snd_pcm_runtime *runtime = substream->runtime;
  1830. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1831. int k, err;
  1832. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
  1833. snd_mask_any(constrs_mask(constrs, k));
  1834. }
  1835. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
  1836. snd_interval_any(constrs_interval(constrs, k));
  1837. }
  1838. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
  1839. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
  1840. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
  1841. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
  1842. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
  1843. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  1844. snd_pcm_hw_rule_format, NULL,
  1845. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1846. if (err < 0)
  1847. return err;
  1848. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  1849. snd_pcm_hw_rule_sample_bits, NULL,
  1850. SNDRV_PCM_HW_PARAM_FORMAT,
  1851. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1852. if (err < 0)
  1853. return err;
  1854. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  1855. snd_pcm_hw_rule_div, NULL,
  1856. SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  1857. if (err < 0)
  1858. return err;
  1859. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1860. snd_pcm_hw_rule_mul, NULL,
  1861. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  1862. if (err < 0)
  1863. return err;
  1864. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1865. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1866. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
  1867. if (err < 0)
  1868. return err;
  1869. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1870. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1871. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
  1872. if (err < 0)
  1873. return err;
  1874. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  1875. snd_pcm_hw_rule_div, NULL,
  1876. SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1877. if (err < 0)
  1878. return err;
  1879. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1880. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1881. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
  1882. if (err < 0)
  1883. return err;
  1884. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1885. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1886. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
  1887. if (err < 0)
  1888. return err;
  1889. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
  1890. snd_pcm_hw_rule_div, NULL,
  1891. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
  1892. if (err < 0)
  1893. return err;
  1894. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1895. snd_pcm_hw_rule_div, NULL,
  1896. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
  1897. if (err < 0)
  1898. return err;
  1899. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1900. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1901. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1902. if (err < 0)
  1903. return err;
  1904. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1905. snd_pcm_hw_rule_muldivk, (void*) 1000000,
  1906. SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
  1907. if (err < 0)
  1908. return err;
  1909. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1910. snd_pcm_hw_rule_mul, NULL,
  1911. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
  1912. if (err < 0)
  1913. return err;
  1914. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1915. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1916. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1917. if (err < 0)
  1918. return err;
  1919. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1920. snd_pcm_hw_rule_muldivk, (void*) 1000000,
  1921. SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
  1922. if (err < 0)
  1923. return err;
  1924. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1925. snd_pcm_hw_rule_muldivk, (void*) 8,
  1926. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1927. if (err < 0)
  1928. return err;
  1929. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1930. snd_pcm_hw_rule_muldivk, (void*) 8,
  1931. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1932. if (err < 0)
  1933. return err;
  1934. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1935. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1936. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
  1937. if (err < 0)
  1938. return err;
  1939. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
  1940. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1941. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
  1942. if (err < 0)
  1943. return err;
  1944. return 0;
  1945. }
  1946. int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
  1947. {
  1948. struct snd_pcm_runtime *runtime = substream->runtime;
  1949. struct snd_pcm_hardware *hw = &runtime->hw;
  1950. int err;
  1951. unsigned int mask = 0;
  1952. if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
  1953. mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  1954. if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
  1955. mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
  1956. if (hw_support_mmap(substream)) {
  1957. if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
  1958. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
  1959. if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
  1960. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
  1961. if (hw->info & SNDRV_PCM_INFO_COMPLEX)
  1962. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
  1963. }
  1964. err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
  1965. if (err < 0)
  1966. return err;
  1967. err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
  1968. if (err < 0)
  1969. return err;
  1970. err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
  1971. if (err < 0)
  1972. return err;
  1973. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
  1974. hw->channels_min, hw->channels_max);
  1975. if (err < 0)
  1976. return err;
  1977. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
  1978. hw->rate_min, hw->rate_max);
  1979. if (err < 0)
  1980. return err;
  1981. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1982. hw->period_bytes_min, hw->period_bytes_max);
  1983. if (err < 0)
  1984. return err;
  1985. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
  1986. hw->periods_min, hw->periods_max);
  1987. if (err < 0)
  1988. return err;
  1989. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1990. hw->period_bytes_min, hw->buffer_bytes_max);
  1991. if (err < 0)
  1992. return err;
  1993. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1994. snd_pcm_hw_rule_buffer_bytes_max, substream,
  1995. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
  1996. if (err < 0)
  1997. return err;
  1998. /* FIXME: remove */
  1999. if (runtime->dma_bytes) {
  2000. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
  2001. if (err < 0)
  2002. return err;
  2003. }
  2004. if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
  2005. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  2006. snd_pcm_hw_rule_rate, hw,
  2007. SNDRV_PCM_HW_PARAM_RATE, -1);
  2008. if (err < 0)
  2009. return err;
  2010. }
  2011. /* FIXME: this belong to lowlevel */
  2012. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
  2013. return 0;
  2014. }
  2015. static void pcm_release_private(struct snd_pcm_substream *substream)
  2016. {
  2017. snd_pcm_unlink(substream);
  2018. }
  2019. void snd_pcm_release_substream(struct snd_pcm_substream *substream)
  2020. {
  2021. substream->ref_count--;
  2022. if (substream->ref_count > 0)
  2023. return;
  2024. snd_pcm_drop(substream);
  2025. if (substream->hw_opened) {
  2026. if (substream->ops->hw_free &&
  2027. substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
  2028. substream->ops->hw_free(substream);
  2029. substream->ops->close(substream);
  2030. substream->hw_opened = 0;
  2031. }
  2032. if (pm_qos_request_active(&substream->latency_pm_qos_req))
  2033. pm_qos_remove_request(&substream->latency_pm_qos_req);
  2034. if (substream->pcm_release) {
  2035. substream->pcm_release(substream);
  2036. substream->pcm_release = NULL;
  2037. }
  2038. snd_pcm_detach_substream(substream);
  2039. }
  2040. EXPORT_SYMBOL(snd_pcm_release_substream);
  2041. int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
  2042. struct file *file,
  2043. struct snd_pcm_substream **rsubstream)
  2044. {
  2045. struct snd_pcm_substream *substream;
  2046. int err;
  2047. err = snd_pcm_attach_substream(pcm, stream, file, &substream);
  2048. if (err < 0)
  2049. return err;
  2050. if (substream->ref_count > 1) {
  2051. *rsubstream = substream;
  2052. return 0;
  2053. }
  2054. err = snd_pcm_hw_constraints_init(substream);
  2055. if (err < 0) {
  2056. pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
  2057. goto error;
  2058. }
  2059. if ((err = substream->ops->open(substream)) < 0)
  2060. goto error;
  2061. substream->hw_opened = 1;
  2062. err = snd_pcm_hw_constraints_complete(substream);
  2063. if (err < 0) {
  2064. pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
  2065. goto error;
  2066. }
  2067. *rsubstream = substream;
  2068. return 0;
  2069. error:
  2070. snd_pcm_release_substream(substream);
  2071. return err;
  2072. }
  2073. EXPORT_SYMBOL(snd_pcm_open_substream);
  2074. static int snd_pcm_open_file(struct file *file,
  2075. struct snd_pcm *pcm,
  2076. int stream)
  2077. {
  2078. struct snd_pcm_file *pcm_file;
  2079. struct snd_pcm_substream *substream;
  2080. int err;
  2081. err = snd_pcm_open_substream(pcm, stream, file, &substream);
  2082. if (err < 0)
  2083. return err;
  2084. pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
  2085. if (pcm_file == NULL) {
  2086. snd_pcm_release_substream(substream);
  2087. return -ENOMEM;
  2088. }
  2089. pcm_file->substream = substream;
  2090. if (substream->ref_count == 1) {
  2091. substream->file = pcm_file;
  2092. substream->pcm_release = pcm_release_private;
  2093. }
  2094. file->private_data = pcm_file;
  2095. return 0;
  2096. }
  2097. static int snd_pcm_playback_open(struct inode *inode, struct file *file)
  2098. {
  2099. struct snd_pcm *pcm;
  2100. int err = nonseekable_open(inode, file);
  2101. if (err < 0)
  2102. return err;
  2103. pcm = snd_lookup_minor_data(iminor(inode),
  2104. SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
  2105. err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
  2106. if (pcm)
  2107. snd_card_unref(pcm->card);
  2108. return err;
  2109. }
  2110. static int snd_pcm_capture_open(struct inode *inode, struct file *file)
  2111. {
  2112. struct snd_pcm *pcm;
  2113. int err = nonseekable_open(inode, file);
  2114. if (err < 0)
  2115. return err;
  2116. pcm = snd_lookup_minor_data(iminor(inode),
  2117. SNDRV_DEVICE_TYPE_PCM_CAPTURE);
  2118. err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
  2119. if (pcm)
  2120. snd_card_unref(pcm->card);
  2121. return err;
  2122. }
  2123. static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
  2124. {
  2125. int err;
  2126. wait_queue_entry_t wait;
  2127. if (pcm == NULL) {
  2128. err = -ENODEV;
  2129. goto __error1;
  2130. }
  2131. err = snd_card_file_add(pcm->card, file);
  2132. if (err < 0)
  2133. goto __error1;
  2134. if (!try_module_get(pcm->card->module)) {
  2135. err = -EFAULT;
  2136. goto __error2;
  2137. }
  2138. init_waitqueue_entry(&wait, current);
  2139. add_wait_queue(&pcm->open_wait, &wait);
  2140. mutex_lock(&pcm->open_mutex);
  2141. while (1) {
  2142. err = snd_pcm_open_file(file, pcm, stream);
  2143. if (err >= 0)
  2144. break;
  2145. if (err == -EAGAIN) {
  2146. if (file->f_flags & O_NONBLOCK) {
  2147. err = -EBUSY;
  2148. break;
  2149. }
  2150. } else
  2151. break;
  2152. set_current_state(TASK_INTERRUPTIBLE);
  2153. mutex_unlock(&pcm->open_mutex);
  2154. schedule();
  2155. mutex_lock(&pcm->open_mutex);
  2156. if (pcm->card->shutdown) {
  2157. err = -ENODEV;
  2158. break;
  2159. }
  2160. if (signal_pending(current)) {
  2161. err = -ERESTARTSYS;
  2162. break;
  2163. }
  2164. }
  2165. remove_wait_queue(&pcm->open_wait, &wait);
  2166. mutex_unlock(&pcm->open_mutex);
  2167. if (err < 0)
  2168. goto __error;
  2169. return err;
  2170. __error:
  2171. module_put(pcm->card->module);
  2172. __error2:
  2173. snd_card_file_remove(pcm->card, file);
  2174. __error1:
  2175. return err;
  2176. }
  2177. static int snd_pcm_release(struct inode *inode, struct file *file)
  2178. {
  2179. struct snd_pcm *pcm;
  2180. struct snd_pcm_substream *substream;
  2181. struct snd_pcm_file *pcm_file;
  2182. pcm_file = file->private_data;
  2183. substream = pcm_file->substream;
  2184. if (snd_BUG_ON(!substream))
  2185. return -ENXIO;
  2186. pcm = substream->pcm;
  2187. mutex_lock(&pcm->open_mutex);
  2188. snd_pcm_release_substream(substream);
  2189. kfree(pcm_file);
  2190. mutex_unlock(&pcm->open_mutex);
  2191. wake_up(&pcm->open_wait);
  2192. module_put(pcm->card->module);
  2193. snd_card_file_remove(pcm->card, file);
  2194. return 0;
  2195. }
  2196. static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
  2197. snd_pcm_uframes_t frames)
  2198. {
  2199. struct snd_pcm_runtime *runtime = substream->runtime;
  2200. snd_pcm_sframes_t appl_ptr;
  2201. snd_pcm_sframes_t ret;
  2202. snd_pcm_sframes_t hw_avail;
  2203. if (frames == 0)
  2204. return 0;
  2205. snd_pcm_stream_lock_irq(substream);
  2206. switch (runtime->status->state) {
  2207. case SNDRV_PCM_STATE_PREPARED:
  2208. break;
  2209. case SNDRV_PCM_STATE_DRAINING:
  2210. case SNDRV_PCM_STATE_RUNNING:
  2211. if (snd_pcm_update_hw_ptr(substream) >= 0)
  2212. break;
  2213. /* Fall through */
  2214. case SNDRV_PCM_STATE_XRUN:
  2215. ret = -EPIPE;
  2216. goto __end;
  2217. case SNDRV_PCM_STATE_SUSPENDED:
  2218. ret = -ESTRPIPE;
  2219. goto __end;
  2220. default:
  2221. ret = -EBADFD;
  2222. goto __end;
  2223. }
  2224. hw_avail = snd_pcm_playback_hw_avail(runtime);
  2225. if (hw_avail <= 0) {
  2226. ret = 0;
  2227. goto __end;
  2228. }
  2229. if (frames > (snd_pcm_uframes_t)hw_avail)
  2230. frames = hw_avail;
  2231. appl_ptr = runtime->control->appl_ptr - frames;
  2232. if (appl_ptr < 0)
  2233. appl_ptr += runtime->boundary;
  2234. runtime->control->appl_ptr = appl_ptr;
  2235. ret = frames;
  2236. __end:
  2237. snd_pcm_stream_unlock_irq(substream);
  2238. return ret;
  2239. }
  2240. static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
  2241. snd_pcm_uframes_t frames)
  2242. {
  2243. struct snd_pcm_runtime *runtime = substream->runtime;
  2244. snd_pcm_sframes_t appl_ptr;
  2245. snd_pcm_sframes_t ret;
  2246. snd_pcm_sframes_t hw_avail;
  2247. if (frames == 0)
  2248. return 0;
  2249. snd_pcm_stream_lock_irq(substream);
  2250. switch (runtime->status->state) {
  2251. case SNDRV_PCM_STATE_PREPARED:
  2252. case SNDRV_PCM_STATE_DRAINING:
  2253. break;
  2254. case SNDRV_PCM_STATE_RUNNING:
  2255. if (snd_pcm_update_hw_ptr(substream) >= 0)
  2256. break;
  2257. /* Fall through */
  2258. case SNDRV_PCM_STATE_XRUN:
  2259. ret = -EPIPE;
  2260. goto __end;
  2261. case SNDRV_PCM_STATE_SUSPENDED:
  2262. ret = -ESTRPIPE;
  2263. goto __end;
  2264. default:
  2265. ret = -EBADFD;
  2266. goto __end;
  2267. }
  2268. hw_avail = snd_pcm_capture_hw_avail(runtime);
  2269. if (hw_avail <= 0) {
  2270. ret = 0;
  2271. goto __end;
  2272. }
  2273. if (frames > (snd_pcm_uframes_t)hw_avail)
  2274. frames = hw_avail;
  2275. appl_ptr = runtime->control->appl_ptr - frames;
  2276. if (appl_ptr < 0)
  2277. appl_ptr += runtime->boundary;
  2278. runtime->control->appl_ptr = appl_ptr;
  2279. ret = frames;
  2280. __end:
  2281. snd_pcm_stream_unlock_irq(substream);
  2282. return ret;
  2283. }
  2284. static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
  2285. snd_pcm_uframes_t frames)
  2286. {
  2287. struct snd_pcm_runtime *runtime = substream->runtime;
  2288. snd_pcm_sframes_t appl_ptr;
  2289. snd_pcm_sframes_t ret;
  2290. snd_pcm_sframes_t avail;
  2291. if (frames == 0)
  2292. return 0;
  2293. snd_pcm_stream_lock_irq(substream);
  2294. switch (runtime->status->state) {
  2295. case SNDRV_PCM_STATE_PREPARED:
  2296. case SNDRV_PCM_STATE_PAUSED:
  2297. break;
  2298. case SNDRV_PCM_STATE_DRAINING:
  2299. case SNDRV_PCM_STATE_RUNNING:
  2300. if (snd_pcm_update_hw_ptr(substream) >= 0)
  2301. break;
  2302. /* Fall through */
  2303. case SNDRV_PCM_STATE_XRUN:
  2304. ret = -EPIPE;
  2305. goto __end;
  2306. case SNDRV_PCM_STATE_SUSPENDED:
  2307. ret = -ESTRPIPE;
  2308. goto __end;
  2309. default:
  2310. ret = -EBADFD;
  2311. goto __end;
  2312. }
  2313. avail = snd_pcm_playback_avail(runtime);
  2314. if (avail <= 0) {
  2315. ret = 0;
  2316. goto __end;
  2317. }
  2318. if (frames > (snd_pcm_uframes_t)avail)
  2319. frames = avail;
  2320. appl_ptr = runtime->control->appl_ptr + frames;
  2321. if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
  2322. appl_ptr -= runtime->boundary;
  2323. runtime->control->appl_ptr = appl_ptr;
  2324. ret = frames;
  2325. __end:
  2326. snd_pcm_stream_unlock_irq(substream);
  2327. return ret;
  2328. }
  2329. static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
  2330. snd_pcm_uframes_t frames)
  2331. {
  2332. struct snd_pcm_runtime *runtime = substream->runtime;
  2333. snd_pcm_sframes_t appl_ptr;
  2334. snd_pcm_sframes_t ret;
  2335. snd_pcm_sframes_t avail;
  2336. if (frames == 0)
  2337. return 0;
  2338. snd_pcm_stream_lock_irq(substream);
  2339. switch (runtime->status->state) {
  2340. case SNDRV_PCM_STATE_PREPARED:
  2341. case SNDRV_PCM_STATE_DRAINING:
  2342. case SNDRV_PCM_STATE_PAUSED:
  2343. break;
  2344. case SNDRV_PCM_STATE_RUNNING:
  2345. if (snd_pcm_update_hw_ptr(substream) >= 0)
  2346. break;
  2347. /* Fall through */
  2348. case SNDRV_PCM_STATE_XRUN:
  2349. ret = -EPIPE;
  2350. goto __end;
  2351. case SNDRV_PCM_STATE_SUSPENDED:
  2352. ret = -ESTRPIPE;
  2353. goto __end;
  2354. default:
  2355. ret = -EBADFD;
  2356. goto __end;
  2357. }
  2358. avail = snd_pcm_capture_avail(runtime);
  2359. if (avail <= 0) {
  2360. ret = 0;
  2361. goto __end;
  2362. }
  2363. if (frames > (snd_pcm_uframes_t)avail)
  2364. frames = avail;
  2365. appl_ptr = runtime->control->appl_ptr + frames;
  2366. if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
  2367. appl_ptr -= runtime->boundary;
  2368. runtime->control->appl_ptr = appl_ptr;
  2369. ret = frames;
  2370. __end:
  2371. snd_pcm_stream_unlock_irq(substream);
  2372. return ret;
  2373. }
  2374. static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
  2375. {
  2376. struct snd_pcm_runtime *runtime = substream->runtime;
  2377. int err;
  2378. snd_pcm_stream_lock_irq(substream);
  2379. switch (runtime->status->state) {
  2380. case SNDRV_PCM_STATE_DRAINING:
  2381. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  2382. goto __badfd;
  2383. /* Fall through */
  2384. case SNDRV_PCM_STATE_RUNNING:
  2385. if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
  2386. break;
  2387. /* Fall through */
  2388. case SNDRV_PCM_STATE_PREPARED:
  2389. err = 0;
  2390. break;
  2391. case SNDRV_PCM_STATE_SUSPENDED:
  2392. err = -ESTRPIPE;
  2393. break;
  2394. case SNDRV_PCM_STATE_XRUN:
  2395. err = -EPIPE;
  2396. break;
  2397. default:
  2398. __badfd:
  2399. err = -EBADFD;
  2400. break;
  2401. }
  2402. snd_pcm_stream_unlock_irq(substream);
  2403. return err;
  2404. }
  2405. static int snd_pcm_delay(struct snd_pcm_substream *substream,
  2406. snd_pcm_sframes_t __user *res)
  2407. {
  2408. struct snd_pcm_runtime *runtime = substream->runtime;
  2409. int err;
  2410. snd_pcm_sframes_t n = 0;
  2411. snd_pcm_stream_lock_irq(substream);
  2412. switch (runtime->status->state) {
  2413. case SNDRV_PCM_STATE_DRAINING:
  2414. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  2415. goto __badfd;
  2416. /* Fall through */
  2417. case SNDRV_PCM_STATE_RUNNING:
  2418. if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
  2419. break;
  2420. /* Fall through */
  2421. case SNDRV_PCM_STATE_PREPARED:
  2422. case SNDRV_PCM_STATE_SUSPENDED:
  2423. err = 0;
  2424. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  2425. n = snd_pcm_playback_hw_avail(runtime);
  2426. else
  2427. n = snd_pcm_capture_avail(runtime);
  2428. n += runtime->delay;
  2429. break;
  2430. case SNDRV_PCM_STATE_XRUN:
  2431. err = -EPIPE;
  2432. break;
  2433. default:
  2434. __badfd:
  2435. err = -EBADFD;
  2436. break;
  2437. }
  2438. snd_pcm_stream_unlock_irq(substream);
  2439. if (!err)
  2440. if (put_user(n, res))
  2441. err = -EFAULT;
  2442. return err;
  2443. }
  2444. static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
  2445. struct snd_pcm_sync_ptr __user *_sync_ptr)
  2446. {
  2447. struct snd_pcm_runtime *runtime = substream->runtime;
  2448. struct snd_pcm_sync_ptr sync_ptr;
  2449. volatile struct snd_pcm_mmap_status *status;
  2450. volatile struct snd_pcm_mmap_control *control;
  2451. int err;
  2452. memset(&sync_ptr, 0, sizeof(sync_ptr));
  2453. if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
  2454. return -EFAULT;
  2455. if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
  2456. return -EFAULT;
  2457. status = runtime->status;
  2458. control = runtime->control;
  2459. if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
  2460. err = snd_pcm_hwsync(substream);
  2461. if (err < 0)
  2462. return err;
  2463. }
  2464. snd_pcm_stream_lock_irq(substream);
  2465. if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
  2466. control->appl_ptr = sync_ptr.c.control.appl_ptr;
  2467. else
  2468. sync_ptr.c.control.appl_ptr = control->appl_ptr;
  2469. if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
  2470. control->avail_min = sync_ptr.c.control.avail_min;
  2471. else
  2472. sync_ptr.c.control.avail_min = control->avail_min;
  2473. sync_ptr.s.status.state = status->state;
  2474. sync_ptr.s.status.hw_ptr = status->hw_ptr;
  2475. sync_ptr.s.status.tstamp = status->tstamp;
  2476. sync_ptr.s.status.suspended_state = status->suspended_state;
  2477. snd_pcm_stream_unlock_irq(substream);
  2478. if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
  2479. return -EFAULT;
  2480. return 0;
  2481. }
  2482. static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
  2483. {
  2484. struct snd_pcm_runtime *runtime = substream->runtime;
  2485. int arg;
  2486. if (get_user(arg, _arg))
  2487. return -EFAULT;
  2488. if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
  2489. return -EINVAL;
  2490. runtime->tstamp_type = arg;
  2491. return 0;
  2492. }
  2493. static int snd_pcm_common_ioctl1(struct file *file,
  2494. struct snd_pcm_substream *substream,
  2495. unsigned int cmd, void __user *arg)
  2496. {
  2497. switch (cmd) {
  2498. case SNDRV_PCM_IOCTL_PVERSION:
  2499. return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
  2500. case SNDRV_PCM_IOCTL_INFO:
  2501. return snd_pcm_info_user(substream, arg);
  2502. case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
  2503. return 0;
  2504. case SNDRV_PCM_IOCTL_TTSTAMP:
  2505. return snd_pcm_tstamp(substream, arg);
  2506. case SNDRV_PCM_IOCTL_HW_REFINE:
  2507. return snd_pcm_hw_refine_user(substream, arg);
  2508. case SNDRV_PCM_IOCTL_HW_PARAMS:
  2509. return snd_pcm_hw_params_user(substream, arg);
  2510. case SNDRV_PCM_IOCTL_HW_FREE:
  2511. return snd_pcm_hw_free(substream);
  2512. case SNDRV_PCM_IOCTL_SW_PARAMS:
  2513. return snd_pcm_sw_params_user(substream, arg);
  2514. case SNDRV_PCM_IOCTL_STATUS:
  2515. return snd_pcm_status_user(substream, arg, false);
  2516. case SNDRV_PCM_IOCTL_STATUS_EXT:
  2517. return snd_pcm_status_user(substream, arg, true);
  2518. case SNDRV_PCM_IOCTL_CHANNEL_INFO:
  2519. return snd_pcm_channel_info_user(substream, arg);
  2520. case SNDRV_PCM_IOCTL_PREPARE:
  2521. return snd_pcm_prepare(substream, file);
  2522. case SNDRV_PCM_IOCTL_RESET:
  2523. return snd_pcm_reset(substream);
  2524. case SNDRV_PCM_IOCTL_START:
  2525. return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
  2526. case SNDRV_PCM_IOCTL_LINK:
  2527. return snd_pcm_link(substream, (int)(unsigned long) arg);
  2528. case SNDRV_PCM_IOCTL_UNLINK:
  2529. return snd_pcm_unlink(substream);
  2530. case SNDRV_PCM_IOCTL_RESUME:
  2531. return snd_pcm_resume(substream);
  2532. case SNDRV_PCM_IOCTL_XRUN:
  2533. return snd_pcm_xrun(substream);
  2534. case SNDRV_PCM_IOCTL_HWSYNC:
  2535. return snd_pcm_hwsync(substream);
  2536. case SNDRV_PCM_IOCTL_DELAY:
  2537. return snd_pcm_delay(substream, arg);
  2538. case SNDRV_PCM_IOCTL_SYNC_PTR:
  2539. return snd_pcm_sync_ptr(substream, arg);
  2540. #ifdef CONFIG_SND_SUPPORT_OLD_API
  2541. case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
  2542. return snd_pcm_hw_refine_old_user(substream, arg);
  2543. case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
  2544. return snd_pcm_hw_params_old_user(substream, arg);
  2545. #endif
  2546. case SNDRV_PCM_IOCTL_DRAIN:
  2547. return snd_pcm_drain(substream, file);
  2548. case SNDRV_PCM_IOCTL_DROP:
  2549. return snd_pcm_drop(substream);
  2550. case SNDRV_PCM_IOCTL_PAUSE:
  2551. {
  2552. int res;
  2553. snd_pcm_stream_lock_irq(substream);
  2554. res = snd_pcm_pause(substream, (int)(unsigned long)arg);
  2555. snd_pcm_stream_unlock_irq(substream);
  2556. return res;
  2557. }
  2558. }
  2559. pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
  2560. return -ENOTTY;
  2561. }
  2562. static int snd_pcm_playback_ioctl1(struct file *file,
  2563. struct snd_pcm_substream *substream,
  2564. unsigned int cmd, void __user *arg)
  2565. {
  2566. if (snd_BUG_ON(!substream))
  2567. return -ENXIO;
  2568. if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
  2569. return -EINVAL;
  2570. switch (cmd) {
  2571. case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
  2572. {
  2573. struct snd_xferi xferi;
  2574. struct snd_xferi __user *_xferi = arg;
  2575. struct snd_pcm_runtime *runtime = substream->runtime;
  2576. snd_pcm_sframes_t result;
  2577. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2578. return -EBADFD;
  2579. if (put_user(0, &_xferi->result))
  2580. return -EFAULT;
  2581. if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
  2582. return -EFAULT;
  2583. result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
  2584. __put_user(result, &_xferi->result);
  2585. return result < 0 ? result : 0;
  2586. }
  2587. case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
  2588. {
  2589. struct snd_xfern xfern;
  2590. struct snd_xfern __user *_xfern = arg;
  2591. struct snd_pcm_runtime *runtime = substream->runtime;
  2592. void __user **bufs;
  2593. snd_pcm_sframes_t result;
  2594. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2595. return -EBADFD;
  2596. if (runtime->channels > 128)
  2597. return -EINVAL;
  2598. if (put_user(0, &_xfern->result))
  2599. return -EFAULT;
  2600. if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
  2601. return -EFAULT;
  2602. bufs = memdup_user(xfern.bufs,
  2603. sizeof(void *) * runtime->channels);
  2604. if (IS_ERR(bufs))
  2605. return PTR_ERR(bufs);
  2606. result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
  2607. kfree(bufs);
  2608. __put_user(result, &_xfern->result);
  2609. return result < 0 ? result : 0;
  2610. }
  2611. case SNDRV_PCM_IOCTL_REWIND:
  2612. {
  2613. snd_pcm_uframes_t frames;
  2614. snd_pcm_uframes_t __user *_frames = arg;
  2615. snd_pcm_sframes_t result;
  2616. if (get_user(frames, _frames))
  2617. return -EFAULT;
  2618. if (put_user(0, _frames))
  2619. return -EFAULT;
  2620. result = snd_pcm_playback_rewind(substream, frames);
  2621. __put_user(result, _frames);
  2622. return result < 0 ? result : 0;
  2623. }
  2624. case SNDRV_PCM_IOCTL_FORWARD:
  2625. {
  2626. snd_pcm_uframes_t frames;
  2627. snd_pcm_uframes_t __user *_frames = arg;
  2628. snd_pcm_sframes_t result;
  2629. if (get_user(frames, _frames))
  2630. return -EFAULT;
  2631. if (put_user(0, _frames))
  2632. return -EFAULT;
  2633. result = snd_pcm_playback_forward(substream, frames);
  2634. __put_user(result, _frames);
  2635. return result < 0 ? result : 0;
  2636. }
  2637. }
  2638. return snd_pcm_common_ioctl1(file, substream, cmd, arg);
  2639. }
  2640. static int snd_pcm_capture_ioctl1(struct file *file,
  2641. struct snd_pcm_substream *substream,
  2642. unsigned int cmd, void __user *arg)
  2643. {
  2644. if (snd_BUG_ON(!substream))
  2645. return -ENXIO;
  2646. if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
  2647. return -EINVAL;
  2648. switch (cmd) {
  2649. case SNDRV_PCM_IOCTL_READI_FRAMES:
  2650. {
  2651. struct snd_xferi xferi;
  2652. struct snd_xferi __user *_xferi = arg;
  2653. struct snd_pcm_runtime *runtime = substream->runtime;
  2654. snd_pcm_sframes_t result;
  2655. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2656. return -EBADFD;
  2657. if (put_user(0, &_xferi->result))
  2658. return -EFAULT;
  2659. if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
  2660. return -EFAULT;
  2661. result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
  2662. __put_user(result, &_xferi->result);
  2663. return result < 0 ? result : 0;
  2664. }
  2665. case SNDRV_PCM_IOCTL_READN_FRAMES:
  2666. {
  2667. struct snd_xfern xfern;
  2668. struct snd_xfern __user *_xfern = arg;
  2669. struct snd_pcm_runtime *runtime = substream->runtime;
  2670. void *bufs;
  2671. snd_pcm_sframes_t result;
  2672. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2673. return -EBADFD;
  2674. if (runtime->channels > 128)
  2675. return -EINVAL;
  2676. if (put_user(0, &_xfern->result))
  2677. return -EFAULT;
  2678. if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
  2679. return -EFAULT;
  2680. bufs = memdup_user(xfern.bufs,
  2681. sizeof(void *) * runtime->channels);
  2682. if (IS_ERR(bufs))
  2683. return PTR_ERR(bufs);
  2684. result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
  2685. kfree(bufs);
  2686. __put_user(result, &_xfern->result);
  2687. return result < 0 ? result : 0;
  2688. }
  2689. case SNDRV_PCM_IOCTL_REWIND:
  2690. {
  2691. snd_pcm_uframes_t frames;
  2692. snd_pcm_uframes_t __user *_frames = arg;
  2693. snd_pcm_sframes_t result;
  2694. if (get_user(frames, _frames))
  2695. return -EFAULT;
  2696. if (put_user(0, _frames))
  2697. return -EFAULT;
  2698. result = snd_pcm_capture_rewind(substream, frames);
  2699. __put_user(result, _frames);
  2700. return result < 0 ? result : 0;
  2701. }
  2702. case SNDRV_PCM_IOCTL_FORWARD:
  2703. {
  2704. snd_pcm_uframes_t frames;
  2705. snd_pcm_uframes_t __user *_frames = arg;
  2706. snd_pcm_sframes_t result;
  2707. if (get_user(frames, _frames))
  2708. return -EFAULT;
  2709. if (put_user(0, _frames))
  2710. return -EFAULT;
  2711. result = snd_pcm_capture_forward(substream, frames);
  2712. __put_user(result, _frames);
  2713. return result < 0 ? result : 0;
  2714. }
  2715. }
  2716. return snd_pcm_common_ioctl1(file, substream, cmd, arg);
  2717. }
  2718. static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
  2719. unsigned long arg)
  2720. {
  2721. struct snd_pcm_file *pcm_file;
  2722. pcm_file = file->private_data;
  2723. if (((cmd >> 8) & 0xff) != 'A')
  2724. return -ENOTTY;
  2725. return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
  2726. (void __user *)arg);
  2727. }
  2728. static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
  2729. unsigned long arg)
  2730. {
  2731. struct snd_pcm_file *pcm_file;
  2732. pcm_file = file->private_data;
  2733. if (((cmd >> 8) & 0xff) != 'A')
  2734. return -ENOTTY;
  2735. return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
  2736. (void __user *)arg);
  2737. }
  2738. int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
  2739. unsigned int cmd, void *arg)
  2740. {
  2741. mm_segment_t fs;
  2742. int result;
  2743. fs = snd_enter_user();
  2744. switch (substream->stream) {
  2745. case SNDRV_PCM_STREAM_PLAYBACK:
  2746. result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
  2747. (void __user *)arg);
  2748. break;
  2749. case SNDRV_PCM_STREAM_CAPTURE:
  2750. result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
  2751. (void __user *)arg);
  2752. break;
  2753. default:
  2754. result = -EINVAL;
  2755. break;
  2756. }
  2757. snd_leave_user(fs);
  2758. return result;
  2759. }
  2760. EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
  2761. static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
  2762. loff_t * offset)
  2763. {
  2764. struct snd_pcm_file *pcm_file;
  2765. struct snd_pcm_substream *substream;
  2766. struct snd_pcm_runtime *runtime;
  2767. snd_pcm_sframes_t result;
  2768. pcm_file = file->private_data;
  2769. substream = pcm_file->substream;
  2770. if (PCM_RUNTIME_CHECK(substream))
  2771. return -ENXIO;
  2772. runtime = substream->runtime;
  2773. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2774. return -EBADFD;
  2775. if (!frame_aligned(runtime, count))
  2776. return -EINVAL;
  2777. count = bytes_to_frames(runtime, count);
  2778. result = snd_pcm_lib_read(substream, buf, count);
  2779. if (result > 0)
  2780. result = frames_to_bytes(runtime, result);
  2781. return result;
  2782. }
  2783. static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
  2784. size_t count, loff_t * offset)
  2785. {
  2786. struct snd_pcm_file *pcm_file;
  2787. struct snd_pcm_substream *substream;
  2788. struct snd_pcm_runtime *runtime;
  2789. snd_pcm_sframes_t result;
  2790. pcm_file = file->private_data;
  2791. substream = pcm_file->substream;
  2792. if (PCM_RUNTIME_CHECK(substream))
  2793. return -ENXIO;
  2794. runtime = substream->runtime;
  2795. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2796. return -EBADFD;
  2797. if (!frame_aligned(runtime, count))
  2798. return -EINVAL;
  2799. count = bytes_to_frames(runtime, count);
  2800. result = snd_pcm_lib_write(substream, buf, count);
  2801. if (result > 0)
  2802. result = frames_to_bytes(runtime, result);
  2803. return result;
  2804. }
  2805. static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
  2806. {
  2807. struct snd_pcm_file *pcm_file;
  2808. struct snd_pcm_substream *substream;
  2809. struct snd_pcm_runtime *runtime;
  2810. snd_pcm_sframes_t result;
  2811. unsigned long i;
  2812. void __user **bufs;
  2813. snd_pcm_uframes_t frames;
  2814. pcm_file = iocb->ki_filp->private_data;
  2815. substream = pcm_file->substream;
  2816. if (PCM_RUNTIME_CHECK(substream))
  2817. return -ENXIO;
  2818. runtime = substream->runtime;
  2819. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2820. return -EBADFD;
  2821. if (!iter_is_iovec(to))
  2822. return -EINVAL;
  2823. if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
  2824. return -EINVAL;
  2825. if (!frame_aligned(runtime, to->iov->iov_len))
  2826. return -EINVAL;
  2827. frames = bytes_to_samples(runtime, to->iov->iov_len);
  2828. bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
  2829. if (bufs == NULL)
  2830. return -ENOMEM;
  2831. for (i = 0; i < to->nr_segs; ++i)
  2832. bufs[i] = to->iov[i].iov_base;
  2833. result = snd_pcm_lib_readv(substream, bufs, frames);
  2834. if (result > 0)
  2835. result = frames_to_bytes(runtime, result);
  2836. kfree(bufs);
  2837. return result;
  2838. }
  2839. static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
  2840. {
  2841. struct snd_pcm_file *pcm_file;
  2842. struct snd_pcm_substream *substream;
  2843. struct snd_pcm_runtime *runtime;
  2844. snd_pcm_sframes_t result;
  2845. unsigned long i;
  2846. void __user **bufs;
  2847. snd_pcm_uframes_t frames;
  2848. pcm_file = iocb->ki_filp->private_data;
  2849. substream = pcm_file->substream;
  2850. if (PCM_RUNTIME_CHECK(substream))
  2851. return -ENXIO;
  2852. runtime = substream->runtime;
  2853. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2854. return -EBADFD;
  2855. if (!iter_is_iovec(from))
  2856. return -EINVAL;
  2857. if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
  2858. !frame_aligned(runtime, from->iov->iov_len))
  2859. return -EINVAL;
  2860. frames = bytes_to_samples(runtime, from->iov->iov_len);
  2861. bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
  2862. if (bufs == NULL)
  2863. return -ENOMEM;
  2864. for (i = 0; i < from->nr_segs; ++i)
  2865. bufs[i] = from->iov[i].iov_base;
  2866. result = snd_pcm_lib_writev(substream, bufs, frames);
  2867. if (result > 0)
  2868. result = frames_to_bytes(runtime, result);
  2869. kfree(bufs);
  2870. return result;
  2871. }
  2872. static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
  2873. {
  2874. struct snd_pcm_file *pcm_file;
  2875. struct snd_pcm_substream *substream;
  2876. struct snd_pcm_runtime *runtime;
  2877. unsigned int mask;
  2878. snd_pcm_uframes_t avail;
  2879. pcm_file = file->private_data;
  2880. substream = pcm_file->substream;
  2881. if (PCM_RUNTIME_CHECK(substream))
  2882. return POLLOUT | POLLWRNORM | POLLERR;
  2883. runtime = substream->runtime;
  2884. poll_wait(file, &runtime->sleep, wait);
  2885. snd_pcm_stream_lock_irq(substream);
  2886. avail = snd_pcm_playback_avail(runtime);
  2887. switch (runtime->status->state) {
  2888. case SNDRV_PCM_STATE_RUNNING:
  2889. case SNDRV_PCM_STATE_PREPARED:
  2890. case SNDRV_PCM_STATE_PAUSED:
  2891. if (avail >= runtime->control->avail_min) {
  2892. mask = POLLOUT | POLLWRNORM;
  2893. break;
  2894. }
  2895. /* Fall through */
  2896. case SNDRV_PCM_STATE_DRAINING:
  2897. mask = 0;
  2898. break;
  2899. default:
  2900. mask = POLLOUT | POLLWRNORM | POLLERR;
  2901. break;
  2902. }
  2903. snd_pcm_stream_unlock_irq(substream);
  2904. return mask;
  2905. }
  2906. static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
  2907. {
  2908. struct snd_pcm_file *pcm_file;
  2909. struct snd_pcm_substream *substream;
  2910. struct snd_pcm_runtime *runtime;
  2911. unsigned int mask;
  2912. snd_pcm_uframes_t avail;
  2913. pcm_file = file->private_data;
  2914. substream = pcm_file->substream;
  2915. if (PCM_RUNTIME_CHECK(substream))
  2916. return POLLIN | POLLRDNORM | POLLERR;
  2917. runtime = substream->runtime;
  2918. poll_wait(file, &runtime->sleep, wait);
  2919. snd_pcm_stream_lock_irq(substream);
  2920. avail = snd_pcm_capture_avail(runtime);
  2921. switch (runtime->status->state) {
  2922. case SNDRV_PCM_STATE_RUNNING:
  2923. case SNDRV_PCM_STATE_PREPARED:
  2924. case SNDRV_PCM_STATE_PAUSED:
  2925. if (avail >= runtime->control->avail_min) {
  2926. mask = POLLIN | POLLRDNORM;
  2927. break;
  2928. }
  2929. mask = 0;
  2930. break;
  2931. case SNDRV_PCM_STATE_DRAINING:
  2932. if (avail > 0) {
  2933. mask = POLLIN | POLLRDNORM;
  2934. break;
  2935. }
  2936. /* Fall through */
  2937. default:
  2938. mask = POLLIN | POLLRDNORM | POLLERR;
  2939. break;
  2940. }
  2941. snd_pcm_stream_unlock_irq(substream);
  2942. return mask;
  2943. }
  2944. /*
  2945. * mmap support
  2946. */
  2947. /*
  2948. * Only on coherent architectures, we can mmap the status and the control records
  2949. * for effcient data transfer. On others, we have to use HWSYNC ioctl...
  2950. */
  2951. #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
  2952. /*
  2953. * mmap status record
  2954. */
  2955. static int snd_pcm_mmap_status_fault(struct vm_fault *vmf)
  2956. {
  2957. struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
  2958. struct snd_pcm_runtime *runtime;
  2959. if (substream == NULL)
  2960. return VM_FAULT_SIGBUS;
  2961. runtime = substream->runtime;
  2962. vmf->page = virt_to_page(runtime->status);
  2963. get_page(vmf->page);
  2964. return 0;
  2965. }
  2966. static const struct vm_operations_struct snd_pcm_vm_ops_status =
  2967. {
  2968. .fault = snd_pcm_mmap_status_fault,
  2969. };
  2970. static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
  2971. struct vm_area_struct *area)
  2972. {
  2973. long size;
  2974. if (!(area->vm_flags & VM_READ))
  2975. return -EINVAL;
  2976. size = area->vm_end - area->vm_start;
  2977. if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
  2978. return -EINVAL;
  2979. area->vm_ops = &snd_pcm_vm_ops_status;
  2980. area->vm_private_data = substream;
  2981. area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  2982. return 0;
  2983. }
  2984. /*
  2985. * mmap control record
  2986. */
  2987. static int snd_pcm_mmap_control_fault(struct vm_fault *vmf)
  2988. {
  2989. struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
  2990. struct snd_pcm_runtime *runtime;
  2991. if (substream == NULL)
  2992. return VM_FAULT_SIGBUS;
  2993. runtime = substream->runtime;
  2994. vmf->page = virt_to_page(runtime->control);
  2995. get_page(vmf->page);
  2996. return 0;
  2997. }
  2998. static const struct vm_operations_struct snd_pcm_vm_ops_control =
  2999. {
  3000. .fault = snd_pcm_mmap_control_fault,
  3001. };
  3002. static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
  3003. struct vm_area_struct *area)
  3004. {
  3005. long size;
  3006. if (!(area->vm_flags & VM_READ))
  3007. return -EINVAL;
  3008. size = area->vm_end - area->vm_start;
  3009. if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
  3010. return -EINVAL;
  3011. area->vm_ops = &snd_pcm_vm_ops_control;
  3012. area->vm_private_data = substream;
  3013. area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  3014. return 0;
  3015. }
  3016. #else /* ! coherent mmap */
  3017. /*
  3018. * don't support mmap for status and control records.
  3019. */
  3020. static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
  3021. struct vm_area_struct *area)
  3022. {
  3023. return -ENXIO;
  3024. }
  3025. static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
  3026. struct vm_area_struct *area)
  3027. {
  3028. return -ENXIO;
  3029. }
  3030. #endif /* coherent mmap */
  3031. static inline struct page *
  3032. snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
  3033. {
  3034. void *vaddr = substream->runtime->dma_area + ofs;
  3035. return virt_to_page(vaddr);
  3036. }
  3037. /*
  3038. * fault callback for mmapping a RAM page
  3039. */
  3040. static int snd_pcm_mmap_data_fault(struct vm_fault *vmf)
  3041. {
  3042. struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
  3043. struct snd_pcm_runtime *runtime;
  3044. unsigned long offset;
  3045. struct page * page;
  3046. size_t dma_bytes;
  3047. if (substream == NULL)
  3048. return VM_FAULT_SIGBUS;
  3049. runtime = substream->runtime;
  3050. offset = vmf->pgoff << PAGE_SHIFT;
  3051. dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
  3052. if (offset > dma_bytes - PAGE_SIZE)
  3053. return VM_FAULT_SIGBUS;
  3054. if (substream->ops->page)
  3055. page = substream->ops->page(substream, offset);
  3056. else
  3057. page = snd_pcm_default_page_ops(substream, offset);
  3058. if (!page)
  3059. return VM_FAULT_SIGBUS;
  3060. get_page(page);
  3061. vmf->page = page;
  3062. return 0;
  3063. }
  3064. static const struct vm_operations_struct snd_pcm_vm_ops_data = {
  3065. .open = snd_pcm_mmap_data_open,
  3066. .close = snd_pcm_mmap_data_close,
  3067. };
  3068. static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
  3069. .open = snd_pcm_mmap_data_open,
  3070. .close = snd_pcm_mmap_data_close,
  3071. .fault = snd_pcm_mmap_data_fault,
  3072. };
  3073. /*
  3074. * mmap the DMA buffer on RAM
  3075. */
  3076. /**
  3077. * snd_pcm_lib_default_mmap - Default PCM data mmap function
  3078. * @substream: PCM substream
  3079. * @area: VMA
  3080. *
  3081. * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
  3082. * this function is invoked implicitly.
  3083. */
  3084. int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
  3085. struct vm_area_struct *area)
  3086. {
  3087. area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  3088. #ifdef CONFIG_GENERIC_ALLOCATOR
  3089. if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
  3090. area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
  3091. return remap_pfn_range(area, area->vm_start,
  3092. substream->dma_buffer.addr >> PAGE_SHIFT,
  3093. area->vm_end - area->vm_start, area->vm_page_prot);
  3094. }
  3095. #endif /* CONFIG_GENERIC_ALLOCATOR */
  3096. #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
  3097. if (!substream->ops->page &&
  3098. substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
  3099. return dma_mmap_coherent(substream->dma_buffer.dev.dev,
  3100. area,
  3101. substream->runtime->dma_area,
  3102. substream->runtime->dma_addr,
  3103. area->vm_end - area->vm_start);
  3104. #endif /* CONFIG_X86 */
  3105. /* mmap with fault handler */
  3106. area->vm_ops = &snd_pcm_vm_ops_data_fault;
  3107. return 0;
  3108. }
  3109. EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
  3110. /*
  3111. * mmap the DMA buffer on I/O memory area
  3112. */
  3113. #if SNDRV_PCM_INFO_MMAP_IOMEM
  3114. /**
  3115. * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
  3116. * @substream: PCM substream
  3117. * @area: VMA
  3118. *
  3119. * When your hardware uses the iomapped pages as the hardware buffer and
  3120. * wants to mmap it, pass this function as mmap pcm_ops. Note that this
  3121. * is supposed to work only on limited architectures.
  3122. */
  3123. int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
  3124. struct vm_area_struct *area)
  3125. {
  3126. struct snd_pcm_runtime *runtime = substream->runtime;;
  3127. area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
  3128. return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
  3129. }
  3130. EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
  3131. #endif /* SNDRV_PCM_INFO_MMAP */
  3132. /*
  3133. * mmap DMA buffer
  3134. */
  3135. int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
  3136. struct vm_area_struct *area)
  3137. {
  3138. struct snd_pcm_runtime *runtime;
  3139. long size;
  3140. unsigned long offset;
  3141. size_t dma_bytes;
  3142. int err;
  3143. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  3144. if (!(area->vm_flags & (VM_WRITE|VM_READ)))
  3145. return -EINVAL;
  3146. } else {
  3147. if (!(area->vm_flags & VM_READ))
  3148. return -EINVAL;
  3149. }
  3150. runtime = substream->runtime;
  3151. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  3152. return -EBADFD;
  3153. if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
  3154. return -ENXIO;
  3155. if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
  3156. runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  3157. return -EINVAL;
  3158. size = area->vm_end - area->vm_start;
  3159. offset = area->vm_pgoff << PAGE_SHIFT;
  3160. dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
  3161. if ((size_t)size > dma_bytes)
  3162. return -EINVAL;
  3163. if (offset > dma_bytes - size)
  3164. return -EINVAL;
  3165. area->vm_ops = &snd_pcm_vm_ops_data;
  3166. area->vm_private_data = substream;
  3167. if (substream->ops->mmap)
  3168. err = substream->ops->mmap(substream, area);
  3169. else
  3170. err = snd_pcm_lib_default_mmap(substream, area);
  3171. if (!err)
  3172. atomic_inc(&substream->mmap_count);
  3173. return err;
  3174. }
  3175. EXPORT_SYMBOL(snd_pcm_mmap_data);
  3176. static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
  3177. {
  3178. struct snd_pcm_file * pcm_file;
  3179. struct snd_pcm_substream *substream;
  3180. unsigned long offset;
  3181. pcm_file = file->private_data;
  3182. substream = pcm_file->substream;
  3183. if (PCM_RUNTIME_CHECK(substream))
  3184. return -ENXIO;
  3185. offset = area->vm_pgoff << PAGE_SHIFT;
  3186. switch (offset) {
  3187. case SNDRV_PCM_MMAP_OFFSET_STATUS:
  3188. if (pcm_file->no_compat_mmap)
  3189. return -ENXIO;
  3190. return snd_pcm_mmap_status(substream, file, area);
  3191. case SNDRV_PCM_MMAP_OFFSET_CONTROL:
  3192. if (pcm_file->no_compat_mmap)
  3193. return -ENXIO;
  3194. return snd_pcm_mmap_control(substream, file, area);
  3195. default:
  3196. return snd_pcm_mmap_data(substream, file, area);
  3197. }
  3198. return 0;
  3199. }
  3200. static int snd_pcm_fasync(int fd, struct file * file, int on)
  3201. {
  3202. struct snd_pcm_file * pcm_file;
  3203. struct snd_pcm_substream *substream;
  3204. struct snd_pcm_runtime *runtime;
  3205. pcm_file = file->private_data;
  3206. substream = pcm_file->substream;
  3207. if (PCM_RUNTIME_CHECK(substream))
  3208. return -ENXIO;
  3209. runtime = substream->runtime;
  3210. return fasync_helper(fd, file, on, &runtime->fasync);
  3211. }
  3212. /*
  3213. * ioctl32 compat
  3214. */
  3215. #ifdef CONFIG_COMPAT
  3216. #include "pcm_compat.c"
  3217. #else
  3218. #define snd_pcm_ioctl_compat NULL
  3219. #endif
  3220. /*
  3221. * To be removed helpers to keep binary compatibility
  3222. */
  3223. #ifdef CONFIG_SND_SUPPORT_OLD_API
  3224. #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
  3225. #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
  3226. static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
  3227. struct snd_pcm_hw_params_old *oparams)
  3228. {
  3229. unsigned int i;
  3230. memset(params, 0, sizeof(*params));
  3231. params->flags = oparams->flags;
  3232. for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
  3233. params->masks[i].bits[0] = oparams->masks[i];
  3234. memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
  3235. params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
  3236. params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
  3237. params->info = oparams->info;
  3238. params->msbits = oparams->msbits;
  3239. params->rate_num = oparams->rate_num;
  3240. params->rate_den = oparams->rate_den;
  3241. params->fifo_size = oparams->fifo_size;
  3242. }
  3243. static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
  3244. struct snd_pcm_hw_params *params)
  3245. {
  3246. unsigned int i;
  3247. memset(oparams, 0, sizeof(*oparams));
  3248. oparams->flags = params->flags;
  3249. for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
  3250. oparams->masks[i] = params->masks[i].bits[0];
  3251. memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
  3252. oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
  3253. oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
  3254. oparams->info = params->info;
  3255. oparams->msbits = params->msbits;
  3256. oparams->rate_num = params->rate_num;
  3257. oparams->rate_den = params->rate_den;
  3258. oparams->fifo_size = params->fifo_size;
  3259. }
  3260. static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
  3261. struct snd_pcm_hw_params_old __user * _oparams)
  3262. {
  3263. struct snd_pcm_hw_params *params;
  3264. struct snd_pcm_hw_params_old *oparams = NULL;
  3265. int err;
  3266. params = kmalloc(sizeof(*params), GFP_KERNEL);
  3267. if (!params)
  3268. return -ENOMEM;
  3269. oparams = memdup_user(_oparams, sizeof(*oparams));
  3270. if (IS_ERR(oparams)) {
  3271. err = PTR_ERR(oparams);
  3272. goto out;
  3273. }
  3274. snd_pcm_hw_convert_from_old_params(params, oparams);
  3275. err = snd_pcm_hw_refine(substream, params);
  3276. snd_pcm_hw_convert_to_old_params(oparams, params);
  3277. if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
  3278. if (!err)
  3279. err = -EFAULT;
  3280. }
  3281. kfree(oparams);
  3282. out:
  3283. kfree(params);
  3284. return err;
  3285. }
  3286. static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
  3287. struct snd_pcm_hw_params_old __user * _oparams)
  3288. {
  3289. struct snd_pcm_hw_params *params;
  3290. struct snd_pcm_hw_params_old *oparams = NULL;
  3291. int err;
  3292. params = kmalloc(sizeof(*params), GFP_KERNEL);
  3293. if (!params)
  3294. return -ENOMEM;
  3295. oparams = memdup_user(_oparams, sizeof(*oparams));
  3296. if (IS_ERR(oparams)) {
  3297. err = PTR_ERR(oparams);
  3298. goto out;
  3299. }
  3300. snd_pcm_hw_convert_from_old_params(params, oparams);
  3301. err = snd_pcm_hw_params(substream, params);
  3302. snd_pcm_hw_convert_to_old_params(oparams, params);
  3303. if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
  3304. if (!err)
  3305. err = -EFAULT;
  3306. }
  3307. kfree(oparams);
  3308. out:
  3309. kfree(params);
  3310. return err;
  3311. }
  3312. #endif /* CONFIG_SND_SUPPORT_OLD_API */
  3313. #ifndef CONFIG_MMU
  3314. static unsigned long snd_pcm_get_unmapped_area(struct file *file,
  3315. unsigned long addr,
  3316. unsigned long len,
  3317. unsigned long pgoff,
  3318. unsigned long flags)
  3319. {
  3320. struct snd_pcm_file *pcm_file = file->private_data;
  3321. struct snd_pcm_substream *substream = pcm_file->substream;
  3322. struct snd_pcm_runtime *runtime = substream->runtime;
  3323. unsigned long offset = pgoff << PAGE_SHIFT;
  3324. switch (offset) {
  3325. case SNDRV_PCM_MMAP_OFFSET_STATUS:
  3326. return (unsigned long)runtime->status;
  3327. case SNDRV_PCM_MMAP_OFFSET_CONTROL:
  3328. return (unsigned long)runtime->control;
  3329. default:
  3330. return (unsigned long)runtime->dma_area + offset;
  3331. }
  3332. }
  3333. #else
  3334. # define snd_pcm_get_unmapped_area NULL
  3335. #endif
  3336. /*
  3337. * Register section
  3338. */
  3339. const struct file_operations snd_pcm_f_ops[2] = {
  3340. {
  3341. .owner = THIS_MODULE,
  3342. .write = snd_pcm_write,
  3343. .write_iter = snd_pcm_writev,
  3344. .open = snd_pcm_playback_open,
  3345. .release = snd_pcm_release,
  3346. .llseek = no_llseek,
  3347. .poll = snd_pcm_playback_poll,
  3348. .unlocked_ioctl = snd_pcm_playback_ioctl,
  3349. .compat_ioctl = snd_pcm_ioctl_compat,
  3350. .mmap = snd_pcm_mmap,
  3351. .fasync = snd_pcm_fasync,
  3352. .get_unmapped_area = snd_pcm_get_unmapped_area,
  3353. },
  3354. {
  3355. .owner = THIS_MODULE,
  3356. .read = snd_pcm_read,
  3357. .read_iter = snd_pcm_readv,
  3358. .open = snd_pcm_capture_open,
  3359. .release = snd_pcm_release,
  3360. .llseek = no_llseek,
  3361. .poll = snd_pcm_capture_poll,
  3362. .unlocked_ioctl = snd_pcm_capture_ioctl,
  3363. .compat_ioctl = snd_pcm_ioctl_compat,
  3364. .mmap = snd_pcm_mmap,
  3365. .fasync = snd_pcm_fasync,
  3366. .get_unmapped_area = snd_pcm_get_unmapped_area,
  3367. }
  3368. };