pcm_native.c 93 KB

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