pcm_native.c 94 KB

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