pcm_native.c 100 KB

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