pcm_native.c 94 KB

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