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. snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
  523. } else
  524. snd_timestamp_now(&status->tstamp, runtime->tstamp_timespec);
  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. snd_timestamp_now(&runtime->trigger_tstamp, runtime->tstamp_timespec);
  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. *
  793. * Start all linked streams.
  794. */
  795. int snd_pcm_start(snd_pcm_substream_t *substream)
  796. {
  797. return snd_pcm_action(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
  798. }
  799. /*
  800. * stop callbacks
  801. */
  802. static int snd_pcm_pre_stop(snd_pcm_substream_t *substream, int state)
  803. {
  804. snd_pcm_runtime_t *runtime = substream->runtime;
  805. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  806. return -EBADFD;
  807. runtime->trigger_master = substream;
  808. return 0;
  809. }
  810. static int snd_pcm_do_stop(snd_pcm_substream_t *substream, int state)
  811. {
  812. if (substream->runtime->trigger_master == substream &&
  813. snd_pcm_running(substream))
  814. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
  815. return 0; /* unconditonally stop all substreams */
  816. }
  817. static void snd_pcm_post_stop(snd_pcm_substream_t *substream, int state)
  818. {
  819. snd_pcm_runtime_t *runtime = substream->runtime;
  820. if (runtime->status->state != state) {
  821. snd_pcm_trigger_tstamp(substream);
  822. if (substream->timer)
  823. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP, &runtime->trigger_tstamp);
  824. runtime->status->state = state;
  825. snd_pcm_tick_set(substream, 0);
  826. }
  827. wake_up(&runtime->sleep);
  828. }
  829. static struct action_ops snd_pcm_action_stop = {
  830. .pre_action = snd_pcm_pre_stop,
  831. .do_action = snd_pcm_do_stop,
  832. .post_action = snd_pcm_post_stop
  833. };
  834. /**
  835. * snd_pcm_stop
  836. *
  837. * Try to stop all running streams in the substream group.
  838. * The state of each stream is changed to the given value after that unconditionally.
  839. */
  840. int snd_pcm_stop(snd_pcm_substream_t *substream, int state)
  841. {
  842. return snd_pcm_action(&snd_pcm_action_stop, substream, state);
  843. }
  844. /**
  845. * snd_pcm_drain_done
  846. *
  847. * Stop the DMA only when the given stream is playback.
  848. * The state is changed to SETUP.
  849. * Unlike snd_pcm_stop(), this affects only the given stream.
  850. */
  851. int snd_pcm_drain_done(snd_pcm_substream_t *substream)
  852. {
  853. return snd_pcm_action_single(&snd_pcm_action_stop, substream, SNDRV_PCM_STATE_SETUP);
  854. }
  855. /*
  856. * pause callbacks
  857. */
  858. static int snd_pcm_pre_pause(snd_pcm_substream_t *substream, int push)
  859. {
  860. snd_pcm_runtime_t *runtime = substream->runtime;
  861. if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
  862. return -ENOSYS;
  863. if (push) {
  864. if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
  865. return -EBADFD;
  866. } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
  867. return -EBADFD;
  868. runtime->trigger_master = substream;
  869. return 0;
  870. }
  871. static int snd_pcm_do_pause(snd_pcm_substream_t *substream, int push)
  872. {
  873. if (substream->runtime->trigger_master != substream)
  874. return 0;
  875. return substream->ops->trigger(substream,
  876. push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
  877. SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
  878. }
  879. static void snd_pcm_undo_pause(snd_pcm_substream_t *substream, int push)
  880. {
  881. if (substream->runtime->trigger_master == substream)
  882. substream->ops->trigger(substream,
  883. push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
  884. SNDRV_PCM_TRIGGER_PAUSE_PUSH);
  885. }
  886. static void snd_pcm_post_pause(snd_pcm_substream_t *substream, int push)
  887. {
  888. snd_pcm_runtime_t *runtime = substream->runtime;
  889. snd_pcm_trigger_tstamp(substream);
  890. if (push) {
  891. runtime->status->state = SNDRV_PCM_STATE_PAUSED;
  892. if (substream->timer)
  893. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MPAUSE, &runtime->trigger_tstamp);
  894. snd_pcm_tick_set(substream, 0);
  895. wake_up(&runtime->sleep);
  896. } else {
  897. runtime->status->state = SNDRV_PCM_STATE_RUNNING;
  898. if (runtime->sleep_min)
  899. snd_pcm_tick_prepare(substream);
  900. if (substream->timer)
  901. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MCONTINUE, &runtime->trigger_tstamp);
  902. }
  903. }
  904. static struct action_ops snd_pcm_action_pause = {
  905. .pre_action = snd_pcm_pre_pause,
  906. .do_action = snd_pcm_do_pause,
  907. .undo_action = snd_pcm_undo_pause,
  908. .post_action = snd_pcm_post_pause
  909. };
  910. /*
  911. * Push/release the pause for all linked streams.
  912. */
  913. static int snd_pcm_pause(snd_pcm_substream_t *substream, int push)
  914. {
  915. return snd_pcm_action(&snd_pcm_action_pause, substream, push);
  916. }
  917. #ifdef CONFIG_PM
  918. /* suspend */
  919. static int snd_pcm_pre_suspend(snd_pcm_substream_t *substream, int state)
  920. {
  921. snd_pcm_runtime_t *runtime = substream->runtime;
  922. if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
  923. return -EBUSY;
  924. runtime->trigger_master = substream;
  925. return 0;
  926. }
  927. static int snd_pcm_do_suspend(snd_pcm_substream_t *substream, int state)
  928. {
  929. snd_pcm_runtime_t *runtime = substream->runtime;
  930. if (runtime->trigger_master != substream)
  931. return 0;
  932. if (! snd_pcm_running(substream))
  933. return 0;
  934. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
  935. return 0; /* suspend unconditionally */
  936. }
  937. static void snd_pcm_post_suspend(snd_pcm_substream_t *substream, int state)
  938. {
  939. snd_pcm_runtime_t *runtime = substream->runtime;
  940. snd_pcm_trigger_tstamp(substream);
  941. if (substream->timer)
  942. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND, &runtime->trigger_tstamp);
  943. runtime->status->suspended_state = runtime->status->state;
  944. runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
  945. snd_pcm_tick_set(substream, 0);
  946. wake_up(&runtime->sleep);
  947. }
  948. static struct action_ops snd_pcm_action_suspend = {
  949. .pre_action = snd_pcm_pre_suspend,
  950. .do_action = snd_pcm_do_suspend,
  951. .post_action = snd_pcm_post_suspend
  952. };
  953. /**
  954. * snd_pcm_suspend
  955. *
  956. * Trigger SUSPEND to all linked streams.
  957. * After this call, all streams are changed to SUSPENDED state.
  958. */
  959. int snd_pcm_suspend(snd_pcm_substream_t *substream)
  960. {
  961. int err;
  962. unsigned long flags;
  963. snd_pcm_stream_lock_irqsave(substream, flags);
  964. err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
  965. snd_pcm_stream_unlock_irqrestore(substream, flags);
  966. return err;
  967. }
  968. /**
  969. * snd_pcm_suspend_all
  970. *
  971. * Trigger SUSPEND to all substreams in the given pcm.
  972. * After this call, all streams are changed to SUSPENDED state.
  973. */
  974. int snd_pcm_suspend_all(snd_pcm_t *pcm)
  975. {
  976. snd_pcm_substream_t *substream;
  977. int stream, err = 0;
  978. for (stream = 0; stream < 2; stream++) {
  979. for (substream = pcm->streams[stream].substream; substream; substream = substream->next) {
  980. /* FIXME: the open/close code should lock this as well */
  981. if (substream->runtime == NULL)
  982. continue;
  983. err = snd_pcm_suspend(substream);
  984. if (err < 0 && err != -EBUSY)
  985. return err;
  986. }
  987. }
  988. return 0;
  989. }
  990. /* resume */
  991. static int snd_pcm_pre_resume(snd_pcm_substream_t *substream, int state)
  992. {
  993. snd_pcm_runtime_t *runtime = substream->runtime;
  994. if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
  995. return -ENOSYS;
  996. runtime->trigger_master = substream;
  997. return 0;
  998. }
  999. static int snd_pcm_do_resume(snd_pcm_substream_t *substream, int state)
  1000. {
  1001. snd_pcm_runtime_t *runtime = substream->runtime;
  1002. if (runtime->trigger_master != substream)
  1003. return 0;
  1004. /* DMA not running previously? */
  1005. if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
  1006. (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
  1007. substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
  1008. return 0;
  1009. return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
  1010. }
  1011. static void snd_pcm_undo_resume(snd_pcm_substream_t *substream, int state)
  1012. {
  1013. if (substream->runtime->trigger_master == substream &&
  1014. snd_pcm_running(substream))
  1015. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
  1016. }
  1017. static void snd_pcm_post_resume(snd_pcm_substream_t *substream, int state)
  1018. {
  1019. snd_pcm_runtime_t *runtime = substream->runtime;
  1020. snd_pcm_trigger_tstamp(substream);
  1021. if (substream->timer)
  1022. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME, &runtime->trigger_tstamp);
  1023. runtime->status->state = runtime->status->suspended_state;
  1024. if (runtime->sleep_min)
  1025. snd_pcm_tick_prepare(substream);
  1026. }
  1027. static struct action_ops snd_pcm_action_resume = {
  1028. .pre_action = snd_pcm_pre_resume,
  1029. .do_action = snd_pcm_do_resume,
  1030. .undo_action = snd_pcm_undo_resume,
  1031. .post_action = snd_pcm_post_resume
  1032. };
  1033. static int snd_pcm_resume(snd_pcm_substream_t *substream)
  1034. {
  1035. snd_card_t *card = substream->pcm->card;
  1036. int res;
  1037. snd_power_lock(card);
  1038. if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
  1039. res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
  1040. snd_power_unlock(card);
  1041. return res;
  1042. }
  1043. #else
  1044. static int snd_pcm_resume(snd_pcm_substream_t *substream)
  1045. {
  1046. return -ENOSYS;
  1047. }
  1048. #endif /* CONFIG_PM */
  1049. /*
  1050. * xrun ioctl
  1051. *
  1052. * Change the RUNNING stream(s) to XRUN state.
  1053. */
  1054. static int snd_pcm_xrun(snd_pcm_substream_t *substream)
  1055. {
  1056. snd_card_t *card = substream->pcm->card;
  1057. snd_pcm_runtime_t *runtime = substream->runtime;
  1058. int result;
  1059. snd_power_lock(card);
  1060. if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1061. result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
  1062. if (result < 0)
  1063. goto _unlock;
  1064. }
  1065. snd_pcm_stream_lock_irq(substream);
  1066. switch (runtime->status->state) {
  1067. case SNDRV_PCM_STATE_XRUN:
  1068. result = 0; /* already there */
  1069. break;
  1070. case SNDRV_PCM_STATE_RUNNING:
  1071. result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  1072. break;
  1073. default:
  1074. result = -EBADFD;
  1075. }
  1076. snd_pcm_stream_unlock_irq(substream);
  1077. _unlock:
  1078. snd_power_unlock(card);
  1079. return result;
  1080. }
  1081. /*
  1082. * reset ioctl
  1083. */
  1084. static int snd_pcm_pre_reset(snd_pcm_substream_t * substream, int state)
  1085. {
  1086. snd_pcm_runtime_t *runtime = substream->runtime;
  1087. switch (runtime->status->state) {
  1088. case SNDRV_PCM_STATE_RUNNING:
  1089. case SNDRV_PCM_STATE_PREPARED:
  1090. case SNDRV_PCM_STATE_PAUSED:
  1091. case SNDRV_PCM_STATE_SUSPENDED:
  1092. return 0;
  1093. default:
  1094. return -EBADFD;
  1095. }
  1096. }
  1097. static int snd_pcm_do_reset(snd_pcm_substream_t * substream, int state)
  1098. {
  1099. snd_pcm_runtime_t *runtime = substream->runtime;
  1100. int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
  1101. if (err < 0)
  1102. return err;
  1103. // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
  1104. runtime->hw_ptr_base = 0;
  1105. runtime->hw_ptr_interrupt = runtime->status->hw_ptr - runtime->status->hw_ptr % runtime->period_size;
  1106. runtime->silence_start = runtime->status->hw_ptr;
  1107. runtime->silence_filled = 0;
  1108. return 0;
  1109. }
  1110. static void snd_pcm_post_reset(snd_pcm_substream_t * substream, int state)
  1111. {
  1112. snd_pcm_runtime_t *runtime = substream->runtime;
  1113. runtime->control->appl_ptr = runtime->status->hw_ptr;
  1114. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  1115. runtime->silence_size > 0)
  1116. snd_pcm_playback_silence(substream, ULONG_MAX);
  1117. }
  1118. static struct action_ops snd_pcm_action_reset = {
  1119. .pre_action = snd_pcm_pre_reset,
  1120. .do_action = snd_pcm_do_reset,
  1121. .post_action = snd_pcm_post_reset
  1122. };
  1123. static int snd_pcm_reset(snd_pcm_substream_t *substream)
  1124. {
  1125. return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
  1126. }
  1127. /*
  1128. * prepare ioctl
  1129. */
  1130. static int snd_pcm_pre_prepare(snd_pcm_substream_t * substream, int state)
  1131. {
  1132. snd_pcm_runtime_t *runtime = substream->runtime;
  1133. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1134. return -EBADFD;
  1135. if (snd_pcm_running(substream))
  1136. return -EBUSY;
  1137. return 0;
  1138. }
  1139. static int snd_pcm_do_prepare(snd_pcm_substream_t * substream, int state)
  1140. {
  1141. int err;
  1142. err = substream->ops->prepare(substream);
  1143. if (err < 0)
  1144. return err;
  1145. return snd_pcm_do_reset(substream, 0);
  1146. }
  1147. static void snd_pcm_post_prepare(snd_pcm_substream_t * substream, int state)
  1148. {
  1149. snd_pcm_runtime_t *runtime = substream->runtime;
  1150. runtime->control->appl_ptr = runtime->status->hw_ptr;
  1151. runtime->status->state = SNDRV_PCM_STATE_PREPARED;
  1152. }
  1153. static struct action_ops snd_pcm_action_prepare = {
  1154. .pre_action = snd_pcm_pre_prepare,
  1155. .do_action = snd_pcm_do_prepare,
  1156. .post_action = snd_pcm_post_prepare
  1157. };
  1158. /**
  1159. * snd_pcm_prepare
  1160. */
  1161. int snd_pcm_prepare(snd_pcm_substream_t *substream)
  1162. {
  1163. int res;
  1164. snd_card_t *card = substream->pcm->card;
  1165. snd_power_lock(card);
  1166. if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
  1167. res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare, substream, 0);
  1168. snd_power_unlock(card);
  1169. return res;
  1170. }
  1171. /*
  1172. * drain ioctl
  1173. */
  1174. static int snd_pcm_pre_drain_init(snd_pcm_substream_t * substream, int state)
  1175. {
  1176. if (substream->ffile->f_flags & O_NONBLOCK)
  1177. return -EAGAIN;
  1178. substream->runtime->trigger_master = substream;
  1179. return 0;
  1180. }
  1181. static int snd_pcm_do_drain_init(snd_pcm_substream_t * substream, int state)
  1182. {
  1183. snd_pcm_runtime_t *runtime = substream->runtime;
  1184. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1185. switch (runtime->status->state) {
  1186. case SNDRV_PCM_STATE_PREPARED:
  1187. /* start playback stream if possible */
  1188. if (! snd_pcm_playback_empty(substream)) {
  1189. snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
  1190. snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
  1191. }
  1192. break;
  1193. case SNDRV_PCM_STATE_RUNNING:
  1194. runtime->status->state = SNDRV_PCM_STATE_DRAINING;
  1195. break;
  1196. default:
  1197. break;
  1198. }
  1199. } else {
  1200. /* stop running stream */
  1201. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
  1202. int state = snd_pcm_capture_avail(runtime) > 0 ?
  1203. SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
  1204. snd_pcm_do_stop(substream, state);
  1205. snd_pcm_post_stop(substream, state);
  1206. }
  1207. }
  1208. return 0;
  1209. }
  1210. static void snd_pcm_post_drain_init(snd_pcm_substream_t * substream, int state)
  1211. {
  1212. }
  1213. static struct action_ops snd_pcm_action_drain_init = {
  1214. .pre_action = snd_pcm_pre_drain_init,
  1215. .do_action = snd_pcm_do_drain_init,
  1216. .post_action = snd_pcm_post_drain_init
  1217. };
  1218. struct drain_rec {
  1219. snd_pcm_substream_t *substream;
  1220. wait_queue_t wait;
  1221. snd_pcm_uframes_t stop_threshold;
  1222. };
  1223. static int snd_pcm_drop(snd_pcm_substream_t *substream);
  1224. /*
  1225. * Drain the stream(s).
  1226. * When the substream is linked, sync until the draining of all playback streams
  1227. * is finished.
  1228. * After this call, all streams are supposed to be either SETUP or DRAINING
  1229. * (capture only) state.
  1230. */
  1231. static int snd_pcm_drain(snd_pcm_substream_t *substream)
  1232. {
  1233. snd_card_t *card;
  1234. snd_pcm_runtime_t *runtime;
  1235. struct list_head *pos;
  1236. int result = 0;
  1237. int i, num_drecs;
  1238. struct drain_rec *drec, drec_tmp, *d;
  1239. snd_assert(substream != NULL, return -ENXIO);
  1240. card = substream->pcm->card;
  1241. runtime = substream->runtime;
  1242. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1243. return -EBADFD;
  1244. snd_power_lock(card);
  1245. if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1246. result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
  1247. if (result < 0) {
  1248. snd_power_unlock(card);
  1249. return result;
  1250. }
  1251. }
  1252. /* allocate temporary record for drain sync */
  1253. down_read(&snd_pcm_link_rwsem);
  1254. if (snd_pcm_stream_linked(substream)) {
  1255. drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
  1256. if (! drec) {
  1257. up_read(&snd_pcm_link_rwsem);
  1258. snd_power_unlock(card);
  1259. return -ENOMEM;
  1260. }
  1261. } else
  1262. drec = &drec_tmp;
  1263. /* count only playback streams */
  1264. num_drecs = 0;
  1265. snd_pcm_group_for_each(pos, substream) {
  1266. snd_pcm_substream_t *s = snd_pcm_group_substream_entry(pos);
  1267. runtime = s->runtime;
  1268. if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1269. d = &drec[num_drecs++];
  1270. d->substream = s;
  1271. init_waitqueue_entry(&d->wait, current);
  1272. add_wait_queue(&runtime->sleep, &d->wait);
  1273. /* stop_threshold fixup to avoid endless loop when
  1274. * stop_threshold > buffer_size
  1275. */
  1276. d->stop_threshold = runtime->stop_threshold;
  1277. if (runtime->stop_threshold > runtime->buffer_size)
  1278. runtime->stop_threshold = runtime->buffer_size;
  1279. }
  1280. }
  1281. up_read(&snd_pcm_link_rwsem);
  1282. if (! num_drecs)
  1283. goto _error;
  1284. snd_pcm_stream_lock_irq(substream);
  1285. /* resume pause */
  1286. if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
  1287. snd_pcm_pause(substream, 0);
  1288. /* pre-start/stop - all running streams are changed to DRAINING state */
  1289. result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
  1290. if (result < 0) {
  1291. snd_pcm_stream_unlock_irq(substream);
  1292. goto _error;
  1293. }
  1294. for (;;) {
  1295. long tout;
  1296. if (signal_pending(current)) {
  1297. result = -ERESTARTSYS;
  1298. break;
  1299. }
  1300. /* all finished? */
  1301. for (i = 0; i < num_drecs; i++) {
  1302. runtime = drec[i].substream->runtime;
  1303. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
  1304. break;
  1305. }
  1306. if (i == num_drecs)
  1307. break; /* yes, all drained */
  1308. set_current_state(TASK_INTERRUPTIBLE);
  1309. snd_pcm_stream_unlock_irq(substream);
  1310. snd_power_unlock(card);
  1311. tout = schedule_timeout(10 * HZ);
  1312. snd_power_lock(card);
  1313. snd_pcm_stream_lock_irq(substream);
  1314. if (tout == 0) {
  1315. if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
  1316. result = -ESTRPIPE;
  1317. else {
  1318. snd_printd("playback drain error (DMA or IRQ trouble?)\n");
  1319. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1320. result = -EIO;
  1321. }
  1322. break;
  1323. }
  1324. }
  1325. snd_pcm_stream_unlock_irq(substream);
  1326. _error:
  1327. for (i = 0; i < num_drecs; i++) {
  1328. d = &drec[i];
  1329. runtime = d->substream->runtime;
  1330. remove_wait_queue(&runtime->sleep, &d->wait);
  1331. runtime->stop_threshold = d->stop_threshold;
  1332. }
  1333. if (drec != &drec_tmp)
  1334. kfree(drec);
  1335. snd_power_unlock(card);
  1336. return result;
  1337. }
  1338. /*
  1339. * drop ioctl
  1340. *
  1341. * Immediately put all linked substreams into SETUP state.
  1342. */
  1343. static int snd_pcm_drop(snd_pcm_substream_t *substream)
  1344. {
  1345. snd_pcm_runtime_t *runtime;
  1346. snd_card_t *card;
  1347. int result = 0;
  1348. snd_assert(substream != NULL, return -ENXIO);
  1349. runtime = substream->runtime;
  1350. card = substream->pcm->card;
  1351. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1352. return -EBADFD;
  1353. snd_power_lock(card);
  1354. if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1355. result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
  1356. if (result < 0)
  1357. goto _unlock;
  1358. }
  1359. snd_pcm_stream_lock_irq(substream);
  1360. /* resume pause */
  1361. if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
  1362. snd_pcm_pause(substream, 0);
  1363. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1364. /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
  1365. snd_pcm_stream_unlock_irq(substream);
  1366. _unlock:
  1367. snd_power_unlock(card);
  1368. return result;
  1369. }
  1370. /* WARNING: Don't forget to fput back the file */
  1371. extern int snd_major;
  1372. static struct file *snd_pcm_file_fd(int fd)
  1373. {
  1374. struct file *file;
  1375. struct inode *inode;
  1376. unsigned short minor;
  1377. file = fget(fd);
  1378. if (!file)
  1379. return NULL;
  1380. inode = file->f_dentry->d_inode;
  1381. if (!S_ISCHR(inode->i_mode) ||
  1382. imajor(inode) != snd_major) {
  1383. fput(file);
  1384. return NULL;
  1385. }
  1386. minor = iminor(inode);
  1387. if (minor >= 256 ||
  1388. minor % SNDRV_MINOR_DEVICES < SNDRV_MINOR_PCM_PLAYBACK) {
  1389. fput(file);
  1390. return NULL;
  1391. }
  1392. return file;
  1393. }
  1394. /*
  1395. * PCM link handling
  1396. */
  1397. static int snd_pcm_link(snd_pcm_substream_t *substream, int fd)
  1398. {
  1399. int res = 0;
  1400. struct file *file;
  1401. snd_pcm_file_t *pcm_file;
  1402. snd_pcm_substream_t *substream1;
  1403. file = snd_pcm_file_fd(fd);
  1404. if (!file)
  1405. return -EBADFD;
  1406. pcm_file = file->private_data;
  1407. substream1 = pcm_file->substream;
  1408. down_write(&snd_pcm_link_rwsem);
  1409. write_lock_irq(&snd_pcm_link_rwlock);
  1410. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
  1411. substream->runtime->status->state != substream1->runtime->status->state) {
  1412. res = -EBADFD;
  1413. goto _end;
  1414. }
  1415. if (snd_pcm_stream_linked(substream1)) {
  1416. res = -EALREADY;
  1417. goto _end;
  1418. }
  1419. if (!snd_pcm_stream_linked(substream)) {
  1420. substream->group = kmalloc(sizeof(snd_pcm_group_t), GFP_ATOMIC);
  1421. if (substream->group == NULL) {
  1422. res = -ENOMEM;
  1423. goto _end;
  1424. }
  1425. spin_lock_init(&substream->group->lock);
  1426. INIT_LIST_HEAD(&substream->group->substreams);
  1427. list_add_tail(&substream->link_list, &substream->group->substreams);
  1428. substream->group->count = 1;
  1429. }
  1430. list_add_tail(&substream1->link_list, &substream->group->substreams);
  1431. substream->group->count++;
  1432. substream1->group = substream->group;
  1433. _end:
  1434. write_unlock_irq(&snd_pcm_link_rwlock);
  1435. up_write(&snd_pcm_link_rwsem);
  1436. fput(file);
  1437. return res;
  1438. }
  1439. static void relink_to_local(snd_pcm_substream_t *substream)
  1440. {
  1441. substream->group = &substream->self_group;
  1442. INIT_LIST_HEAD(&substream->self_group.substreams);
  1443. list_add_tail(&substream->link_list, &substream->self_group.substreams);
  1444. }
  1445. static int snd_pcm_unlink(snd_pcm_substream_t *substream)
  1446. {
  1447. struct list_head *pos;
  1448. int res = 0;
  1449. down_write(&snd_pcm_link_rwsem);
  1450. write_lock_irq(&snd_pcm_link_rwlock);
  1451. if (!snd_pcm_stream_linked(substream)) {
  1452. res = -EALREADY;
  1453. goto _end;
  1454. }
  1455. list_del(&substream->link_list);
  1456. substream->group->count--;
  1457. if (substream->group->count == 1) { /* detach the last stream, too */
  1458. snd_pcm_group_for_each(pos, substream) {
  1459. relink_to_local(snd_pcm_group_substream_entry(pos));
  1460. break;
  1461. }
  1462. kfree(substream->group);
  1463. }
  1464. relink_to_local(substream);
  1465. _end:
  1466. write_unlock_irq(&snd_pcm_link_rwlock);
  1467. up_write(&snd_pcm_link_rwsem);
  1468. return res;
  1469. }
  1470. /*
  1471. * hw configurator
  1472. */
  1473. static int snd_pcm_hw_rule_mul(snd_pcm_hw_params_t *params,
  1474. snd_pcm_hw_rule_t *rule)
  1475. {
  1476. snd_interval_t t;
  1477. snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
  1478. hw_param_interval_c(params, rule->deps[1]), &t);
  1479. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1480. }
  1481. static int snd_pcm_hw_rule_div(snd_pcm_hw_params_t *params,
  1482. snd_pcm_hw_rule_t *rule)
  1483. {
  1484. snd_interval_t t;
  1485. snd_interval_div(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_muldivk(snd_pcm_hw_params_t *params,
  1490. snd_pcm_hw_rule_t *rule)
  1491. {
  1492. snd_interval_t t;
  1493. snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
  1494. hw_param_interval_c(params, rule->deps[1]),
  1495. (unsigned long) rule->private, &t);
  1496. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1497. }
  1498. static int snd_pcm_hw_rule_mulkdiv(snd_pcm_hw_params_t *params,
  1499. snd_pcm_hw_rule_t *rule)
  1500. {
  1501. snd_interval_t t;
  1502. snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
  1503. (unsigned long) rule->private,
  1504. hw_param_interval_c(params, rule->deps[1]), &t);
  1505. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1506. }
  1507. static int snd_pcm_hw_rule_format(snd_pcm_hw_params_t *params,
  1508. snd_pcm_hw_rule_t *rule)
  1509. {
  1510. unsigned int k;
  1511. snd_interval_t *i = hw_param_interval(params, rule->deps[0]);
  1512. snd_mask_t m;
  1513. snd_mask_t *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  1514. snd_mask_any(&m);
  1515. for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
  1516. int bits;
  1517. if (! snd_mask_test(mask, k))
  1518. continue;
  1519. bits = snd_pcm_format_physical_width(k);
  1520. if (bits <= 0)
  1521. continue; /* ignore invalid formats */
  1522. if ((unsigned)bits < i->min || (unsigned)bits > i->max)
  1523. snd_mask_reset(&m, k);
  1524. }
  1525. return snd_mask_refine(mask, &m);
  1526. }
  1527. static int snd_pcm_hw_rule_sample_bits(snd_pcm_hw_params_t *params,
  1528. snd_pcm_hw_rule_t *rule)
  1529. {
  1530. snd_interval_t t;
  1531. unsigned int k;
  1532. t.min = UINT_MAX;
  1533. t.max = 0;
  1534. t.openmin = 0;
  1535. t.openmax = 0;
  1536. for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
  1537. int bits;
  1538. if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
  1539. continue;
  1540. bits = snd_pcm_format_physical_width(k);
  1541. if (bits <= 0)
  1542. continue; /* ignore invalid formats */
  1543. if (t.min > (unsigned)bits)
  1544. t.min = bits;
  1545. if (t.max < (unsigned)bits)
  1546. t.max = bits;
  1547. }
  1548. t.integer = 1;
  1549. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1550. }
  1551. #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
  1552. #error "Change this table"
  1553. #endif
  1554. static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
  1555. 48000, 64000, 88200, 96000, 176400, 192000 };
  1556. static int snd_pcm_hw_rule_rate(snd_pcm_hw_params_t *params,
  1557. snd_pcm_hw_rule_t *rule)
  1558. {
  1559. snd_pcm_hardware_t *hw = rule->private;
  1560. return snd_interval_list(hw_param_interval(params, rule->var),
  1561. ARRAY_SIZE(rates), rates, hw->rates);
  1562. }
  1563. static int snd_pcm_hw_rule_buffer_bytes_max(snd_pcm_hw_params_t *params,
  1564. snd_pcm_hw_rule_t *rule)
  1565. {
  1566. snd_interval_t t;
  1567. snd_pcm_substream_t *substream = rule->private;
  1568. t.min = 0;
  1569. t.max = substream->buffer_bytes_max;
  1570. t.openmin = 0;
  1571. t.openmax = 0;
  1572. t.integer = 1;
  1573. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1574. }
  1575. int snd_pcm_hw_constraints_init(snd_pcm_substream_t *substream)
  1576. {
  1577. snd_pcm_runtime_t *runtime = substream->runtime;
  1578. snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
  1579. int k, err;
  1580. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
  1581. snd_mask_any(constrs_mask(constrs, k));
  1582. }
  1583. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
  1584. snd_interval_any(constrs_interval(constrs, k));
  1585. }
  1586. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
  1587. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
  1588. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
  1589. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
  1590. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
  1591. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  1592. snd_pcm_hw_rule_format, NULL,
  1593. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1594. if (err < 0)
  1595. return err;
  1596. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  1597. snd_pcm_hw_rule_sample_bits, NULL,
  1598. SNDRV_PCM_HW_PARAM_FORMAT,
  1599. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1600. if (err < 0)
  1601. return err;
  1602. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  1603. snd_pcm_hw_rule_div, NULL,
  1604. SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  1605. if (err < 0)
  1606. return err;
  1607. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1608. snd_pcm_hw_rule_mul, NULL,
  1609. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  1610. if (err < 0)
  1611. return err;
  1612. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1613. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1614. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
  1615. if (err < 0)
  1616. return err;
  1617. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1618. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1619. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
  1620. if (err < 0)
  1621. return err;
  1622. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  1623. snd_pcm_hw_rule_div, NULL,
  1624. SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1625. if (err < 0)
  1626. return err;
  1627. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1628. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1629. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
  1630. if (err < 0)
  1631. return err;
  1632. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1633. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1634. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
  1635. if (err < 0)
  1636. return err;
  1637. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
  1638. snd_pcm_hw_rule_div, NULL,
  1639. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
  1640. if (err < 0)
  1641. return err;
  1642. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1643. snd_pcm_hw_rule_div, NULL,
  1644. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
  1645. if (err < 0)
  1646. return err;
  1647. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1648. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1649. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1650. if (err < 0)
  1651. return err;
  1652. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1653. snd_pcm_hw_rule_muldivk, (void*) 1000000,
  1654. SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
  1655. if (err < 0)
  1656. return err;
  1657. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1658. snd_pcm_hw_rule_mul, NULL,
  1659. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
  1660. if (err < 0)
  1661. return err;
  1662. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1663. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1664. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1665. if (err < 0)
  1666. return err;
  1667. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1668. snd_pcm_hw_rule_muldivk, (void*) 1000000,
  1669. SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
  1670. if (err < 0)
  1671. return err;
  1672. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1673. snd_pcm_hw_rule_muldivk, (void*) 8,
  1674. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1675. if (err < 0)
  1676. return err;
  1677. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1678. snd_pcm_hw_rule_muldivk, (void*) 8,
  1679. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1680. if (err < 0)
  1681. return err;
  1682. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1683. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1684. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
  1685. if (err < 0)
  1686. return err;
  1687. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
  1688. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1689. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
  1690. if (err < 0)
  1691. return err;
  1692. return 0;
  1693. }
  1694. int snd_pcm_hw_constraints_complete(snd_pcm_substream_t *substream)
  1695. {
  1696. snd_pcm_runtime_t *runtime = substream->runtime;
  1697. snd_pcm_hardware_t *hw = &runtime->hw;
  1698. int err;
  1699. unsigned int mask = 0;
  1700. if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
  1701. mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  1702. if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
  1703. mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
  1704. if (hw->info & SNDRV_PCM_INFO_MMAP) {
  1705. if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
  1706. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
  1707. if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
  1708. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
  1709. if (hw->info & SNDRV_PCM_INFO_COMPLEX)
  1710. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
  1711. }
  1712. err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
  1713. snd_assert(err >= 0, return -EINVAL);
  1714. err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
  1715. snd_assert(err >= 0, return -EINVAL);
  1716. err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
  1717. snd_assert(err >= 0, return -EINVAL);
  1718. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
  1719. hw->channels_min, hw->channels_max);
  1720. snd_assert(err >= 0, return -EINVAL);
  1721. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
  1722. hw->rate_min, hw->rate_max);
  1723. snd_assert(err >= 0, return -EINVAL);
  1724. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1725. hw->period_bytes_min, hw->period_bytes_max);
  1726. snd_assert(err >= 0, return -EINVAL);
  1727. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
  1728. hw->periods_min, hw->periods_max);
  1729. snd_assert(err >= 0, return -EINVAL);
  1730. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1731. hw->period_bytes_min, hw->buffer_bytes_max);
  1732. snd_assert(err >= 0, return -EINVAL);
  1733. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1734. snd_pcm_hw_rule_buffer_bytes_max, substream,
  1735. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
  1736. if (err < 0)
  1737. return err;
  1738. /* FIXME: remove */
  1739. if (runtime->dma_bytes) {
  1740. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
  1741. snd_assert(err >= 0, return -EINVAL);
  1742. }
  1743. if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
  1744. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1745. snd_pcm_hw_rule_rate, hw,
  1746. SNDRV_PCM_HW_PARAM_RATE, -1);
  1747. if (err < 0)
  1748. return err;
  1749. }
  1750. /* FIXME: this belong to lowlevel */
  1751. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
  1752. 1000000 / HZ, 1000000 / HZ);
  1753. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
  1754. return 0;
  1755. }
  1756. static void snd_pcm_add_file(snd_pcm_str_t *str,
  1757. snd_pcm_file_t *pcm_file)
  1758. {
  1759. pcm_file->next = str->files;
  1760. str->files = pcm_file;
  1761. }
  1762. static void snd_pcm_remove_file(snd_pcm_str_t *str,
  1763. snd_pcm_file_t *pcm_file)
  1764. {
  1765. snd_pcm_file_t * pcm_file1;
  1766. if (str->files == pcm_file) {
  1767. str->files = pcm_file->next;
  1768. } else {
  1769. pcm_file1 = str->files;
  1770. while (pcm_file1 && pcm_file1->next != pcm_file)
  1771. pcm_file1 = pcm_file1->next;
  1772. if (pcm_file1 != NULL)
  1773. pcm_file1->next = pcm_file->next;
  1774. }
  1775. }
  1776. static int snd_pcm_release_file(snd_pcm_file_t * pcm_file)
  1777. {
  1778. snd_pcm_substream_t *substream;
  1779. snd_pcm_runtime_t *runtime;
  1780. snd_pcm_str_t * str;
  1781. snd_assert(pcm_file != NULL, return -ENXIO);
  1782. substream = pcm_file->substream;
  1783. snd_assert(substream != NULL, return -ENXIO);
  1784. runtime = substream->runtime;
  1785. str = substream->pstr;
  1786. snd_pcm_unlink(substream);
  1787. if (substream->ffile != NULL) {
  1788. if (substream->ops->hw_free != NULL)
  1789. substream->ops->hw_free(substream);
  1790. substream->ops->close(substream);
  1791. substream->ffile = NULL;
  1792. }
  1793. snd_pcm_remove_file(str, pcm_file);
  1794. snd_pcm_release_substream(substream);
  1795. kfree(pcm_file);
  1796. return 0;
  1797. }
  1798. static int snd_pcm_open_file(struct file *file,
  1799. snd_pcm_t *pcm,
  1800. int stream,
  1801. snd_pcm_file_t **rpcm_file)
  1802. {
  1803. int err = 0;
  1804. snd_pcm_file_t *pcm_file;
  1805. snd_pcm_substream_t *substream;
  1806. snd_pcm_str_t *str;
  1807. snd_assert(rpcm_file != NULL, return -EINVAL);
  1808. *rpcm_file = NULL;
  1809. pcm_file = kcalloc(1, sizeof(*pcm_file), GFP_KERNEL);
  1810. if (pcm_file == NULL) {
  1811. return -ENOMEM;
  1812. }
  1813. if ((err = snd_pcm_open_substream(pcm, stream, &substream)) < 0) {
  1814. kfree(pcm_file);
  1815. return err;
  1816. }
  1817. str = substream->pstr;
  1818. substream->file = pcm_file;
  1819. substream->no_mmap_ctrl = 0;
  1820. pcm_file->substream = substream;
  1821. snd_pcm_add_file(str, pcm_file);
  1822. err = snd_pcm_hw_constraints_init(substream);
  1823. if (err < 0) {
  1824. snd_printd("snd_pcm_hw_constraints_init failed\n");
  1825. snd_pcm_release_file(pcm_file);
  1826. return err;
  1827. }
  1828. if ((err = substream->ops->open(substream)) < 0) {
  1829. snd_pcm_release_file(pcm_file);
  1830. return err;
  1831. }
  1832. substream->ffile = file;
  1833. err = snd_pcm_hw_constraints_complete(substream);
  1834. if (err < 0) {
  1835. snd_printd("snd_pcm_hw_constraints_complete failed\n");
  1836. snd_pcm_release_file(pcm_file);
  1837. return err;
  1838. }
  1839. file->private_data = pcm_file;
  1840. *rpcm_file = pcm_file;
  1841. return 0;
  1842. }
  1843. static int snd_pcm_open(struct inode *inode, struct file *file)
  1844. {
  1845. int cardnum = SNDRV_MINOR_CARD(iminor(inode));
  1846. int device = SNDRV_MINOR_DEVICE(iminor(inode));
  1847. int err;
  1848. snd_pcm_t *pcm;
  1849. snd_pcm_file_t *pcm_file;
  1850. wait_queue_t wait;
  1851. snd_runtime_check(device >= SNDRV_MINOR_PCM_PLAYBACK && device < SNDRV_MINOR_DEVICES, return -ENXIO);
  1852. pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + (device % SNDRV_MINOR_PCMS)];
  1853. if (pcm == NULL) {
  1854. err = -ENODEV;
  1855. goto __error1;
  1856. }
  1857. err = snd_card_file_add(pcm->card, file);
  1858. if (err < 0)
  1859. goto __error1;
  1860. if (!try_module_get(pcm->card->module)) {
  1861. err = -EFAULT;
  1862. goto __error2;
  1863. }
  1864. init_waitqueue_entry(&wait, current);
  1865. add_wait_queue(&pcm->open_wait, &wait);
  1866. down(&pcm->open_mutex);
  1867. while (1) {
  1868. err = snd_pcm_open_file(file, pcm, device >= SNDRV_MINOR_PCM_CAPTURE ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK, &pcm_file);
  1869. if (err >= 0)
  1870. break;
  1871. if (err == -EAGAIN) {
  1872. if (file->f_flags & O_NONBLOCK) {
  1873. err = -EBUSY;
  1874. break;
  1875. }
  1876. } else
  1877. break;
  1878. set_current_state(TASK_INTERRUPTIBLE);
  1879. up(&pcm->open_mutex);
  1880. schedule();
  1881. down(&pcm->open_mutex);
  1882. if (signal_pending(current)) {
  1883. err = -ERESTARTSYS;
  1884. break;
  1885. }
  1886. }
  1887. remove_wait_queue(&pcm->open_wait, &wait);
  1888. up(&pcm->open_mutex);
  1889. if (err < 0)
  1890. goto __error;
  1891. return err;
  1892. __error:
  1893. module_put(pcm->card->module);
  1894. __error2:
  1895. snd_card_file_remove(pcm->card, file);
  1896. __error1:
  1897. return err;
  1898. }
  1899. static int snd_pcm_release(struct inode *inode, struct file *file)
  1900. {
  1901. snd_pcm_t *pcm;
  1902. snd_pcm_substream_t *substream;
  1903. snd_pcm_file_t *pcm_file;
  1904. pcm_file = file->private_data;
  1905. substream = pcm_file->substream;
  1906. snd_assert(substream != NULL, return -ENXIO);
  1907. snd_assert(!atomic_read(&substream->runtime->mmap_count), );
  1908. pcm = substream->pcm;
  1909. snd_pcm_drop(substream);
  1910. fasync_helper(-1, file, 0, &substream->runtime->fasync);
  1911. down(&pcm->open_mutex);
  1912. snd_pcm_release_file(pcm_file);
  1913. up(&pcm->open_mutex);
  1914. wake_up(&pcm->open_wait);
  1915. module_put(pcm->card->module);
  1916. snd_card_file_remove(pcm->card, file);
  1917. return 0;
  1918. }
  1919. static snd_pcm_sframes_t snd_pcm_playback_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
  1920. {
  1921. snd_pcm_runtime_t *runtime = substream->runtime;
  1922. snd_pcm_sframes_t appl_ptr;
  1923. snd_pcm_sframes_t ret;
  1924. snd_pcm_sframes_t hw_avail;
  1925. if (frames == 0)
  1926. return 0;
  1927. snd_pcm_stream_lock_irq(substream);
  1928. switch (runtime->status->state) {
  1929. case SNDRV_PCM_STATE_PREPARED:
  1930. break;
  1931. case SNDRV_PCM_STATE_DRAINING:
  1932. case SNDRV_PCM_STATE_RUNNING:
  1933. if (snd_pcm_update_hw_ptr(substream) >= 0)
  1934. break;
  1935. /* Fall through */
  1936. case SNDRV_PCM_STATE_XRUN:
  1937. ret = -EPIPE;
  1938. goto __end;
  1939. default:
  1940. ret = -EBADFD;
  1941. goto __end;
  1942. }
  1943. hw_avail = snd_pcm_playback_hw_avail(runtime);
  1944. if (hw_avail <= 0) {
  1945. ret = 0;
  1946. goto __end;
  1947. }
  1948. if (frames > (snd_pcm_uframes_t)hw_avail)
  1949. frames = hw_avail;
  1950. else
  1951. frames -= frames % runtime->xfer_align;
  1952. appl_ptr = runtime->control->appl_ptr - frames;
  1953. if (appl_ptr < 0)
  1954. appl_ptr += runtime->boundary;
  1955. runtime->control->appl_ptr = appl_ptr;
  1956. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
  1957. runtime->sleep_min)
  1958. snd_pcm_tick_prepare(substream);
  1959. ret = frames;
  1960. __end:
  1961. snd_pcm_stream_unlock_irq(substream);
  1962. return ret;
  1963. }
  1964. static snd_pcm_sframes_t snd_pcm_capture_rewind(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
  1965. {
  1966. snd_pcm_runtime_t *runtime = substream->runtime;
  1967. snd_pcm_sframes_t appl_ptr;
  1968. snd_pcm_sframes_t ret;
  1969. snd_pcm_sframes_t hw_avail;
  1970. if (frames == 0)
  1971. return 0;
  1972. snd_pcm_stream_lock_irq(substream);
  1973. switch (runtime->status->state) {
  1974. case SNDRV_PCM_STATE_PREPARED:
  1975. case SNDRV_PCM_STATE_DRAINING:
  1976. break;
  1977. case SNDRV_PCM_STATE_RUNNING:
  1978. if (snd_pcm_update_hw_ptr(substream) >= 0)
  1979. break;
  1980. /* Fall through */
  1981. case SNDRV_PCM_STATE_XRUN:
  1982. ret = -EPIPE;
  1983. goto __end;
  1984. default:
  1985. ret = -EBADFD;
  1986. goto __end;
  1987. }
  1988. hw_avail = snd_pcm_capture_hw_avail(runtime);
  1989. if (hw_avail <= 0) {
  1990. ret = 0;
  1991. goto __end;
  1992. }
  1993. if (frames > (snd_pcm_uframes_t)hw_avail)
  1994. frames = hw_avail;
  1995. else
  1996. frames -= frames % runtime->xfer_align;
  1997. appl_ptr = runtime->control->appl_ptr - frames;
  1998. if (appl_ptr < 0)
  1999. appl_ptr += runtime->boundary;
  2000. runtime->control->appl_ptr = appl_ptr;
  2001. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
  2002. runtime->sleep_min)
  2003. snd_pcm_tick_prepare(substream);
  2004. ret = frames;
  2005. __end:
  2006. snd_pcm_stream_unlock_irq(substream);
  2007. return ret;
  2008. }
  2009. static snd_pcm_sframes_t snd_pcm_playback_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
  2010. {
  2011. snd_pcm_runtime_t *runtime = substream->runtime;
  2012. snd_pcm_sframes_t appl_ptr;
  2013. snd_pcm_sframes_t ret;
  2014. snd_pcm_sframes_t avail;
  2015. if (frames == 0)
  2016. return 0;
  2017. snd_pcm_stream_lock_irq(substream);
  2018. switch (runtime->status->state) {
  2019. case SNDRV_PCM_STATE_PREPARED:
  2020. case SNDRV_PCM_STATE_PAUSED:
  2021. break;
  2022. case SNDRV_PCM_STATE_DRAINING:
  2023. case SNDRV_PCM_STATE_RUNNING:
  2024. if (snd_pcm_update_hw_ptr(substream) >= 0)
  2025. break;
  2026. /* Fall through */
  2027. case SNDRV_PCM_STATE_XRUN:
  2028. ret = -EPIPE;
  2029. goto __end;
  2030. default:
  2031. ret = -EBADFD;
  2032. goto __end;
  2033. }
  2034. avail = snd_pcm_playback_avail(runtime);
  2035. if (avail <= 0) {
  2036. ret = 0;
  2037. goto __end;
  2038. }
  2039. if (frames > (snd_pcm_uframes_t)avail)
  2040. frames = avail;
  2041. else
  2042. frames -= frames % runtime->xfer_align;
  2043. appl_ptr = runtime->control->appl_ptr + frames;
  2044. if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
  2045. appl_ptr -= runtime->boundary;
  2046. runtime->control->appl_ptr = appl_ptr;
  2047. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
  2048. runtime->sleep_min)
  2049. snd_pcm_tick_prepare(substream);
  2050. ret = frames;
  2051. __end:
  2052. snd_pcm_stream_unlock_irq(substream);
  2053. return ret;
  2054. }
  2055. static snd_pcm_sframes_t snd_pcm_capture_forward(snd_pcm_substream_t *substream, snd_pcm_uframes_t frames)
  2056. {
  2057. snd_pcm_runtime_t *runtime = substream->runtime;
  2058. snd_pcm_sframes_t appl_ptr;
  2059. snd_pcm_sframes_t ret;
  2060. snd_pcm_sframes_t avail;
  2061. if (frames == 0)
  2062. return 0;
  2063. snd_pcm_stream_lock_irq(substream);
  2064. switch (runtime->status->state) {
  2065. case SNDRV_PCM_STATE_PREPARED:
  2066. case SNDRV_PCM_STATE_DRAINING:
  2067. case SNDRV_PCM_STATE_PAUSED:
  2068. break;
  2069. case SNDRV_PCM_STATE_RUNNING:
  2070. if (snd_pcm_update_hw_ptr(substream) >= 0)
  2071. break;
  2072. /* Fall through */
  2073. case SNDRV_PCM_STATE_XRUN:
  2074. ret = -EPIPE;
  2075. goto __end;
  2076. default:
  2077. ret = -EBADFD;
  2078. goto __end;
  2079. }
  2080. avail = snd_pcm_capture_avail(runtime);
  2081. if (avail <= 0) {
  2082. ret = 0;
  2083. goto __end;
  2084. }
  2085. if (frames > (snd_pcm_uframes_t)avail)
  2086. frames = avail;
  2087. else
  2088. frames -= frames % runtime->xfer_align;
  2089. appl_ptr = runtime->control->appl_ptr + frames;
  2090. if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
  2091. appl_ptr -= runtime->boundary;
  2092. runtime->control->appl_ptr = appl_ptr;
  2093. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
  2094. runtime->sleep_min)
  2095. snd_pcm_tick_prepare(substream);
  2096. ret = frames;
  2097. __end:
  2098. snd_pcm_stream_unlock_irq(substream);
  2099. return ret;
  2100. }
  2101. static int snd_pcm_hwsync(snd_pcm_substream_t *substream)
  2102. {
  2103. snd_pcm_runtime_t *runtime = substream->runtime;
  2104. int err;
  2105. snd_pcm_stream_lock_irq(substream);
  2106. switch (runtime->status->state) {
  2107. case SNDRV_PCM_STATE_DRAINING:
  2108. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  2109. goto __badfd;
  2110. case SNDRV_PCM_STATE_RUNNING:
  2111. if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
  2112. break;
  2113. /* Fall through */
  2114. case SNDRV_PCM_STATE_PREPARED:
  2115. case SNDRV_PCM_STATE_SUSPENDED:
  2116. err = 0;
  2117. break;
  2118. case SNDRV_PCM_STATE_XRUN:
  2119. err = -EPIPE;
  2120. break;
  2121. default:
  2122. __badfd:
  2123. err = -EBADFD;
  2124. break;
  2125. }
  2126. snd_pcm_stream_unlock_irq(substream);
  2127. return err;
  2128. }
  2129. static int snd_pcm_delay(snd_pcm_substream_t *substream, snd_pcm_sframes_t __user *res)
  2130. {
  2131. snd_pcm_runtime_t *runtime = substream->runtime;
  2132. int err;
  2133. snd_pcm_sframes_t n = 0;
  2134. snd_pcm_stream_lock_irq(substream);
  2135. switch (runtime->status->state) {
  2136. case SNDRV_PCM_STATE_DRAINING:
  2137. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  2138. goto __badfd;
  2139. case SNDRV_PCM_STATE_RUNNING:
  2140. if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
  2141. break;
  2142. /* Fall through */
  2143. case SNDRV_PCM_STATE_PREPARED:
  2144. case SNDRV_PCM_STATE_SUSPENDED:
  2145. err = 0;
  2146. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  2147. n = snd_pcm_playback_hw_avail(runtime);
  2148. else
  2149. n = snd_pcm_capture_avail(runtime);
  2150. break;
  2151. case SNDRV_PCM_STATE_XRUN:
  2152. err = -EPIPE;
  2153. break;
  2154. default:
  2155. __badfd:
  2156. err = -EBADFD;
  2157. break;
  2158. }
  2159. snd_pcm_stream_unlock_irq(substream);
  2160. if (!err)
  2161. if (put_user(n, res))
  2162. err = -EFAULT;
  2163. return err;
  2164. }
  2165. static int snd_pcm_sync_ptr(snd_pcm_substream_t *substream, struct sndrv_pcm_sync_ptr __user *_sync_ptr)
  2166. {
  2167. snd_pcm_runtime_t *runtime = substream->runtime;
  2168. struct sndrv_pcm_sync_ptr sync_ptr;
  2169. volatile struct sndrv_pcm_mmap_status *status;
  2170. volatile struct sndrv_pcm_mmap_control *control;
  2171. int err;
  2172. memset(&sync_ptr, 0, sizeof(sync_ptr));
  2173. if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
  2174. return -EFAULT;
  2175. if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct sndrv_pcm_mmap_control)))
  2176. return -EFAULT;
  2177. status = runtime->status;
  2178. control = runtime->control;
  2179. if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
  2180. err = snd_pcm_hwsync(substream);
  2181. if (err < 0)
  2182. return err;
  2183. }
  2184. snd_pcm_stream_lock_irq(substream);
  2185. if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
  2186. control->appl_ptr = sync_ptr.c.control.appl_ptr;
  2187. else
  2188. sync_ptr.c.control.appl_ptr = control->appl_ptr;
  2189. if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
  2190. control->avail_min = sync_ptr.c.control.avail_min;
  2191. else
  2192. sync_ptr.c.control.avail_min = control->avail_min;
  2193. sync_ptr.s.status.state = status->state;
  2194. sync_ptr.s.status.hw_ptr = status->hw_ptr;
  2195. sync_ptr.s.status.tstamp = status->tstamp;
  2196. sync_ptr.s.status.suspended_state = status->suspended_state;
  2197. snd_pcm_stream_unlock_irq(substream);
  2198. if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
  2199. return -EFAULT;
  2200. return 0;
  2201. }
  2202. static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
  2203. unsigned int cmd, void __user *arg);
  2204. static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
  2205. unsigned int cmd, void __user *arg);
  2206. static int snd_pcm_common_ioctl1(snd_pcm_substream_t *substream,
  2207. unsigned int cmd, void __user *arg)
  2208. {
  2209. snd_assert(substream != NULL, return -ENXIO);
  2210. switch (cmd) {
  2211. case SNDRV_PCM_IOCTL_PVERSION:
  2212. return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
  2213. case SNDRV_PCM_IOCTL_INFO:
  2214. return snd_pcm_info_user(substream, arg);
  2215. case SNDRV_PCM_IOCTL_TSTAMP:
  2216. {
  2217. int xarg;
  2218. if (get_user(xarg, (int __user *)arg))
  2219. return -EFAULT;
  2220. substream->runtime->tstamp_timespec = xarg ? 1 : 0;
  2221. return 0;
  2222. }
  2223. case SNDRV_PCM_IOCTL_HW_REFINE:
  2224. return snd_pcm_hw_refine_user(substream, arg);
  2225. case SNDRV_PCM_IOCTL_HW_PARAMS:
  2226. return snd_pcm_hw_params_user(substream, arg);
  2227. case SNDRV_PCM_IOCTL_HW_FREE:
  2228. return snd_pcm_hw_free(substream);
  2229. case SNDRV_PCM_IOCTL_SW_PARAMS:
  2230. return snd_pcm_sw_params_user(substream, arg);
  2231. case SNDRV_PCM_IOCTL_STATUS:
  2232. return snd_pcm_status_user(substream, arg);
  2233. case SNDRV_PCM_IOCTL_CHANNEL_INFO:
  2234. return snd_pcm_channel_info_user(substream, arg);
  2235. case SNDRV_PCM_IOCTL_PREPARE:
  2236. return snd_pcm_prepare(substream);
  2237. case SNDRV_PCM_IOCTL_RESET:
  2238. return snd_pcm_reset(substream);
  2239. case SNDRV_PCM_IOCTL_START:
  2240. return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
  2241. case SNDRV_PCM_IOCTL_LINK:
  2242. return snd_pcm_link(substream, (int)(unsigned long) arg);
  2243. case SNDRV_PCM_IOCTL_UNLINK:
  2244. return snd_pcm_unlink(substream);
  2245. case SNDRV_PCM_IOCTL_RESUME:
  2246. return snd_pcm_resume(substream);
  2247. case SNDRV_PCM_IOCTL_XRUN:
  2248. return snd_pcm_xrun(substream);
  2249. case SNDRV_PCM_IOCTL_HWSYNC:
  2250. return snd_pcm_hwsync(substream);
  2251. case SNDRV_PCM_IOCTL_DELAY:
  2252. return snd_pcm_delay(substream, arg);
  2253. case SNDRV_PCM_IOCTL_SYNC_PTR:
  2254. return snd_pcm_sync_ptr(substream, arg);
  2255. case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
  2256. return snd_pcm_hw_refine_old_user(substream, arg);
  2257. case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
  2258. return snd_pcm_hw_params_old_user(substream, arg);
  2259. case SNDRV_PCM_IOCTL_DRAIN:
  2260. return snd_pcm_drain(substream);
  2261. case SNDRV_PCM_IOCTL_DROP:
  2262. return snd_pcm_drop(substream);
  2263. }
  2264. snd_printd("unknown ioctl = 0x%x\n", cmd);
  2265. return -ENOTTY;
  2266. }
  2267. static int snd_pcm_playback_ioctl1(snd_pcm_substream_t *substream,
  2268. unsigned int cmd, void __user *arg)
  2269. {
  2270. snd_assert(substream != NULL, return -ENXIO);
  2271. snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
  2272. switch (cmd) {
  2273. case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
  2274. {
  2275. snd_xferi_t xferi;
  2276. snd_xferi_t __user *_xferi = arg;
  2277. snd_pcm_runtime_t *runtime = substream->runtime;
  2278. snd_pcm_sframes_t result;
  2279. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2280. return -EBADFD;
  2281. if (put_user(0, &_xferi->result))
  2282. return -EFAULT;
  2283. if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
  2284. return -EFAULT;
  2285. result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
  2286. __put_user(result, &_xferi->result);
  2287. return result < 0 ? result : 0;
  2288. }
  2289. case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
  2290. {
  2291. snd_xfern_t xfern;
  2292. snd_xfern_t __user *_xfern = arg;
  2293. snd_pcm_runtime_t *runtime = substream->runtime;
  2294. void __user **bufs;
  2295. snd_pcm_sframes_t result;
  2296. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2297. return -EBADFD;
  2298. if (runtime->channels > 128)
  2299. return -EINVAL;
  2300. if (put_user(0, &_xfern->result))
  2301. return -EFAULT;
  2302. if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
  2303. return -EFAULT;
  2304. bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
  2305. if (bufs == NULL)
  2306. return -ENOMEM;
  2307. if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
  2308. kfree(bufs);
  2309. return -EFAULT;
  2310. }
  2311. result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
  2312. kfree(bufs);
  2313. __put_user(result, &_xfern->result);
  2314. return result < 0 ? result : 0;
  2315. }
  2316. case SNDRV_PCM_IOCTL_REWIND:
  2317. {
  2318. snd_pcm_uframes_t frames;
  2319. snd_pcm_uframes_t __user *_frames = arg;
  2320. snd_pcm_sframes_t result;
  2321. if (get_user(frames, _frames))
  2322. return -EFAULT;
  2323. if (put_user(0, _frames))
  2324. return -EFAULT;
  2325. result = snd_pcm_playback_rewind(substream, frames);
  2326. __put_user(result, _frames);
  2327. return result < 0 ? result : 0;
  2328. }
  2329. case SNDRV_PCM_IOCTL_FORWARD:
  2330. {
  2331. snd_pcm_uframes_t frames;
  2332. snd_pcm_uframes_t __user *_frames = arg;
  2333. snd_pcm_sframes_t result;
  2334. if (get_user(frames, _frames))
  2335. return -EFAULT;
  2336. if (put_user(0, _frames))
  2337. return -EFAULT;
  2338. result = snd_pcm_playback_forward(substream, frames);
  2339. __put_user(result, _frames);
  2340. return result < 0 ? result : 0;
  2341. }
  2342. case SNDRV_PCM_IOCTL_PAUSE:
  2343. {
  2344. int res;
  2345. snd_pcm_stream_lock_irq(substream);
  2346. res = snd_pcm_pause(substream, (int)(unsigned long)arg);
  2347. snd_pcm_stream_unlock_irq(substream);
  2348. return res;
  2349. }
  2350. }
  2351. return snd_pcm_common_ioctl1(substream, cmd, arg);
  2352. }
  2353. static int snd_pcm_capture_ioctl1(snd_pcm_substream_t *substream,
  2354. unsigned int cmd, void __user *arg)
  2355. {
  2356. snd_assert(substream != NULL, return -ENXIO);
  2357. snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
  2358. switch (cmd) {
  2359. case SNDRV_PCM_IOCTL_READI_FRAMES:
  2360. {
  2361. snd_xferi_t xferi;
  2362. snd_xferi_t __user *_xferi = arg;
  2363. snd_pcm_runtime_t *runtime = substream->runtime;
  2364. snd_pcm_sframes_t result;
  2365. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2366. return -EBADFD;
  2367. if (put_user(0, &_xferi->result))
  2368. return -EFAULT;
  2369. if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
  2370. return -EFAULT;
  2371. result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
  2372. __put_user(result, &_xferi->result);
  2373. return result < 0 ? result : 0;
  2374. }
  2375. case SNDRV_PCM_IOCTL_READN_FRAMES:
  2376. {
  2377. snd_xfern_t xfern;
  2378. snd_xfern_t __user *_xfern = arg;
  2379. snd_pcm_runtime_t *runtime = substream->runtime;
  2380. void *bufs;
  2381. snd_pcm_sframes_t result;
  2382. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2383. return -EBADFD;
  2384. if (runtime->channels > 128)
  2385. return -EINVAL;
  2386. if (put_user(0, &_xfern->result))
  2387. return -EFAULT;
  2388. if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
  2389. return -EFAULT;
  2390. bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
  2391. if (bufs == NULL)
  2392. return -ENOMEM;
  2393. if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
  2394. kfree(bufs);
  2395. return -EFAULT;
  2396. }
  2397. result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
  2398. kfree(bufs);
  2399. __put_user(result, &_xfern->result);
  2400. return result < 0 ? result : 0;
  2401. }
  2402. case SNDRV_PCM_IOCTL_REWIND:
  2403. {
  2404. snd_pcm_uframes_t frames;
  2405. snd_pcm_uframes_t __user *_frames = arg;
  2406. snd_pcm_sframes_t result;
  2407. if (get_user(frames, _frames))
  2408. return -EFAULT;
  2409. if (put_user(0, _frames))
  2410. return -EFAULT;
  2411. result = snd_pcm_capture_rewind(substream, frames);
  2412. __put_user(result, _frames);
  2413. return result < 0 ? result : 0;
  2414. }
  2415. case SNDRV_PCM_IOCTL_FORWARD:
  2416. {
  2417. snd_pcm_uframes_t frames;
  2418. snd_pcm_uframes_t __user *_frames = arg;
  2419. snd_pcm_sframes_t result;
  2420. if (get_user(frames, _frames))
  2421. return -EFAULT;
  2422. if (put_user(0, _frames))
  2423. return -EFAULT;
  2424. result = snd_pcm_capture_forward(substream, frames);
  2425. __put_user(result, _frames);
  2426. return result < 0 ? result : 0;
  2427. }
  2428. }
  2429. return snd_pcm_common_ioctl1(substream, cmd, arg);
  2430. }
  2431. static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2432. {
  2433. snd_pcm_file_t *pcm_file;
  2434. pcm_file = file->private_data;
  2435. if (((cmd >> 8) & 0xff) != 'A')
  2436. return -ENOTTY;
  2437. return snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
  2438. }
  2439. static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2440. {
  2441. snd_pcm_file_t *pcm_file;
  2442. pcm_file = file->private_data;
  2443. if (((cmd >> 8) & 0xff) != 'A')
  2444. return -ENOTTY;
  2445. return snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
  2446. }
  2447. int snd_pcm_kernel_playback_ioctl(snd_pcm_substream_t *substream,
  2448. unsigned int cmd, void *arg)
  2449. {
  2450. mm_segment_t fs;
  2451. int result;
  2452. fs = snd_enter_user();
  2453. result = snd_pcm_playback_ioctl1(substream, cmd, (void __user *)arg);
  2454. snd_leave_user(fs);
  2455. return result;
  2456. }
  2457. int snd_pcm_kernel_capture_ioctl(snd_pcm_substream_t *substream,
  2458. unsigned int cmd, void *arg)
  2459. {
  2460. mm_segment_t fs;
  2461. int result;
  2462. fs = snd_enter_user();
  2463. result = snd_pcm_capture_ioctl1(substream, cmd, (void __user *)arg);
  2464. snd_leave_user(fs);
  2465. return result;
  2466. }
  2467. int snd_pcm_kernel_ioctl(snd_pcm_substream_t *substream,
  2468. unsigned int cmd, void *arg)
  2469. {
  2470. switch (substream->stream) {
  2471. case SNDRV_PCM_STREAM_PLAYBACK:
  2472. return snd_pcm_kernel_playback_ioctl(substream, cmd, arg);
  2473. case SNDRV_PCM_STREAM_CAPTURE:
  2474. return snd_pcm_kernel_capture_ioctl(substream, cmd, arg);
  2475. default:
  2476. return -EINVAL;
  2477. }
  2478. }
  2479. static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count, loff_t * offset)
  2480. {
  2481. snd_pcm_file_t *pcm_file;
  2482. snd_pcm_substream_t *substream;
  2483. snd_pcm_runtime_t *runtime;
  2484. snd_pcm_sframes_t result;
  2485. pcm_file = file->private_data;
  2486. substream = pcm_file->substream;
  2487. snd_assert(substream != NULL, return -ENXIO);
  2488. runtime = substream->runtime;
  2489. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2490. return -EBADFD;
  2491. if (!frame_aligned(runtime, count))
  2492. return -EINVAL;
  2493. count = bytes_to_frames(runtime, count);
  2494. result = snd_pcm_lib_read(substream, buf, count);
  2495. if (result > 0)
  2496. result = frames_to_bytes(runtime, result);
  2497. return result;
  2498. }
  2499. static ssize_t snd_pcm_write(struct file *file, const char __user *buf, size_t count, loff_t * offset)
  2500. {
  2501. snd_pcm_file_t *pcm_file;
  2502. snd_pcm_substream_t *substream;
  2503. snd_pcm_runtime_t *runtime;
  2504. snd_pcm_sframes_t result;
  2505. pcm_file = file->private_data;
  2506. substream = pcm_file->substream;
  2507. snd_assert(substream != NULL, result = -ENXIO; goto end);
  2508. runtime = substream->runtime;
  2509. if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  2510. result = -EBADFD;
  2511. goto end;
  2512. }
  2513. if (!frame_aligned(runtime, count)) {
  2514. result = -EINVAL;
  2515. goto end;
  2516. }
  2517. count = bytes_to_frames(runtime, count);
  2518. result = snd_pcm_lib_write(substream, buf, count);
  2519. if (result > 0)
  2520. result = frames_to_bytes(runtime, result);
  2521. end:
  2522. return result;
  2523. }
  2524. static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
  2525. unsigned long count, loff_t * offset)
  2526. {
  2527. snd_pcm_file_t *pcm_file;
  2528. snd_pcm_substream_t *substream;
  2529. snd_pcm_runtime_t *runtime;
  2530. snd_pcm_sframes_t result;
  2531. unsigned long i;
  2532. void __user **bufs;
  2533. snd_pcm_uframes_t frames;
  2534. pcm_file = file->private_data;
  2535. substream = pcm_file->substream;
  2536. snd_assert(substream != NULL, return -ENXIO);
  2537. runtime = substream->runtime;
  2538. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2539. return -EBADFD;
  2540. if (count > 1024 || count != runtime->channels)
  2541. return -EINVAL;
  2542. if (!frame_aligned(runtime, _vector->iov_len))
  2543. return -EINVAL;
  2544. frames = bytes_to_samples(runtime, _vector->iov_len);
  2545. bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
  2546. if (bufs == NULL)
  2547. return -ENOMEM;
  2548. for (i = 0; i < count; ++i)
  2549. bufs[i] = _vector[i].iov_base;
  2550. result = snd_pcm_lib_readv(substream, bufs, frames);
  2551. if (result > 0)
  2552. result = frames_to_bytes(runtime, result);
  2553. kfree(bufs);
  2554. return result;
  2555. }
  2556. static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
  2557. unsigned long count, loff_t * offset)
  2558. {
  2559. snd_pcm_file_t *pcm_file;
  2560. snd_pcm_substream_t *substream;
  2561. snd_pcm_runtime_t *runtime;
  2562. snd_pcm_sframes_t result;
  2563. unsigned long i;
  2564. void __user **bufs;
  2565. snd_pcm_uframes_t frames;
  2566. pcm_file = file->private_data;
  2567. substream = pcm_file->substream;
  2568. snd_assert(substream != NULL, result = -ENXIO; goto end);
  2569. runtime = substream->runtime;
  2570. if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  2571. result = -EBADFD;
  2572. goto end;
  2573. }
  2574. if (count > 128 || count != runtime->channels ||
  2575. !frame_aligned(runtime, _vector->iov_len)) {
  2576. result = -EINVAL;
  2577. goto end;
  2578. }
  2579. frames = bytes_to_samples(runtime, _vector->iov_len);
  2580. bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
  2581. if (bufs == NULL)
  2582. return -ENOMEM;
  2583. for (i = 0; i < count; ++i)
  2584. bufs[i] = _vector[i].iov_base;
  2585. result = snd_pcm_lib_writev(substream, bufs, frames);
  2586. if (result > 0)
  2587. result = frames_to_bytes(runtime, result);
  2588. kfree(bufs);
  2589. end:
  2590. return result;
  2591. }
  2592. static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
  2593. {
  2594. snd_pcm_file_t *pcm_file;
  2595. snd_pcm_substream_t *substream;
  2596. snd_pcm_runtime_t *runtime;
  2597. unsigned int mask;
  2598. snd_pcm_uframes_t avail;
  2599. pcm_file = file->private_data;
  2600. substream = pcm_file->substream;
  2601. snd_assert(substream != NULL, return -ENXIO);
  2602. runtime = substream->runtime;
  2603. poll_wait(file, &runtime->sleep, wait);
  2604. snd_pcm_stream_lock_irq(substream);
  2605. avail = snd_pcm_playback_avail(runtime);
  2606. switch (runtime->status->state) {
  2607. case SNDRV_PCM_STATE_RUNNING:
  2608. case SNDRV_PCM_STATE_PREPARED:
  2609. case SNDRV_PCM_STATE_PAUSED:
  2610. if (avail >= runtime->control->avail_min) {
  2611. mask = POLLOUT | POLLWRNORM;
  2612. break;
  2613. }
  2614. /* Fall through */
  2615. case SNDRV_PCM_STATE_DRAINING:
  2616. mask = 0;
  2617. break;
  2618. default:
  2619. mask = POLLOUT | POLLWRNORM | POLLERR;
  2620. break;
  2621. }
  2622. snd_pcm_stream_unlock_irq(substream);
  2623. return mask;
  2624. }
  2625. static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
  2626. {
  2627. snd_pcm_file_t *pcm_file;
  2628. snd_pcm_substream_t *substream;
  2629. snd_pcm_runtime_t *runtime;
  2630. unsigned int mask;
  2631. snd_pcm_uframes_t avail;
  2632. pcm_file = file->private_data;
  2633. substream = pcm_file->substream;
  2634. snd_assert(substream != NULL, return -ENXIO);
  2635. runtime = substream->runtime;
  2636. poll_wait(file, &runtime->sleep, wait);
  2637. snd_pcm_stream_lock_irq(substream);
  2638. avail = snd_pcm_capture_avail(runtime);
  2639. switch (runtime->status->state) {
  2640. case SNDRV_PCM_STATE_RUNNING:
  2641. case SNDRV_PCM_STATE_PREPARED:
  2642. case SNDRV_PCM_STATE_PAUSED:
  2643. if (avail >= runtime->control->avail_min) {
  2644. mask = POLLIN | POLLRDNORM;
  2645. break;
  2646. }
  2647. mask = 0;
  2648. break;
  2649. case SNDRV_PCM_STATE_DRAINING:
  2650. if (avail > 0) {
  2651. mask = POLLIN | POLLRDNORM;
  2652. break;
  2653. }
  2654. /* Fall through */
  2655. default:
  2656. mask = POLLIN | POLLRDNORM | POLLERR;
  2657. break;
  2658. }
  2659. snd_pcm_stream_unlock_irq(substream);
  2660. return mask;
  2661. }
  2662. /*
  2663. * mmap support
  2664. */
  2665. /*
  2666. * Only on coherent architectures, we can mmap the status and the control records
  2667. * for effcient data transfer. On others, we have to use HWSYNC ioctl...
  2668. */
  2669. #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
  2670. /*
  2671. * mmap status record
  2672. */
  2673. static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area, unsigned long address, int *type)
  2674. {
  2675. snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
  2676. snd_pcm_runtime_t *runtime;
  2677. struct page * page;
  2678. if (substream == NULL)
  2679. return NOPAGE_OOM;
  2680. runtime = substream->runtime;
  2681. page = virt_to_page(runtime->status);
  2682. if (!PageReserved(page))
  2683. get_page(page);
  2684. if (type)
  2685. *type = VM_FAULT_MINOR;
  2686. return page;
  2687. }
  2688. static struct vm_operations_struct snd_pcm_vm_ops_status =
  2689. {
  2690. .nopage = snd_pcm_mmap_status_nopage,
  2691. };
  2692. static int snd_pcm_mmap_status(snd_pcm_substream_t *substream, struct file *file,
  2693. struct vm_area_struct *area)
  2694. {
  2695. snd_pcm_runtime_t *runtime;
  2696. long size;
  2697. if (!(area->vm_flags & VM_READ))
  2698. return -EINVAL;
  2699. runtime = substream->runtime;
  2700. snd_assert(runtime != NULL, return -EAGAIN);
  2701. size = area->vm_end - area->vm_start;
  2702. if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_status_t)))
  2703. return -EINVAL;
  2704. area->vm_ops = &snd_pcm_vm_ops_status;
  2705. area->vm_private_data = substream;
  2706. area->vm_flags |= VM_RESERVED;
  2707. return 0;
  2708. }
  2709. /*
  2710. * mmap control record
  2711. */
  2712. static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area, unsigned long address, int *type)
  2713. {
  2714. snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
  2715. snd_pcm_runtime_t *runtime;
  2716. struct page * page;
  2717. if (substream == NULL)
  2718. return NOPAGE_OOM;
  2719. runtime = substream->runtime;
  2720. page = virt_to_page(runtime->control);
  2721. if (!PageReserved(page))
  2722. get_page(page);
  2723. if (type)
  2724. *type = VM_FAULT_MINOR;
  2725. return page;
  2726. }
  2727. static struct vm_operations_struct snd_pcm_vm_ops_control =
  2728. {
  2729. .nopage = snd_pcm_mmap_control_nopage,
  2730. };
  2731. static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
  2732. struct vm_area_struct *area)
  2733. {
  2734. snd_pcm_runtime_t *runtime;
  2735. long size;
  2736. if (!(area->vm_flags & VM_READ))
  2737. return -EINVAL;
  2738. runtime = substream->runtime;
  2739. snd_assert(runtime != NULL, return -EAGAIN);
  2740. size = area->vm_end - area->vm_start;
  2741. if (size != PAGE_ALIGN(sizeof(snd_pcm_mmap_control_t)))
  2742. return -EINVAL;
  2743. area->vm_ops = &snd_pcm_vm_ops_control;
  2744. area->vm_private_data = substream;
  2745. area->vm_flags |= VM_RESERVED;
  2746. return 0;
  2747. }
  2748. #else /* ! coherent mmap */
  2749. /*
  2750. * don't support mmap for status and control records.
  2751. */
  2752. static int snd_pcm_mmap_status(snd_pcm_substream_t *substream, struct file *file,
  2753. struct vm_area_struct *area)
  2754. {
  2755. return -ENXIO;
  2756. }
  2757. static int snd_pcm_mmap_control(snd_pcm_substream_t *substream, struct file *file,
  2758. struct vm_area_struct *area)
  2759. {
  2760. return -ENXIO;
  2761. }
  2762. #endif /* coherent mmap */
  2763. /*
  2764. * nopage callback for mmapping a RAM page
  2765. */
  2766. static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area, unsigned long address, int *type)
  2767. {
  2768. snd_pcm_substream_t *substream = (snd_pcm_substream_t *)area->vm_private_data;
  2769. snd_pcm_runtime_t *runtime;
  2770. unsigned long offset;
  2771. struct page * page;
  2772. void *vaddr;
  2773. size_t dma_bytes;
  2774. if (substream == NULL)
  2775. return NOPAGE_OOM;
  2776. runtime = substream->runtime;
  2777. offset = area->vm_pgoff << PAGE_SHIFT;
  2778. offset += address - area->vm_start;
  2779. snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
  2780. dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
  2781. if (offset > dma_bytes - PAGE_SIZE)
  2782. return NOPAGE_SIGBUS;
  2783. if (substream->ops->page) {
  2784. page = substream->ops->page(substream, offset);
  2785. if (! page)
  2786. return NOPAGE_OOM;
  2787. } else {
  2788. vaddr = runtime->dma_area + offset;
  2789. page = virt_to_page(vaddr);
  2790. }
  2791. if (!PageReserved(page))
  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. };