pcm_native.c 92 KB

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