pcm_native.c 92 KB

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