pcm_native.c 92 KB

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