pcm_native.c 93 KB

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