pcm_native.c 93 KB

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