pcm_native.c 92 KB

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