pcm_native.c 91 KB

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