pcm_native.c 93 KB

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