pcm_native.c 93 KB

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