pcm_native.c 94 KB

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