pcm_native.c 99 KB

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