pcm_native.c 94 KB

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