pcm_native.c 94 KB

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