pcm_native.c 91 KB

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