pcm_native.c 92 KB

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