pcm_native.c 92 KB

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