pcm_native.c 92 KB

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