pcm_native.c 94 KB

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