pcm_native.c 92 KB

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