pcm_native.c 91 KB

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