pcm_native.c 94 KB

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