pcm_native.c 94 KB

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