pcm_native.c 94 KB

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