pcm_native.c 93 KB

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