pcm_native.c 91 KB

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