pcm_native.c 94 KB

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