pcm_native.c 94 KB

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