pcm_native.c 98 KB

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