pcm_native.c 93 KB

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