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/file.h>
  23. #include <linux/slab.h>
  24. #include <linux/time.h>
  25. #include <linux/pm_qos_params.h>
  26. #include <linux/uio.h>
  27. #include <linux/dma-mapping.h>
  28. #include <sound/core.h>
  29. #include <sound/control.h>
  30. #include <sound/info.h>
  31. #include <sound/pcm.h>
  32. #include <sound/pcm_params.h>
  33. #include <sound/timer.h>
  34. #include <sound/minors.h>
  35. #include <asm/io.h>
  36. #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
  37. #include <dma-coherence.h>
  38. #endif
  39. /*
  40. * Compatibility
  41. */
  42. struct snd_pcm_hw_params_old {
  43. unsigned int flags;
  44. unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
  45. SNDRV_PCM_HW_PARAM_ACCESS + 1];
  46. struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
  47. SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
  48. unsigned int rmask;
  49. unsigned int cmask;
  50. unsigned int info;
  51. unsigned int msbits;
  52. unsigned int rate_num;
  53. unsigned int rate_den;
  54. snd_pcm_uframes_t fifo_size;
  55. unsigned char reserved[64];
  56. };
  57. #ifdef CONFIG_SND_SUPPORT_OLD_API
  58. #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
  59. #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
  60. static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
  61. struct snd_pcm_hw_params_old __user * _oparams);
  62. static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
  63. struct snd_pcm_hw_params_old __user * _oparams);
  64. #endif
  65. static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
  66. /*
  67. *
  68. */
  69. DEFINE_RWLOCK(snd_pcm_link_rwlock);
  70. EXPORT_SYMBOL(snd_pcm_link_rwlock);
  71. static DECLARE_RWSEM(snd_pcm_link_rwsem);
  72. static inline mm_segment_t snd_enter_user(void)
  73. {
  74. mm_segment_t fs = get_fs();
  75. set_fs(get_ds());
  76. return fs;
  77. }
  78. static inline void snd_leave_user(mm_segment_t fs)
  79. {
  80. set_fs(fs);
  81. }
  82. int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
  83. {
  84. struct snd_pcm_runtime *runtime;
  85. struct snd_pcm *pcm = substream->pcm;
  86. struct snd_pcm_str *pstr = substream->pstr;
  87. memset(info, 0, sizeof(*info));
  88. info->card = pcm->card->number;
  89. info->device = pcm->device;
  90. info->stream = substream->stream;
  91. info->subdevice = substream->number;
  92. strlcpy(info->id, pcm->id, sizeof(info->id));
  93. strlcpy(info->name, pcm->name, sizeof(info->name));
  94. info->dev_class = pcm->dev_class;
  95. info->dev_subclass = pcm->dev_subclass;
  96. info->subdevices_count = pstr->substream_count;
  97. info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
  98. strlcpy(info->subname, substream->name, sizeof(info->subname));
  99. runtime = substream->runtime;
  100. /* AB: FIXME!!! This is definitely nonsense */
  101. if (runtime) {
  102. info->sync = runtime->sync;
  103. substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
  104. }
  105. return 0;
  106. }
  107. int snd_pcm_info_user(struct snd_pcm_substream *substream,
  108. struct snd_pcm_info __user * _info)
  109. {
  110. struct snd_pcm_info *info;
  111. int err;
  112. info = kmalloc(sizeof(*info), GFP_KERNEL);
  113. if (! info)
  114. return -ENOMEM;
  115. err = snd_pcm_info(substream, info);
  116. if (err >= 0) {
  117. if (copy_to_user(_info, info, sizeof(*info)))
  118. err = -EFAULT;
  119. }
  120. kfree(info);
  121. return err;
  122. }
  123. #undef RULES_DEBUG
  124. #ifdef RULES_DEBUG
  125. #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
  126. static const char * const snd_pcm_hw_param_names[] = {
  127. HW_PARAM(ACCESS),
  128. HW_PARAM(FORMAT),
  129. HW_PARAM(SUBFORMAT),
  130. HW_PARAM(SAMPLE_BITS),
  131. HW_PARAM(FRAME_BITS),
  132. HW_PARAM(CHANNELS),
  133. HW_PARAM(RATE),
  134. HW_PARAM(PERIOD_TIME),
  135. HW_PARAM(PERIOD_SIZE),
  136. HW_PARAM(PERIOD_BYTES),
  137. HW_PARAM(PERIODS),
  138. HW_PARAM(BUFFER_TIME),
  139. HW_PARAM(BUFFER_SIZE),
  140. HW_PARAM(BUFFER_BYTES),
  141. HW_PARAM(TICK_TIME),
  142. };
  143. #endif
  144. int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
  145. struct snd_pcm_hw_params *params)
  146. {
  147. unsigned int k;
  148. struct snd_pcm_hardware *hw;
  149. struct snd_interval *i = NULL;
  150. struct snd_mask *m = NULL;
  151. struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
  152. unsigned int rstamps[constrs->rules_num];
  153. unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
  154. unsigned int stamp = 2;
  155. int changed, again;
  156. params->info = 0;
  157. params->fifo_size = 0;
  158. if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
  159. params->msbits = 0;
  160. if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
  161. params->rate_num = 0;
  162. params->rate_den = 0;
  163. }
  164. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
  165. m = hw_param_mask(params, k);
  166. if (snd_mask_empty(m))
  167. return -EINVAL;
  168. if (!(params->rmask & (1 << k)))
  169. continue;
  170. #ifdef RULES_DEBUG
  171. printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
  172. printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
  173. #endif
  174. changed = snd_mask_refine(m, constrs_mask(constrs, k));
  175. #ifdef RULES_DEBUG
  176. printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
  177. #endif
  178. if (changed)
  179. params->cmask |= 1 << k;
  180. if (changed < 0)
  181. return changed;
  182. }
  183. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
  184. i = hw_param_interval(params, k);
  185. if (snd_interval_empty(i))
  186. return -EINVAL;
  187. if (!(params->rmask & (1 << k)))
  188. continue;
  189. #ifdef RULES_DEBUG
  190. printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
  191. if (i->empty)
  192. printk("empty");
  193. else
  194. printk("%c%u %u%c",
  195. i->openmin ? '(' : '[', i->min,
  196. i->max, i->openmax ? ')' : ']');
  197. printk(" -> ");
  198. #endif
  199. changed = snd_interval_refine(i, constrs_interval(constrs, k));
  200. #ifdef RULES_DEBUG
  201. if (i->empty)
  202. printk("empty\n");
  203. else
  204. printk("%c%u %u%c\n",
  205. i->openmin ? '(' : '[', i->min,
  206. i->max, i->openmax ? ')' : ']');
  207. #endif
  208. if (changed)
  209. params->cmask |= 1 << k;
  210. if (changed < 0)
  211. return changed;
  212. }
  213. for (k = 0; k < constrs->rules_num; k++)
  214. rstamps[k] = 0;
  215. for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
  216. vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
  217. do {
  218. again = 0;
  219. for (k = 0; k < constrs->rules_num; k++) {
  220. struct snd_pcm_hw_rule *r = &constrs->rules[k];
  221. unsigned int d;
  222. int doit = 0;
  223. if (r->cond && !(r->cond & params->flags))
  224. continue;
  225. for (d = 0; r->deps[d] >= 0; d++) {
  226. if (vstamps[r->deps[d]] > rstamps[k]) {
  227. doit = 1;
  228. break;
  229. }
  230. }
  231. if (!doit)
  232. continue;
  233. #ifdef RULES_DEBUG
  234. printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
  235. if (r->var >= 0) {
  236. printk("%s = ", snd_pcm_hw_param_names[r->var]);
  237. if (hw_is_mask(r->var)) {
  238. m = hw_param_mask(params, r->var);
  239. printk("%x", *m->bits);
  240. } else {
  241. i = hw_param_interval(params, r->var);
  242. if (i->empty)
  243. printk("empty");
  244. else
  245. printk("%c%u %u%c",
  246. i->openmin ? '(' : '[', i->min,
  247. i->max, i->openmax ? ')' : ']');
  248. }
  249. }
  250. #endif
  251. changed = r->func(params, r);
  252. #ifdef RULES_DEBUG
  253. if (r->var >= 0) {
  254. printk(" -> ");
  255. if (hw_is_mask(r->var))
  256. printk("%x", *m->bits);
  257. else {
  258. if (i->empty)
  259. printk("empty");
  260. else
  261. printk("%c%u %u%c",
  262. i->openmin ? '(' : '[', i->min,
  263. i->max, i->openmax ? ')' : ']');
  264. }
  265. }
  266. printk("\n");
  267. #endif
  268. rstamps[k] = stamp;
  269. if (changed && r->var >= 0) {
  270. params->cmask |= (1 << r->var);
  271. vstamps[r->var] = stamp;
  272. again = 1;
  273. }
  274. if (changed < 0)
  275. return changed;
  276. stamp++;
  277. }
  278. } while (again);
  279. if (!params->msbits) {
  280. i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
  281. if (snd_interval_single(i))
  282. params->msbits = snd_interval_value(i);
  283. }
  284. if (!params->rate_den) {
  285. i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  286. if (snd_interval_single(i)) {
  287. params->rate_num = snd_interval_value(i);
  288. params->rate_den = 1;
  289. }
  290. }
  291. hw = &substream->runtime->hw;
  292. if (!params->info)
  293. params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
  294. if (!params->fifo_size) {
  295. m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  296. i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  297. if (snd_mask_min(m) == snd_mask_max(m) &&
  298. snd_interval_min(i) == snd_interval_max(i)) {
  299. changed = substream->ops->ioctl(substream,
  300. SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
  301. if (changed < 0)
  302. return changed;
  303. }
  304. }
  305. params->rmask = 0;
  306. return 0;
  307. }
  308. EXPORT_SYMBOL(snd_pcm_hw_refine);
  309. static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
  310. struct snd_pcm_hw_params __user * _params)
  311. {
  312. struct snd_pcm_hw_params *params;
  313. int err;
  314. params = memdup_user(_params, sizeof(*params));
  315. if (IS_ERR(params))
  316. return PTR_ERR(params);
  317. err = snd_pcm_hw_refine(substream, params);
  318. if (copy_to_user(_params, params, sizeof(*params))) {
  319. if (!err)
  320. err = -EFAULT;
  321. }
  322. kfree(params);
  323. return err;
  324. }
  325. static int period_to_usecs(struct snd_pcm_runtime *runtime)
  326. {
  327. int usecs;
  328. if (! runtime->rate)
  329. return -1; /* invalid */
  330. /* take 75% of period time as the deadline */
  331. usecs = (750000 / runtime->rate) * runtime->period_size;
  332. usecs += ((750000 % runtime->rate) * runtime->period_size) /
  333. runtime->rate;
  334. return usecs;
  335. }
  336. static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
  337. struct snd_pcm_hw_params *params)
  338. {
  339. struct snd_pcm_runtime *runtime;
  340. int err, usecs;
  341. unsigned int bits;
  342. snd_pcm_uframes_t frames;
  343. if (PCM_RUNTIME_CHECK(substream))
  344. return -ENXIO;
  345. runtime = substream->runtime;
  346. snd_pcm_stream_lock_irq(substream);
  347. switch (runtime->status->state) {
  348. case SNDRV_PCM_STATE_OPEN:
  349. case SNDRV_PCM_STATE_SETUP:
  350. case SNDRV_PCM_STATE_PREPARED:
  351. break;
  352. default:
  353. snd_pcm_stream_unlock_irq(substream);
  354. return -EBADFD;
  355. }
  356. snd_pcm_stream_unlock_irq(substream);
  357. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  358. if (!substream->oss.oss)
  359. #endif
  360. if (atomic_read(&substream->mmap_count))
  361. return -EBADFD;
  362. params->rmask = ~0U;
  363. err = snd_pcm_hw_refine(substream, params);
  364. if (err < 0)
  365. goto _error;
  366. err = snd_pcm_hw_params_choose(substream, params);
  367. if (err < 0)
  368. goto _error;
  369. if (substream->ops->hw_params != NULL) {
  370. err = substream->ops->hw_params(substream, params);
  371. if (err < 0)
  372. goto _error;
  373. }
  374. runtime->access = params_access(params);
  375. runtime->format = params_format(params);
  376. runtime->subformat = params_subformat(params);
  377. runtime->channels = params_channels(params);
  378. runtime->rate = params_rate(params);
  379. runtime->period_size = params_period_size(params);
  380. runtime->periods = params_periods(params);
  381. runtime->buffer_size = params_buffer_size(params);
  382. runtime->info = params->info;
  383. runtime->rate_num = params->rate_num;
  384. runtime->rate_den = params->rate_den;
  385. runtime->no_period_wakeup =
  386. (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
  387. (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
  388. bits = snd_pcm_format_physical_width(runtime->format);
  389. runtime->sample_bits = bits;
  390. bits *= runtime->channels;
  391. runtime->frame_bits = bits;
  392. frames = 1;
  393. while (bits % 8 != 0) {
  394. bits *= 2;
  395. frames *= 2;
  396. }
  397. runtime->byte_align = bits / 8;
  398. runtime->min_align = frames;
  399. /* Default sw params */
  400. runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
  401. runtime->period_step = 1;
  402. runtime->control->avail_min = runtime->period_size;
  403. runtime->start_threshold = 1;
  404. runtime->stop_threshold = runtime->buffer_size;
  405. runtime->silence_threshold = 0;
  406. runtime->silence_size = 0;
  407. runtime->boundary = runtime->buffer_size;
  408. while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
  409. runtime->boundary *= 2;
  410. snd_pcm_timer_resolution_change(substream);
  411. runtime->status->state = SNDRV_PCM_STATE_SETUP;
  412. if (pm_qos_request_active(&substream->latency_pm_qos_req))
  413. pm_qos_remove_request(&substream->latency_pm_qos_req);
  414. if ((usecs = period_to_usecs(runtime)) >= 0)
  415. pm_qos_add_request(&substream->latency_pm_qos_req,
  416. PM_QOS_CPU_DMA_LATENCY, usecs);
  417. return 0;
  418. _error:
  419. /* hardware might be unusable from this time,
  420. so we force application to retry to set
  421. the correct hardware parameter settings */
  422. runtime->status->state = SNDRV_PCM_STATE_OPEN;
  423. if (substream->ops->hw_free != NULL)
  424. substream->ops->hw_free(substream);
  425. return err;
  426. }
  427. static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
  428. struct snd_pcm_hw_params __user * _params)
  429. {
  430. struct snd_pcm_hw_params *params;
  431. int err;
  432. params = memdup_user(_params, sizeof(*params));
  433. if (IS_ERR(params))
  434. return PTR_ERR(params);
  435. err = snd_pcm_hw_params(substream, params);
  436. if (copy_to_user(_params, params, sizeof(*params))) {
  437. if (!err)
  438. err = -EFAULT;
  439. }
  440. kfree(params);
  441. return err;
  442. }
  443. static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
  444. {
  445. struct snd_pcm_runtime *runtime;
  446. int result = 0;
  447. if (PCM_RUNTIME_CHECK(substream))
  448. return -ENXIO;
  449. runtime = substream->runtime;
  450. snd_pcm_stream_lock_irq(substream);
  451. switch (runtime->status->state) {
  452. case SNDRV_PCM_STATE_SETUP:
  453. case SNDRV_PCM_STATE_PREPARED:
  454. break;
  455. default:
  456. snd_pcm_stream_unlock_irq(substream);
  457. return -EBADFD;
  458. }
  459. snd_pcm_stream_unlock_irq(substream);
  460. if (atomic_read(&substream->mmap_count))
  461. return -EBADFD;
  462. if (substream->ops->hw_free)
  463. result = substream->ops->hw_free(substream);
  464. runtime->status->state = SNDRV_PCM_STATE_OPEN;
  465. pm_qos_remove_request(&substream->latency_pm_qos_req);
  466. return result;
  467. }
  468. static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
  469. struct snd_pcm_sw_params *params)
  470. {
  471. struct snd_pcm_runtime *runtime;
  472. int err;
  473. if (PCM_RUNTIME_CHECK(substream))
  474. return -ENXIO;
  475. runtime = substream->runtime;
  476. snd_pcm_stream_lock_irq(substream);
  477. if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  478. snd_pcm_stream_unlock_irq(substream);
  479. return -EBADFD;
  480. }
  481. snd_pcm_stream_unlock_irq(substream);
  482. if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
  483. return -EINVAL;
  484. if (params->avail_min == 0)
  485. return -EINVAL;
  486. if (params->silence_size >= runtime->boundary) {
  487. if (params->silence_threshold != 0)
  488. return -EINVAL;
  489. } else {
  490. if (params->silence_size > params->silence_threshold)
  491. return -EINVAL;
  492. if (params->silence_threshold > runtime->buffer_size)
  493. return -EINVAL;
  494. }
  495. err = 0;
  496. snd_pcm_stream_lock_irq(substream);
  497. runtime->tstamp_mode = params->tstamp_mode;
  498. runtime->period_step = params->period_step;
  499. runtime->control->avail_min = params->avail_min;
  500. runtime->start_threshold = params->start_threshold;
  501. runtime->stop_threshold = params->stop_threshold;
  502. runtime->silence_threshold = params->silence_threshold;
  503. runtime->silence_size = params->silence_size;
  504. params->boundary = runtime->boundary;
  505. if (snd_pcm_running(substream)) {
  506. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  507. runtime->silence_size > 0)
  508. snd_pcm_playback_silence(substream, ULONG_MAX);
  509. err = snd_pcm_update_state(substream, runtime);
  510. }
  511. snd_pcm_stream_unlock_irq(substream);
  512. return err;
  513. }
  514. static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
  515. struct snd_pcm_sw_params __user * _params)
  516. {
  517. struct snd_pcm_sw_params params;
  518. int err;
  519. if (copy_from_user(&params, _params, sizeof(params)))
  520. return -EFAULT;
  521. err = snd_pcm_sw_params(substream, &params);
  522. if (copy_to_user(_params, &params, sizeof(params)))
  523. return -EFAULT;
  524. return err;
  525. }
  526. int snd_pcm_status(struct snd_pcm_substream *substream,
  527. struct snd_pcm_status *status)
  528. {
  529. struct snd_pcm_runtime *runtime = substream->runtime;
  530. snd_pcm_stream_lock_irq(substream);
  531. status->state = runtime->status->state;
  532. status->suspended_state = runtime->status->suspended_state;
  533. if (status->state == SNDRV_PCM_STATE_OPEN)
  534. goto _end;
  535. status->trigger_tstamp = runtime->trigger_tstamp;
  536. if (snd_pcm_running(substream)) {
  537. snd_pcm_update_hw_ptr(substream);
  538. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
  539. status->tstamp = runtime->status->tstamp;
  540. goto _tstamp_end;
  541. }
  542. }
  543. snd_pcm_gettime(runtime, &status->tstamp);
  544. _tstamp_end:
  545. status->appl_ptr = runtime->control->appl_ptr;
  546. status->hw_ptr = runtime->status->hw_ptr;
  547. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  548. status->avail = snd_pcm_playback_avail(runtime);
  549. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
  550. runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  551. status->delay = runtime->buffer_size - status->avail;
  552. status->delay += runtime->delay;
  553. } else
  554. status->delay = 0;
  555. } else {
  556. status->avail = snd_pcm_capture_avail(runtime);
  557. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
  558. status->delay = status->avail + runtime->delay;
  559. else
  560. status->delay = 0;
  561. }
  562. status->avail_max = runtime->avail_max;
  563. status->overrange = runtime->overrange;
  564. runtime->avail_max = 0;
  565. runtime->overrange = 0;
  566. _end:
  567. snd_pcm_stream_unlock_irq(substream);
  568. return 0;
  569. }
  570. static int snd_pcm_status_user(struct snd_pcm_substream *substream,
  571. struct snd_pcm_status __user * _status)
  572. {
  573. struct snd_pcm_status status;
  574. int res;
  575. memset(&status, 0, sizeof(status));
  576. res = snd_pcm_status(substream, &status);
  577. if (res < 0)
  578. return res;
  579. if (copy_to_user(_status, &status, sizeof(status)))
  580. return -EFAULT;
  581. return 0;
  582. }
  583. static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
  584. struct snd_pcm_channel_info * info)
  585. {
  586. struct snd_pcm_runtime *runtime;
  587. unsigned int channel;
  588. channel = info->channel;
  589. runtime = substream->runtime;
  590. snd_pcm_stream_lock_irq(substream);
  591. if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
  592. snd_pcm_stream_unlock_irq(substream);
  593. return -EBADFD;
  594. }
  595. snd_pcm_stream_unlock_irq(substream);
  596. if (channel >= runtime->channels)
  597. return -EINVAL;
  598. memset(info, 0, sizeof(*info));
  599. info->channel = channel;
  600. return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
  601. }
  602. static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
  603. struct snd_pcm_channel_info __user * _info)
  604. {
  605. struct snd_pcm_channel_info info;
  606. int res;
  607. if (copy_from_user(&info, _info, sizeof(info)))
  608. return -EFAULT;
  609. res = snd_pcm_channel_info(substream, &info);
  610. if (res < 0)
  611. return res;
  612. if (copy_to_user(_info, &info, sizeof(info)))
  613. return -EFAULT;
  614. return 0;
  615. }
  616. static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
  617. {
  618. struct snd_pcm_runtime *runtime = substream->runtime;
  619. if (runtime->trigger_master == NULL)
  620. return;
  621. if (runtime->trigger_master == substream) {
  622. snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
  623. } else {
  624. snd_pcm_trigger_tstamp(runtime->trigger_master);
  625. runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
  626. }
  627. runtime->trigger_master = NULL;
  628. }
  629. struct action_ops {
  630. int (*pre_action)(struct snd_pcm_substream *substream, int state);
  631. int (*do_action)(struct snd_pcm_substream *substream, int state);
  632. void (*undo_action)(struct snd_pcm_substream *substream, int state);
  633. void (*post_action)(struct snd_pcm_substream *substream, int state);
  634. };
  635. /*
  636. * this functions is core for handling of linked stream
  637. * Note: the stream state might be changed also on failure
  638. * Note2: call with calling stream lock + link lock
  639. */
  640. static int snd_pcm_action_group(struct action_ops *ops,
  641. struct snd_pcm_substream *substream,
  642. int state, int do_lock)
  643. {
  644. struct snd_pcm_substream *s = NULL;
  645. struct snd_pcm_substream *s1;
  646. int res = 0;
  647. snd_pcm_group_for_each_entry(s, substream) {
  648. if (do_lock && s != substream)
  649. spin_lock_nested(&s->self_group.lock,
  650. SINGLE_DEPTH_NESTING);
  651. res = ops->pre_action(s, state);
  652. if (res < 0)
  653. goto _unlock;
  654. }
  655. snd_pcm_group_for_each_entry(s, substream) {
  656. res = ops->do_action(s, state);
  657. if (res < 0) {
  658. if (ops->undo_action) {
  659. snd_pcm_group_for_each_entry(s1, substream) {
  660. if (s1 == s) /* failed stream */
  661. break;
  662. ops->undo_action(s1, state);
  663. }
  664. }
  665. s = NULL; /* unlock all */
  666. goto _unlock;
  667. }
  668. }
  669. snd_pcm_group_for_each_entry(s, substream) {
  670. ops->post_action(s, state);
  671. }
  672. _unlock:
  673. if (do_lock) {
  674. /* unlock streams */
  675. snd_pcm_group_for_each_entry(s1, substream) {
  676. if (s1 != substream)
  677. spin_unlock(&s1->self_group.lock);
  678. if (s1 == s) /* end */
  679. break;
  680. }
  681. }
  682. return res;
  683. }
  684. /*
  685. * Note: call with stream lock
  686. */
  687. static int snd_pcm_action_single(struct action_ops *ops,
  688. struct snd_pcm_substream *substream,
  689. int state)
  690. {
  691. int res;
  692. res = ops->pre_action(substream, state);
  693. if (res < 0)
  694. return res;
  695. res = ops->do_action(substream, state);
  696. if (res == 0)
  697. ops->post_action(substream, state);
  698. else if (ops->undo_action)
  699. ops->undo_action(substream, state);
  700. return res;
  701. }
  702. /*
  703. * Note: call with stream lock
  704. */
  705. static int snd_pcm_action(struct action_ops *ops,
  706. struct snd_pcm_substream *substream,
  707. int state)
  708. {
  709. int res;
  710. if (snd_pcm_stream_linked(substream)) {
  711. if (!spin_trylock(&substream->group->lock)) {
  712. spin_unlock(&substream->self_group.lock);
  713. spin_lock(&substream->group->lock);
  714. spin_lock(&substream->self_group.lock);
  715. }
  716. res = snd_pcm_action_group(ops, substream, state, 1);
  717. spin_unlock(&substream->group->lock);
  718. } else {
  719. res = snd_pcm_action_single(ops, substream, state);
  720. }
  721. return res;
  722. }
  723. /*
  724. * Note: don't use any locks before
  725. */
  726. static int snd_pcm_action_lock_irq(struct action_ops *ops,
  727. struct snd_pcm_substream *substream,
  728. int state)
  729. {
  730. int res;
  731. read_lock_irq(&snd_pcm_link_rwlock);
  732. if (snd_pcm_stream_linked(substream)) {
  733. spin_lock(&substream->group->lock);
  734. spin_lock(&substream->self_group.lock);
  735. res = snd_pcm_action_group(ops, substream, state, 1);
  736. spin_unlock(&substream->self_group.lock);
  737. spin_unlock(&substream->group->lock);
  738. } else {
  739. spin_lock(&substream->self_group.lock);
  740. res = snd_pcm_action_single(ops, substream, state);
  741. spin_unlock(&substream->self_group.lock);
  742. }
  743. read_unlock_irq(&snd_pcm_link_rwlock);
  744. return res;
  745. }
  746. /*
  747. */
  748. static int snd_pcm_action_nonatomic(struct action_ops *ops,
  749. struct snd_pcm_substream *substream,
  750. int state)
  751. {
  752. int res;
  753. down_read(&snd_pcm_link_rwsem);
  754. if (snd_pcm_stream_linked(substream))
  755. res = snd_pcm_action_group(ops, substream, state, 0);
  756. else
  757. res = snd_pcm_action_single(ops, substream, state);
  758. up_read(&snd_pcm_link_rwsem);
  759. return res;
  760. }
  761. /*
  762. * start callbacks
  763. */
  764. static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
  765. {
  766. struct snd_pcm_runtime *runtime = substream->runtime;
  767. if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
  768. return -EBADFD;
  769. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  770. !snd_pcm_playback_data(substream))
  771. return -EPIPE;
  772. runtime->trigger_master = substream;
  773. return 0;
  774. }
  775. static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
  776. {
  777. if (substream->runtime->trigger_master != substream)
  778. return 0;
  779. return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
  780. }
  781. static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
  782. {
  783. if (substream->runtime->trigger_master == substream)
  784. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
  785. }
  786. static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
  787. {
  788. struct snd_pcm_runtime *runtime = substream->runtime;
  789. snd_pcm_trigger_tstamp(substream);
  790. runtime->hw_ptr_jiffies = jiffies;
  791. runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
  792. runtime->rate;
  793. runtime->status->state = state;
  794. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  795. runtime->silence_size > 0)
  796. snd_pcm_playback_silence(substream, ULONG_MAX);
  797. if (substream->timer)
  798. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
  799. &runtime->trigger_tstamp);
  800. }
  801. static struct action_ops snd_pcm_action_start = {
  802. .pre_action = snd_pcm_pre_start,
  803. .do_action = snd_pcm_do_start,
  804. .undo_action = snd_pcm_undo_start,
  805. .post_action = snd_pcm_post_start
  806. };
  807. /**
  808. * snd_pcm_start - start all linked streams
  809. * @substream: the PCM substream instance
  810. */
  811. int snd_pcm_start(struct snd_pcm_substream *substream)
  812. {
  813. return snd_pcm_action(&snd_pcm_action_start, substream,
  814. SNDRV_PCM_STATE_RUNNING);
  815. }
  816. /*
  817. * stop callbacks
  818. */
  819. static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
  820. {
  821. struct snd_pcm_runtime *runtime = substream->runtime;
  822. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  823. return -EBADFD;
  824. runtime->trigger_master = substream;
  825. return 0;
  826. }
  827. static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
  828. {
  829. if (substream->runtime->trigger_master == substream &&
  830. snd_pcm_running(substream))
  831. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
  832. return 0; /* unconditonally stop all substreams */
  833. }
  834. static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
  835. {
  836. struct snd_pcm_runtime *runtime = substream->runtime;
  837. if (runtime->status->state != state) {
  838. snd_pcm_trigger_tstamp(substream);
  839. if (substream->timer)
  840. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
  841. &runtime->trigger_tstamp);
  842. runtime->status->state = state;
  843. }
  844. wake_up(&runtime->sleep);
  845. wake_up(&runtime->tsleep);
  846. }
  847. static struct action_ops snd_pcm_action_stop = {
  848. .pre_action = snd_pcm_pre_stop,
  849. .do_action = snd_pcm_do_stop,
  850. .post_action = snd_pcm_post_stop
  851. };
  852. /**
  853. * snd_pcm_stop - try to stop all running streams in the substream group
  854. * @substream: the PCM substream instance
  855. * @state: PCM state after stopping the stream
  856. *
  857. * The state of each stream is then changed to the given state unconditionally.
  858. */
  859. int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
  860. {
  861. return snd_pcm_action(&snd_pcm_action_stop, substream, state);
  862. }
  863. EXPORT_SYMBOL(snd_pcm_stop);
  864. /**
  865. * snd_pcm_drain_done - stop the DMA only when the given stream is playback
  866. * @substream: the PCM substream
  867. *
  868. * After stopping, the state is changed to SETUP.
  869. * Unlike snd_pcm_stop(), this affects only the given stream.
  870. */
  871. int snd_pcm_drain_done(struct snd_pcm_substream *substream)
  872. {
  873. return snd_pcm_action_single(&snd_pcm_action_stop, substream,
  874. SNDRV_PCM_STATE_SETUP);
  875. }
  876. /*
  877. * pause callbacks
  878. */
  879. static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
  880. {
  881. struct snd_pcm_runtime *runtime = substream->runtime;
  882. if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
  883. return -ENOSYS;
  884. if (push) {
  885. if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
  886. return -EBADFD;
  887. } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
  888. return -EBADFD;
  889. runtime->trigger_master = substream;
  890. return 0;
  891. }
  892. static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
  893. {
  894. if (substream->runtime->trigger_master != substream)
  895. return 0;
  896. /* some drivers might use hw_ptr to recover from the pause -
  897. update the hw_ptr now */
  898. if (push)
  899. snd_pcm_update_hw_ptr(substream);
  900. /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
  901. * a delta between the current jiffies, this gives a large enough
  902. * delta, effectively to skip the check once.
  903. */
  904. substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
  905. return substream->ops->trigger(substream,
  906. push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
  907. SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
  908. }
  909. static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
  910. {
  911. if (substream->runtime->trigger_master == substream)
  912. substream->ops->trigger(substream,
  913. push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
  914. SNDRV_PCM_TRIGGER_PAUSE_PUSH);
  915. }
  916. static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
  917. {
  918. struct snd_pcm_runtime *runtime = substream->runtime;
  919. snd_pcm_trigger_tstamp(substream);
  920. if (push) {
  921. runtime->status->state = SNDRV_PCM_STATE_PAUSED;
  922. if (substream->timer)
  923. snd_timer_notify(substream->timer,
  924. SNDRV_TIMER_EVENT_MPAUSE,
  925. &runtime->trigger_tstamp);
  926. wake_up(&runtime->sleep);
  927. wake_up(&runtime->tsleep);
  928. } else {
  929. runtime->status->state = SNDRV_PCM_STATE_RUNNING;
  930. if (substream->timer)
  931. snd_timer_notify(substream->timer,
  932. SNDRV_TIMER_EVENT_MCONTINUE,
  933. &runtime->trigger_tstamp);
  934. }
  935. }
  936. static struct action_ops snd_pcm_action_pause = {
  937. .pre_action = snd_pcm_pre_pause,
  938. .do_action = snd_pcm_do_pause,
  939. .undo_action = snd_pcm_undo_pause,
  940. .post_action = snd_pcm_post_pause
  941. };
  942. /*
  943. * Push/release the pause for all linked streams.
  944. */
  945. static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
  946. {
  947. return snd_pcm_action(&snd_pcm_action_pause, substream, push);
  948. }
  949. #ifdef CONFIG_PM
  950. /* suspend */
  951. static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
  952. {
  953. struct snd_pcm_runtime *runtime = substream->runtime;
  954. if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
  955. return -EBUSY;
  956. runtime->trigger_master = substream;
  957. return 0;
  958. }
  959. static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
  960. {
  961. struct snd_pcm_runtime *runtime = substream->runtime;
  962. if (runtime->trigger_master != substream)
  963. return 0;
  964. if (! snd_pcm_running(substream))
  965. return 0;
  966. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
  967. return 0; /* suspend unconditionally */
  968. }
  969. static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
  970. {
  971. struct snd_pcm_runtime *runtime = substream->runtime;
  972. snd_pcm_trigger_tstamp(substream);
  973. if (substream->timer)
  974. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
  975. &runtime->trigger_tstamp);
  976. runtime->status->suspended_state = runtime->status->state;
  977. runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
  978. wake_up(&runtime->sleep);
  979. wake_up(&runtime->tsleep);
  980. }
  981. static struct action_ops snd_pcm_action_suspend = {
  982. .pre_action = snd_pcm_pre_suspend,
  983. .do_action = snd_pcm_do_suspend,
  984. .post_action = snd_pcm_post_suspend
  985. };
  986. /**
  987. * snd_pcm_suspend - trigger SUSPEND to all linked streams
  988. * @substream: the PCM substream
  989. *
  990. * After this call, all streams are changed to SUSPENDED state.
  991. */
  992. int snd_pcm_suspend(struct snd_pcm_substream *substream)
  993. {
  994. int err;
  995. unsigned long flags;
  996. if (! substream)
  997. return 0;
  998. snd_pcm_stream_lock_irqsave(substream, flags);
  999. err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
  1000. snd_pcm_stream_unlock_irqrestore(substream, flags);
  1001. return err;
  1002. }
  1003. EXPORT_SYMBOL(snd_pcm_suspend);
  1004. /**
  1005. * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
  1006. * @pcm: the PCM instance
  1007. *
  1008. * After this call, all streams are changed to SUSPENDED state.
  1009. */
  1010. int snd_pcm_suspend_all(struct snd_pcm *pcm)
  1011. {
  1012. struct snd_pcm_substream *substream;
  1013. int stream, err = 0;
  1014. if (! pcm)
  1015. return 0;
  1016. for (stream = 0; stream < 2; stream++) {
  1017. for (substream = pcm->streams[stream].substream;
  1018. substream; substream = substream->next) {
  1019. /* FIXME: the open/close code should lock this as well */
  1020. if (substream->runtime == NULL)
  1021. continue;
  1022. err = snd_pcm_suspend(substream);
  1023. if (err < 0 && err != -EBUSY)
  1024. return err;
  1025. }
  1026. }
  1027. return 0;
  1028. }
  1029. EXPORT_SYMBOL(snd_pcm_suspend_all);
  1030. /* resume */
  1031. static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
  1032. {
  1033. struct snd_pcm_runtime *runtime = substream->runtime;
  1034. if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
  1035. return -ENOSYS;
  1036. runtime->trigger_master = substream;
  1037. return 0;
  1038. }
  1039. static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
  1040. {
  1041. struct snd_pcm_runtime *runtime = substream->runtime;
  1042. if (runtime->trigger_master != substream)
  1043. return 0;
  1044. /* DMA not running previously? */
  1045. if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
  1046. (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
  1047. substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
  1048. return 0;
  1049. return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
  1050. }
  1051. static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
  1052. {
  1053. if (substream->runtime->trigger_master == substream &&
  1054. snd_pcm_running(substream))
  1055. substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
  1056. }
  1057. static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
  1058. {
  1059. struct snd_pcm_runtime *runtime = substream->runtime;
  1060. snd_pcm_trigger_tstamp(substream);
  1061. if (substream->timer)
  1062. snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
  1063. &runtime->trigger_tstamp);
  1064. runtime->status->state = runtime->status->suspended_state;
  1065. }
  1066. static struct action_ops snd_pcm_action_resume = {
  1067. .pre_action = snd_pcm_pre_resume,
  1068. .do_action = snd_pcm_do_resume,
  1069. .undo_action = snd_pcm_undo_resume,
  1070. .post_action = snd_pcm_post_resume
  1071. };
  1072. static int snd_pcm_resume(struct snd_pcm_substream *substream)
  1073. {
  1074. struct snd_card *card = substream->pcm->card;
  1075. int res;
  1076. snd_power_lock(card);
  1077. if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
  1078. res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
  1079. snd_power_unlock(card);
  1080. return res;
  1081. }
  1082. #else
  1083. static int snd_pcm_resume(struct snd_pcm_substream *substream)
  1084. {
  1085. return -ENOSYS;
  1086. }
  1087. #endif /* CONFIG_PM */
  1088. /*
  1089. * xrun ioctl
  1090. *
  1091. * Change the RUNNING stream(s) to XRUN state.
  1092. */
  1093. static int snd_pcm_xrun(struct snd_pcm_substream *substream)
  1094. {
  1095. struct snd_card *card = substream->pcm->card;
  1096. struct snd_pcm_runtime *runtime = substream->runtime;
  1097. int result;
  1098. snd_power_lock(card);
  1099. if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1100. result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  1101. if (result < 0)
  1102. goto _unlock;
  1103. }
  1104. snd_pcm_stream_lock_irq(substream);
  1105. switch (runtime->status->state) {
  1106. case SNDRV_PCM_STATE_XRUN:
  1107. result = 0; /* already there */
  1108. break;
  1109. case SNDRV_PCM_STATE_RUNNING:
  1110. result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  1111. break;
  1112. default:
  1113. result = -EBADFD;
  1114. }
  1115. snd_pcm_stream_unlock_irq(substream);
  1116. _unlock:
  1117. snd_power_unlock(card);
  1118. return result;
  1119. }
  1120. /*
  1121. * reset ioctl
  1122. */
  1123. static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
  1124. {
  1125. struct snd_pcm_runtime *runtime = substream->runtime;
  1126. switch (runtime->status->state) {
  1127. case SNDRV_PCM_STATE_RUNNING:
  1128. case SNDRV_PCM_STATE_PREPARED:
  1129. case SNDRV_PCM_STATE_PAUSED:
  1130. case SNDRV_PCM_STATE_SUSPENDED:
  1131. return 0;
  1132. default:
  1133. return -EBADFD;
  1134. }
  1135. }
  1136. static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
  1137. {
  1138. struct snd_pcm_runtime *runtime = substream->runtime;
  1139. int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
  1140. if (err < 0)
  1141. return err;
  1142. runtime->hw_ptr_base = 0;
  1143. runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
  1144. runtime->status->hw_ptr % runtime->period_size;
  1145. runtime->silence_start = runtime->status->hw_ptr;
  1146. runtime->silence_filled = 0;
  1147. return 0;
  1148. }
  1149. static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
  1150. {
  1151. struct snd_pcm_runtime *runtime = substream->runtime;
  1152. runtime->control->appl_ptr = runtime->status->hw_ptr;
  1153. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  1154. runtime->silence_size > 0)
  1155. snd_pcm_playback_silence(substream, ULONG_MAX);
  1156. }
  1157. static struct action_ops snd_pcm_action_reset = {
  1158. .pre_action = snd_pcm_pre_reset,
  1159. .do_action = snd_pcm_do_reset,
  1160. .post_action = snd_pcm_post_reset
  1161. };
  1162. static int snd_pcm_reset(struct snd_pcm_substream *substream)
  1163. {
  1164. return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
  1165. }
  1166. /*
  1167. * prepare ioctl
  1168. */
  1169. /* we use the second argument for updating f_flags */
  1170. static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
  1171. int f_flags)
  1172. {
  1173. struct snd_pcm_runtime *runtime = substream->runtime;
  1174. if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
  1175. runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
  1176. return -EBADFD;
  1177. if (snd_pcm_running(substream))
  1178. return -EBUSY;
  1179. substream->f_flags = f_flags;
  1180. return 0;
  1181. }
  1182. static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
  1183. {
  1184. int err;
  1185. err = substream->ops->prepare(substream);
  1186. if (err < 0)
  1187. return err;
  1188. return snd_pcm_do_reset(substream, 0);
  1189. }
  1190. static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
  1191. {
  1192. struct snd_pcm_runtime *runtime = substream->runtime;
  1193. runtime->control->appl_ptr = runtime->status->hw_ptr;
  1194. runtime->status->state = SNDRV_PCM_STATE_PREPARED;
  1195. }
  1196. static struct action_ops snd_pcm_action_prepare = {
  1197. .pre_action = snd_pcm_pre_prepare,
  1198. .do_action = snd_pcm_do_prepare,
  1199. .post_action = snd_pcm_post_prepare
  1200. };
  1201. /**
  1202. * snd_pcm_prepare - prepare the PCM substream to be triggerable
  1203. * @substream: the PCM substream instance
  1204. * @file: file to refer f_flags
  1205. */
  1206. static int snd_pcm_prepare(struct snd_pcm_substream *substream,
  1207. struct file *file)
  1208. {
  1209. int res;
  1210. struct snd_card *card = substream->pcm->card;
  1211. int f_flags;
  1212. if (file)
  1213. f_flags = file->f_flags;
  1214. else
  1215. f_flags = substream->f_flags;
  1216. snd_power_lock(card);
  1217. if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
  1218. res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
  1219. substream, f_flags);
  1220. snd_power_unlock(card);
  1221. return res;
  1222. }
  1223. /*
  1224. * drain ioctl
  1225. */
  1226. static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
  1227. {
  1228. substream->runtime->trigger_master = substream;
  1229. return 0;
  1230. }
  1231. static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
  1232. {
  1233. struct snd_pcm_runtime *runtime = substream->runtime;
  1234. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1235. switch (runtime->status->state) {
  1236. case SNDRV_PCM_STATE_PREPARED:
  1237. /* start playback stream if possible */
  1238. if (! snd_pcm_playback_empty(substream)) {
  1239. snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
  1240. snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
  1241. }
  1242. break;
  1243. case SNDRV_PCM_STATE_RUNNING:
  1244. runtime->status->state = SNDRV_PCM_STATE_DRAINING;
  1245. break;
  1246. default:
  1247. break;
  1248. }
  1249. } else {
  1250. /* stop running stream */
  1251. if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
  1252. int new_state = snd_pcm_capture_avail(runtime) > 0 ?
  1253. SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
  1254. snd_pcm_do_stop(substream, new_state);
  1255. snd_pcm_post_stop(substream, new_state);
  1256. }
  1257. }
  1258. return 0;
  1259. }
  1260. static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
  1261. {
  1262. }
  1263. static struct action_ops snd_pcm_action_drain_init = {
  1264. .pre_action = snd_pcm_pre_drain_init,
  1265. .do_action = snd_pcm_do_drain_init,
  1266. .post_action = snd_pcm_post_drain_init
  1267. };
  1268. static int snd_pcm_drop(struct snd_pcm_substream *substream);
  1269. /*
  1270. * Drain the stream(s).
  1271. * When the substream is linked, sync until the draining of all playback streams
  1272. * is finished.
  1273. * After this call, all streams are supposed to be either SETUP or DRAINING
  1274. * (capture only) state.
  1275. */
  1276. static int snd_pcm_drain(struct snd_pcm_substream *substream,
  1277. struct file *file)
  1278. {
  1279. struct snd_card *card;
  1280. struct snd_pcm_runtime *runtime;
  1281. struct snd_pcm_substream *s;
  1282. wait_queue_t wait;
  1283. int result = 0;
  1284. int nonblock = 0;
  1285. card = substream->pcm->card;
  1286. runtime = substream->runtime;
  1287. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  1288. return -EBADFD;
  1289. snd_power_lock(card);
  1290. if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
  1291. result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
  1292. if (result < 0) {
  1293. snd_power_unlock(card);
  1294. return result;
  1295. }
  1296. }
  1297. if (file) {
  1298. if (file->f_flags & O_NONBLOCK)
  1299. nonblock = 1;
  1300. } else if (substream->f_flags & O_NONBLOCK)
  1301. nonblock = 1;
  1302. down_read(&snd_pcm_link_rwsem);
  1303. snd_pcm_stream_lock_irq(substream);
  1304. /* resume pause */
  1305. if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
  1306. snd_pcm_pause(substream, 0);
  1307. /* pre-start/stop - all running streams are changed to DRAINING state */
  1308. result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
  1309. if (result < 0)
  1310. goto unlock;
  1311. /* in non-blocking, we don't wait in ioctl but let caller poll */
  1312. if (nonblock) {
  1313. result = -EAGAIN;
  1314. goto unlock;
  1315. }
  1316. for (;;) {
  1317. long tout;
  1318. struct snd_pcm_runtime *to_check;
  1319. if (signal_pending(current)) {
  1320. result = -ERESTARTSYS;
  1321. break;
  1322. }
  1323. /* find a substream to drain */
  1324. to_check = NULL;
  1325. snd_pcm_group_for_each_entry(s, substream) {
  1326. if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
  1327. continue;
  1328. runtime = s->runtime;
  1329. if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  1330. to_check = runtime;
  1331. break;
  1332. }
  1333. }
  1334. if (!to_check)
  1335. break; /* all drained */
  1336. init_waitqueue_entry(&wait, current);
  1337. add_wait_queue(&to_check->sleep, &wait);
  1338. snd_pcm_stream_unlock_irq(substream);
  1339. up_read(&snd_pcm_link_rwsem);
  1340. snd_power_unlock(card);
  1341. if (runtime->no_period_wakeup)
  1342. tout = MAX_SCHEDULE_TIMEOUT;
  1343. else {
  1344. tout = 10;
  1345. if (runtime->rate) {
  1346. long t = runtime->period_size * 2 / runtime->rate;
  1347. tout = max(t, tout);
  1348. }
  1349. tout = msecs_to_jiffies(tout * 1000);
  1350. }
  1351. tout = schedule_timeout_interruptible(tout);
  1352. snd_power_lock(card);
  1353. down_read(&snd_pcm_link_rwsem);
  1354. snd_pcm_stream_lock_irq(substream);
  1355. remove_wait_queue(&to_check->sleep, &wait);
  1356. if (tout == 0) {
  1357. if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
  1358. result = -ESTRPIPE;
  1359. else {
  1360. snd_printd("playback drain error (DMA or IRQ trouble?)\n");
  1361. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1362. result = -EIO;
  1363. }
  1364. break;
  1365. }
  1366. }
  1367. unlock:
  1368. snd_pcm_stream_unlock_irq(substream);
  1369. up_read(&snd_pcm_link_rwsem);
  1370. snd_power_unlock(card);
  1371. return result;
  1372. }
  1373. /*
  1374. * drop ioctl
  1375. *
  1376. * Immediately put all linked substreams into SETUP state.
  1377. */
  1378. static int snd_pcm_drop(struct snd_pcm_substream *substream)
  1379. {
  1380. struct snd_pcm_runtime *runtime;
  1381. int result = 0;
  1382. if (PCM_RUNTIME_CHECK(substream))
  1383. return -ENXIO;
  1384. runtime = substream->runtime;
  1385. if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
  1386. runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
  1387. runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
  1388. return -EBADFD;
  1389. snd_pcm_stream_lock_irq(substream);
  1390. /* resume pause */
  1391. if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
  1392. snd_pcm_pause(substream, 0);
  1393. snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
  1394. /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
  1395. snd_pcm_stream_unlock_irq(substream);
  1396. return result;
  1397. }
  1398. /* WARNING: Don't forget to fput back the file */
  1399. static struct file *snd_pcm_file_fd(int fd)
  1400. {
  1401. struct file *file;
  1402. struct inode *inode;
  1403. unsigned int minor;
  1404. file = fget(fd);
  1405. if (!file)
  1406. return NULL;
  1407. inode = file->f_path.dentry->d_inode;
  1408. if (!S_ISCHR(inode->i_mode) ||
  1409. imajor(inode) != snd_major) {
  1410. fput(file);
  1411. return NULL;
  1412. }
  1413. minor = iminor(inode);
  1414. if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
  1415. !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
  1416. fput(file);
  1417. return NULL;
  1418. }
  1419. return file;
  1420. }
  1421. /*
  1422. * PCM link handling
  1423. */
  1424. static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
  1425. {
  1426. int res = 0;
  1427. struct file *file;
  1428. struct snd_pcm_file *pcm_file;
  1429. struct snd_pcm_substream *substream1;
  1430. file = snd_pcm_file_fd(fd);
  1431. if (!file)
  1432. return -EBADFD;
  1433. pcm_file = file->private_data;
  1434. substream1 = pcm_file->substream;
  1435. down_write(&snd_pcm_link_rwsem);
  1436. write_lock_irq(&snd_pcm_link_rwlock);
  1437. if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
  1438. substream->runtime->status->state != substream1->runtime->status->state) {
  1439. res = -EBADFD;
  1440. goto _end;
  1441. }
  1442. if (snd_pcm_stream_linked(substream1)) {
  1443. res = -EALREADY;
  1444. goto _end;
  1445. }
  1446. if (!snd_pcm_stream_linked(substream)) {
  1447. substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
  1448. if (substream->group == NULL) {
  1449. res = -ENOMEM;
  1450. goto _end;
  1451. }
  1452. spin_lock_init(&substream->group->lock);
  1453. INIT_LIST_HEAD(&substream->group->substreams);
  1454. list_add_tail(&substream->link_list, &substream->group->substreams);
  1455. substream->group->count = 1;
  1456. }
  1457. list_add_tail(&substream1->link_list, &substream->group->substreams);
  1458. substream->group->count++;
  1459. substream1->group = substream->group;
  1460. _end:
  1461. write_unlock_irq(&snd_pcm_link_rwlock);
  1462. up_write(&snd_pcm_link_rwsem);
  1463. fput(file);
  1464. return res;
  1465. }
  1466. static void relink_to_local(struct snd_pcm_substream *substream)
  1467. {
  1468. substream->group = &substream->self_group;
  1469. INIT_LIST_HEAD(&substream->self_group.substreams);
  1470. list_add_tail(&substream->link_list, &substream->self_group.substreams);
  1471. }
  1472. static int snd_pcm_unlink(struct snd_pcm_substream *substream)
  1473. {
  1474. struct snd_pcm_substream *s;
  1475. int res = 0;
  1476. down_write(&snd_pcm_link_rwsem);
  1477. write_lock_irq(&snd_pcm_link_rwlock);
  1478. if (!snd_pcm_stream_linked(substream)) {
  1479. res = -EALREADY;
  1480. goto _end;
  1481. }
  1482. list_del(&substream->link_list);
  1483. substream->group->count--;
  1484. if (substream->group->count == 1) { /* detach the last stream, too */
  1485. snd_pcm_group_for_each_entry(s, substream) {
  1486. relink_to_local(s);
  1487. break;
  1488. }
  1489. kfree(substream->group);
  1490. }
  1491. relink_to_local(substream);
  1492. _end:
  1493. write_unlock_irq(&snd_pcm_link_rwlock);
  1494. up_write(&snd_pcm_link_rwsem);
  1495. return res;
  1496. }
  1497. /*
  1498. * hw configurator
  1499. */
  1500. static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
  1501. struct snd_pcm_hw_rule *rule)
  1502. {
  1503. struct snd_interval t;
  1504. snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
  1505. hw_param_interval_c(params, rule->deps[1]), &t);
  1506. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1507. }
  1508. static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
  1509. struct snd_pcm_hw_rule *rule)
  1510. {
  1511. struct snd_interval t;
  1512. snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
  1513. hw_param_interval_c(params, rule->deps[1]), &t);
  1514. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1515. }
  1516. static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
  1517. struct snd_pcm_hw_rule *rule)
  1518. {
  1519. struct snd_interval t;
  1520. snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
  1521. hw_param_interval_c(params, rule->deps[1]),
  1522. (unsigned long) rule->private, &t);
  1523. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1524. }
  1525. static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
  1526. struct snd_pcm_hw_rule *rule)
  1527. {
  1528. struct snd_interval t;
  1529. snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
  1530. (unsigned long) rule->private,
  1531. hw_param_interval_c(params, rule->deps[1]), &t);
  1532. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1533. }
  1534. static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
  1535. struct snd_pcm_hw_rule *rule)
  1536. {
  1537. unsigned int k;
  1538. struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
  1539. struct snd_mask m;
  1540. struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  1541. snd_mask_any(&m);
  1542. for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
  1543. int bits;
  1544. if (! snd_mask_test(mask, k))
  1545. continue;
  1546. bits = snd_pcm_format_physical_width(k);
  1547. if (bits <= 0)
  1548. continue; /* ignore invalid formats */
  1549. if ((unsigned)bits < i->min || (unsigned)bits > i->max)
  1550. snd_mask_reset(&m, k);
  1551. }
  1552. return snd_mask_refine(mask, &m);
  1553. }
  1554. static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
  1555. struct snd_pcm_hw_rule *rule)
  1556. {
  1557. struct snd_interval t;
  1558. unsigned int k;
  1559. t.min = UINT_MAX;
  1560. t.max = 0;
  1561. t.openmin = 0;
  1562. t.openmax = 0;
  1563. for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
  1564. int bits;
  1565. if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
  1566. continue;
  1567. bits = snd_pcm_format_physical_width(k);
  1568. if (bits <= 0)
  1569. continue; /* ignore invalid formats */
  1570. if (t.min > (unsigned)bits)
  1571. t.min = bits;
  1572. if (t.max < (unsigned)bits)
  1573. t.max = bits;
  1574. }
  1575. t.integer = 1;
  1576. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1577. }
  1578. #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
  1579. #error "Change this table"
  1580. #endif
  1581. static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
  1582. 48000, 64000, 88200, 96000, 176400, 192000 };
  1583. const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
  1584. .count = ARRAY_SIZE(rates),
  1585. .list = rates,
  1586. };
  1587. static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
  1588. struct snd_pcm_hw_rule *rule)
  1589. {
  1590. struct snd_pcm_hardware *hw = rule->private;
  1591. return snd_interval_list(hw_param_interval(params, rule->var),
  1592. snd_pcm_known_rates.count,
  1593. snd_pcm_known_rates.list, hw->rates);
  1594. }
  1595. static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
  1596. struct snd_pcm_hw_rule *rule)
  1597. {
  1598. struct snd_interval t;
  1599. struct snd_pcm_substream *substream = rule->private;
  1600. t.min = 0;
  1601. t.max = substream->buffer_bytes_max;
  1602. t.openmin = 0;
  1603. t.openmax = 0;
  1604. t.integer = 1;
  1605. return snd_interval_refine(hw_param_interval(params, rule->var), &t);
  1606. }
  1607. int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
  1608. {
  1609. struct snd_pcm_runtime *runtime = substream->runtime;
  1610. struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
  1611. int k, err;
  1612. for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
  1613. snd_mask_any(constrs_mask(constrs, k));
  1614. }
  1615. for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
  1616. snd_interval_any(constrs_interval(constrs, k));
  1617. }
  1618. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
  1619. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
  1620. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
  1621. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
  1622. snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
  1623. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  1624. snd_pcm_hw_rule_format, NULL,
  1625. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1626. if (err < 0)
  1627. return err;
  1628. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
  1629. snd_pcm_hw_rule_sample_bits, NULL,
  1630. SNDRV_PCM_HW_PARAM_FORMAT,
  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_div, NULL,
  1636. SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  1637. if (err < 0)
  1638. return err;
  1639. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1640. snd_pcm_hw_rule_mul, NULL,
  1641. SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  1642. if (err < 0)
  1643. return err;
  1644. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1645. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1646. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
  1647. if (err < 0)
  1648. return err;
  1649. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
  1650. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1651. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
  1652. if (err < 0)
  1653. return err;
  1654. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  1655. snd_pcm_hw_rule_div, NULL,
  1656. SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
  1657. if (err < 0)
  1658. return err;
  1659. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1660. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1661. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
  1662. if (err < 0)
  1663. return err;
  1664. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1665. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1666. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
  1667. if (err < 0)
  1668. return err;
  1669. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
  1670. snd_pcm_hw_rule_div, NULL,
  1671. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
  1672. if (err < 0)
  1673. return err;
  1674. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1675. snd_pcm_hw_rule_div, NULL,
  1676. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
  1677. if (err < 0)
  1678. return err;
  1679. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1680. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1681. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1682. if (err < 0)
  1683. return err;
  1684. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
  1685. snd_pcm_hw_rule_muldivk, (void*) 1000000,
  1686. SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
  1687. if (err < 0)
  1688. return err;
  1689. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1690. snd_pcm_hw_rule_mul, NULL,
  1691. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
  1692. if (err < 0)
  1693. return err;
  1694. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1695. snd_pcm_hw_rule_mulkdiv, (void*) 8,
  1696. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1697. if (err < 0)
  1698. return err;
  1699. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  1700. snd_pcm_hw_rule_muldivk, (void*) 1000000,
  1701. SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
  1702. if (err < 0)
  1703. return err;
  1704. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1705. snd_pcm_hw_rule_muldivk, (void*) 8,
  1706. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1707. if (err < 0)
  1708. return err;
  1709. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1710. snd_pcm_hw_rule_muldivk, (void*) 8,
  1711. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
  1712. if (err < 0)
  1713. return err;
  1714. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
  1715. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1716. SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
  1717. if (err < 0)
  1718. return err;
  1719. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
  1720. snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
  1721. SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
  1722. if (err < 0)
  1723. return err;
  1724. return 0;
  1725. }
  1726. int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
  1727. {
  1728. struct snd_pcm_runtime *runtime = substream->runtime;
  1729. struct snd_pcm_hardware *hw = &runtime->hw;
  1730. int err;
  1731. unsigned int mask = 0;
  1732. if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
  1733. mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
  1734. if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
  1735. mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
  1736. if (hw->info & SNDRV_PCM_INFO_MMAP) {
  1737. if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
  1738. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
  1739. if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
  1740. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
  1741. if (hw->info & SNDRV_PCM_INFO_COMPLEX)
  1742. mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
  1743. }
  1744. err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
  1745. if (err < 0)
  1746. return err;
  1747. err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
  1748. if (err < 0)
  1749. return err;
  1750. err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
  1751. if (err < 0)
  1752. return err;
  1753. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
  1754. hw->channels_min, hw->channels_max);
  1755. if (err < 0)
  1756. return err;
  1757. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
  1758. hw->rate_min, hw->rate_max);
  1759. if (err < 0)
  1760. return err;
  1761. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1762. hw->period_bytes_min, hw->period_bytes_max);
  1763. if (err < 0)
  1764. return err;
  1765. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
  1766. hw->periods_min, hw->periods_max);
  1767. if (err < 0)
  1768. return err;
  1769. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1770. hw->period_bytes_min, hw->buffer_bytes_max);
  1771. if (err < 0)
  1772. return err;
  1773. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  1774. snd_pcm_hw_rule_buffer_bytes_max, substream,
  1775. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
  1776. if (err < 0)
  1777. return err;
  1778. /* FIXME: remove */
  1779. if (runtime->dma_bytes) {
  1780. err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
  1781. if (err < 0)
  1782. return -EINVAL;
  1783. }
  1784. if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
  1785. err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1786. snd_pcm_hw_rule_rate, hw,
  1787. SNDRV_PCM_HW_PARAM_RATE, -1);
  1788. if (err < 0)
  1789. return err;
  1790. }
  1791. /* FIXME: this belong to lowlevel */
  1792. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
  1793. return 0;
  1794. }
  1795. static void pcm_release_private(struct snd_pcm_substream *substream)
  1796. {
  1797. snd_pcm_unlink(substream);
  1798. }
  1799. void snd_pcm_release_substream(struct snd_pcm_substream *substream)
  1800. {
  1801. substream->ref_count--;
  1802. if (substream->ref_count > 0)
  1803. return;
  1804. snd_pcm_drop(substream);
  1805. if (substream->hw_opened) {
  1806. if (substream->ops->hw_free != NULL)
  1807. substream->ops->hw_free(substream);
  1808. substream->ops->close(substream);
  1809. substream->hw_opened = 0;
  1810. }
  1811. if (pm_qos_request_active(&substream->latency_pm_qos_req))
  1812. pm_qos_remove_request(&substream->latency_pm_qos_req);
  1813. if (substream->pcm_release) {
  1814. substream->pcm_release(substream);
  1815. substream->pcm_release = NULL;
  1816. }
  1817. snd_pcm_detach_substream(substream);
  1818. }
  1819. EXPORT_SYMBOL(snd_pcm_release_substream);
  1820. int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
  1821. struct file *file,
  1822. struct snd_pcm_substream **rsubstream)
  1823. {
  1824. struct snd_pcm_substream *substream;
  1825. int err;
  1826. err = snd_pcm_attach_substream(pcm, stream, file, &substream);
  1827. if (err < 0)
  1828. return err;
  1829. if (substream->ref_count > 1) {
  1830. *rsubstream = substream;
  1831. return 0;
  1832. }
  1833. err = snd_pcm_hw_constraints_init(substream);
  1834. if (err < 0) {
  1835. snd_printd("snd_pcm_hw_constraints_init failed\n");
  1836. goto error;
  1837. }
  1838. if ((err = substream->ops->open(substream)) < 0)
  1839. goto error;
  1840. substream->hw_opened = 1;
  1841. err = snd_pcm_hw_constraints_complete(substream);
  1842. if (err < 0) {
  1843. snd_printd("snd_pcm_hw_constraints_complete failed\n");
  1844. goto error;
  1845. }
  1846. *rsubstream = substream;
  1847. return 0;
  1848. error:
  1849. snd_pcm_release_substream(substream);
  1850. return err;
  1851. }
  1852. EXPORT_SYMBOL(snd_pcm_open_substream);
  1853. static int snd_pcm_open_file(struct file *file,
  1854. struct snd_pcm *pcm,
  1855. int stream,
  1856. struct snd_pcm_file **rpcm_file)
  1857. {
  1858. struct snd_pcm_file *pcm_file;
  1859. struct snd_pcm_substream *substream;
  1860. int err;
  1861. if (rpcm_file)
  1862. *rpcm_file = NULL;
  1863. err = snd_pcm_open_substream(pcm, stream, file, &substream);
  1864. if (err < 0)
  1865. return err;
  1866. pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
  1867. if (pcm_file == NULL) {
  1868. snd_pcm_release_substream(substream);
  1869. return -ENOMEM;
  1870. }
  1871. pcm_file->substream = substream;
  1872. if (substream->ref_count == 1) {
  1873. substream->file = pcm_file;
  1874. substream->pcm_release = pcm_release_private;
  1875. }
  1876. file->private_data = pcm_file;
  1877. if (rpcm_file)
  1878. *rpcm_file = pcm_file;
  1879. return 0;
  1880. }
  1881. static int snd_pcm_playback_open(struct inode *inode, struct file *file)
  1882. {
  1883. struct snd_pcm *pcm;
  1884. int err = nonseekable_open(inode, file);
  1885. if (err < 0)
  1886. return err;
  1887. pcm = snd_lookup_minor_data(iminor(inode),
  1888. SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
  1889. return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
  1890. }
  1891. static int snd_pcm_capture_open(struct inode *inode, struct file *file)
  1892. {
  1893. struct snd_pcm *pcm;
  1894. int err = nonseekable_open(inode, file);
  1895. if (err < 0)
  1896. return err;
  1897. pcm = snd_lookup_minor_data(iminor(inode),
  1898. SNDRV_DEVICE_TYPE_PCM_CAPTURE);
  1899. return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
  1900. }
  1901. static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
  1902. {
  1903. int err;
  1904. struct snd_pcm_file *pcm_file;
  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, &pcm_file);
  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. static int snd_pcm_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. /*
  2890. * mmap the DMA buffer on I/O memory area
  2891. */
  2892. #if SNDRV_PCM_INFO_MMAP_IOMEM
  2893. int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
  2894. struct vm_area_struct *area)
  2895. {
  2896. long size;
  2897. unsigned long offset;
  2898. area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
  2899. area->vm_flags |= VM_IO;
  2900. size = area->vm_end - area->vm_start;
  2901. offset = area->vm_pgoff << PAGE_SHIFT;
  2902. if (io_remap_pfn_range(area, area->vm_start,
  2903. (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
  2904. size, area->vm_page_prot))
  2905. return -EAGAIN;
  2906. return 0;
  2907. }
  2908. EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
  2909. #endif /* SNDRV_PCM_INFO_MMAP */
  2910. /*
  2911. * mmap DMA buffer
  2912. */
  2913. int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
  2914. struct vm_area_struct *area)
  2915. {
  2916. struct snd_pcm_runtime *runtime;
  2917. long size;
  2918. unsigned long offset;
  2919. size_t dma_bytes;
  2920. int err;
  2921. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2922. if (!(area->vm_flags & (VM_WRITE|VM_READ)))
  2923. return -EINVAL;
  2924. } else {
  2925. if (!(area->vm_flags & VM_READ))
  2926. return -EINVAL;
  2927. }
  2928. runtime = substream->runtime;
  2929. if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
  2930. return -EBADFD;
  2931. if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
  2932. return -ENXIO;
  2933. if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
  2934. runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  2935. return -EINVAL;
  2936. size = area->vm_end - area->vm_start;
  2937. offset = area->vm_pgoff << PAGE_SHIFT;
  2938. dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
  2939. if ((size_t)size > dma_bytes)
  2940. return -EINVAL;
  2941. if (offset > dma_bytes - size)
  2942. return -EINVAL;
  2943. area->vm_ops = &snd_pcm_vm_ops_data;
  2944. area->vm_private_data = substream;
  2945. if (substream->ops->mmap)
  2946. err = substream->ops->mmap(substream, area);
  2947. else
  2948. err = snd_pcm_default_mmap(substream, area);
  2949. if (!err)
  2950. atomic_inc(&substream->mmap_count);
  2951. return err;
  2952. }
  2953. EXPORT_SYMBOL(snd_pcm_mmap_data);
  2954. static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
  2955. {
  2956. struct snd_pcm_file * pcm_file;
  2957. struct snd_pcm_substream *substream;
  2958. unsigned long offset;
  2959. pcm_file = file->private_data;
  2960. substream = pcm_file->substream;
  2961. if (PCM_RUNTIME_CHECK(substream))
  2962. return -ENXIO;
  2963. offset = area->vm_pgoff << PAGE_SHIFT;
  2964. switch (offset) {
  2965. case SNDRV_PCM_MMAP_OFFSET_STATUS:
  2966. if (pcm_file->no_compat_mmap)
  2967. return -ENXIO;
  2968. return snd_pcm_mmap_status(substream, file, area);
  2969. case SNDRV_PCM_MMAP_OFFSET_CONTROL:
  2970. if (pcm_file->no_compat_mmap)
  2971. return -ENXIO;
  2972. return snd_pcm_mmap_control(substream, file, area);
  2973. default:
  2974. return snd_pcm_mmap_data(substream, file, area);
  2975. }
  2976. return 0;
  2977. }
  2978. static int snd_pcm_fasync(int fd, struct file * file, int on)
  2979. {
  2980. struct snd_pcm_file * pcm_file;
  2981. struct snd_pcm_substream *substream;
  2982. struct snd_pcm_runtime *runtime;
  2983. pcm_file = file->private_data;
  2984. substream = pcm_file->substream;
  2985. if (PCM_RUNTIME_CHECK(substream))
  2986. return -ENXIO;
  2987. runtime = substream->runtime;
  2988. return fasync_helper(fd, file, on, &runtime->fasync);
  2989. }
  2990. /*
  2991. * ioctl32 compat
  2992. */
  2993. #ifdef CONFIG_COMPAT
  2994. #include "pcm_compat.c"
  2995. #else
  2996. #define snd_pcm_ioctl_compat NULL
  2997. #endif
  2998. /*
  2999. * To be removed helpers to keep binary compatibility
  3000. */
  3001. #ifdef CONFIG_SND_SUPPORT_OLD_API
  3002. #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
  3003. #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
  3004. static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
  3005. struct snd_pcm_hw_params_old *oparams)
  3006. {
  3007. unsigned int i;
  3008. memset(params, 0, sizeof(*params));
  3009. params->flags = oparams->flags;
  3010. for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
  3011. params->masks[i].bits[0] = oparams->masks[i];
  3012. memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
  3013. params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
  3014. params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
  3015. params->info = oparams->info;
  3016. params->msbits = oparams->msbits;
  3017. params->rate_num = oparams->rate_num;
  3018. params->rate_den = oparams->rate_den;
  3019. params->fifo_size = oparams->fifo_size;
  3020. }
  3021. static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
  3022. struct snd_pcm_hw_params *params)
  3023. {
  3024. unsigned int i;
  3025. memset(oparams, 0, sizeof(*oparams));
  3026. oparams->flags = params->flags;
  3027. for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
  3028. oparams->masks[i] = params->masks[i].bits[0];
  3029. memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
  3030. oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
  3031. oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
  3032. oparams->info = params->info;
  3033. oparams->msbits = params->msbits;
  3034. oparams->rate_num = params->rate_num;
  3035. oparams->rate_den = params->rate_den;
  3036. oparams->fifo_size = params->fifo_size;
  3037. }
  3038. static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
  3039. struct snd_pcm_hw_params_old __user * _oparams)
  3040. {
  3041. struct snd_pcm_hw_params *params;
  3042. struct snd_pcm_hw_params_old *oparams = NULL;
  3043. int err;
  3044. params = kmalloc(sizeof(*params), GFP_KERNEL);
  3045. if (!params)
  3046. return -ENOMEM;
  3047. oparams = memdup_user(_oparams, sizeof(*oparams));
  3048. if (IS_ERR(oparams)) {
  3049. err = PTR_ERR(oparams);
  3050. goto out;
  3051. }
  3052. snd_pcm_hw_convert_from_old_params(params, oparams);
  3053. err = snd_pcm_hw_refine(substream, params);
  3054. snd_pcm_hw_convert_to_old_params(oparams, params);
  3055. if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
  3056. if (!err)
  3057. err = -EFAULT;
  3058. }
  3059. kfree(oparams);
  3060. out:
  3061. kfree(params);
  3062. return err;
  3063. }
  3064. static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
  3065. struct snd_pcm_hw_params_old __user * _oparams)
  3066. {
  3067. struct snd_pcm_hw_params *params;
  3068. struct snd_pcm_hw_params_old *oparams = NULL;
  3069. int err;
  3070. params = kmalloc(sizeof(*params), GFP_KERNEL);
  3071. if (!params)
  3072. return -ENOMEM;
  3073. oparams = memdup_user(_oparams, sizeof(*oparams));
  3074. if (IS_ERR(oparams)) {
  3075. err = PTR_ERR(oparams);
  3076. goto out;
  3077. }
  3078. snd_pcm_hw_convert_from_old_params(params, oparams);
  3079. err = snd_pcm_hw_params(substream, params);
  3080. snd_pcm_hw_convert_to_old_params(oparams, params);
  3081. if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
  3082. if (!err)
  3083. err = -EFAULT;
  3084. }
  3085. kfree(oparams);
  3086. out:
  3087. kfree(params);
  3088. return err;
  3089. }
  3090. #endif /* CONFIG_SND_SUPPORT_OLD_API */
  3091. #ifndef CONFIG_MMU
  3092. static unsigned long snd_pcm_get_unmapped_area(struct file *file,
  3093. unsigned long addr,
  3094. unsigned long len,
  3095. unsigned long pgoff,
  3096. unsigned long flags)
  3097. {
  3098. struct snd_pcm_file *pcm_file = file->private_data;
  3099. struct snd_pcm_substream *substream = pcm_file->substream;
  3100. struct snd_pcm_runtime *runtime = substream->runtime;
  3101. unsigned long offset = pgoff << PAGE_SHIFT;
  3102. switch (offset) {
  3103. case SNDRV_PCM_MMAP_OFFSET_STATUS:
  3104. return (unsigned long)runtime->status;
  3105. case SNDRV_PCM_MMAP_OFFSET_CONTROL:
  3106. return (unsigned long)runtime->control;
  3107. default:
  3108. return (unsigned long)runtime->dma_area + offset;
  3109. }
  3110. }
  3111. #else
  3112. # define snd_pcm_get_unmapped_area NULL
  3113. #endif
  3114. /*
  3115. * Register section
  3116. */
  3117. const struct file_operations snd_pcm_f_ops[2] = {
  3118. {
  3119. .owner = THIS_MODULE,
  3120. .write = snd_pcm_write,
  3121. .aio_write = snd_pcm_aio_write,
  3122. .open = snd_pcm_playback_open,
  3123. .release = snd_pcm_release,
  3124. .llseek = no_llseek,
  3125. .poll = snd_pcm_playback_poll,
  3126. .unlocked_ioctl = snd_pcm_playback_ioctl,
  3127. .compat_ioctl = snd_pcm_ioctl_compat,
  3128. .mmap = snd_pcm_mmap,
  3129. .fasync = snd_pcm_fasync,
  3130. .get_unmapped_area = snd_pcm_get_unmapped_area,
  3131. },
  3132. {
  3133. .owner = THIS_MODULE,
  3134. .read = snd_pcm_read,
  3135. .aio_read = snd_pcm_aio_read,
  3136. .open = snd_pcm_capture_open,
  3137. .release = snd_pcm_release,
  3138. .llseek = no_llseek,
  3139. .poll = snd_pcm_capture_poll,
  3140. .unlocked_ioctl = snd_pcm_capture_ioctl,
  3141. .compat_ioctl = snd_pcm_ioctl_compat,
  3142. .mmap = snd_pcm_mmap,
  3143. .fasync = snd_pcm_fasync,
  3144. .get_unmapped_area = snd_pcm_get_unmapped_area,
  3145. }
  3146. };