pcm_native.c 91 KB

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