pcm_native.c 93 KB

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