pcm_native.c 91 KB

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