pcm_native.c 92 KB

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