pcm_native.c 100 KB

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