pcm_native.c 93 KB

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