pcm_native.c 95 KB

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