pcm_native.c 91 KB

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