pcm_native.c 92 KB

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