pcm_oss.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Digital Audio (PCM) abstract layer / OSS compatible
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. */
  6. #if 0
  7. #define PLUGIN_DEBUG
  8. #endif
  9. #if 0
  10. #define OSS_DEBUG
  11. #endif
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/sched/signal.h>
  15. #include <linux/time.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/module.h>
  18. #include <linux/math64.h>
  19. #include <linux/string.h>
  20. #include <linux/compat.h>
  21. #include <sound/core.h>
  22. #include <sound/minors.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include "pcm_plugin.h"
  26. #include <sound/info.h>
  27. #include <linux/soundcard.h>
  28. #include <sound/initval.h>
  29. #include <sound/mixer_oss.h>
  30. #define OSS_ALSAEMULVER _SIOR ('M', 249, int)
  31. static int dsp_map[SNDRV_CARDS];
  32. static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
  33. static bool nonblock_open = 1;
  34. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Abramo Bagnara <abramo@alsa-project.org>");
  35. MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");
  36. MODULE_LICENSE("GPL");
  37. module_param_array(dsp_map, int, NULL, 0444);
  38. MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");
  39. module_param_array(adsp_map, int, NULL, 0444);
  40. MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");
  41. module_param(nonblock_open, bool, 0644);
  42. MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");
  43. MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);
  44. MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);
  45. static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file);
  46. static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file);
  47. static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file);
  48. /*
  49. * helper functions to process hw_params
  50. */
  51. static int snd_interval_refine_min(struct snd_interval *i, unsigned int min, int openmin)
  52. {
  53. int changed = 0;
  54. if (i->min < min) {
  55. i->min = min;
  56. i->openmin = openmin;
  57. changed = 1;
  58. } else if (i->min == min && !i->openmin && openmin) {
  59. i->openmin = 1;
  60. changed = 1;
  61. }
  62. if (i->integer) {
  63. if (i->openmin) {
  64. i->min++;
  65. i->openmin = 0;
  66. }
  67. }
  68. if (snd_interval_checkempty(i)) {
  69. snd_interval_none(i);
  70. return -EINVAL;
  71. }
  72. return changed;
  73. }
  74. static int snd_interval_refine_max(struct snd_interval *i, unsigned int max, int openmax)
  75. {
  76. int changed = 0;
  77. if (i->max > max) {
  78. i->max = max;
  79. i->openmax = openmax;
  80. changed = 1;
  81. } else if (i->max == max && !i->openmax && openmax) {
  82. i->openmax = 1;
  83. changed = 1;
  84. }
  85. if (i->integer) {
  86. if (i->openmax) {
  87. i->max--;
  88. i->openmax = 0;
  89. }
  90. }
  91. if (snd_interval_checkempty(i)) {
  92. snd_interval_none(i);
  93. return -EINVAL;
  94. }
  95. return changed;
  96. }
  97. static int snd_interval_refine_set(struct snd_interval *i, unsigned int val)
  98. {
  99. struct snd_interval t;
  100. t.empty = 0;
  101. t.min = t.max = val;
  102. t.openmin = t.openmax = 0;
  103. t.integer = 1;
  104. return snd_interval_refine(i, &t);
  105. }
  106. /**
  107. * snd_pcm_hw_param_value_min
  108. * @params: the hw_params instance
  109. * @var: parameter to retrieve
  110. * @dir: pointer to the direction (-1,0,1) or NULL
  111. *
  112. * Return the minimum value for field PAR.
  113. */
  114. static unsigned int
  115. snd_pcm_hw_param_value_min(const struct snd_pcm_hw_params *params,
  116. snd_pcm_hw_param_t var, int *dir)
  117. {
  118. if (hw_is_mask(var)) {
  119. if (dir)
  120. *dir = 0;
  121. return snd_mask_min(hw_param_mask_c(params, var));
  122. }
  123. if (hw_is_interval(var)) {
  124. const struct snd_interval *i = hw_param_interval_c(params, var);
  125. if (dir)
  126. *dir = i->openmin;
  127. return snd_interval_min(i);
  128. }
  129. return -EINVAL;
  130. }
  131. /**
  132. * snd_pcm_hw_param_value_max
  133. * @params: the hw_params instance
  134. * @var: parameter to retrieve
  135. * @dir: pointer to the direction (-1,0,1) or NULL
  136. *
  137. * Return the maximum value for field PAR.
  138. */
  139. static int
  140. snd_pcm_hw_param_value_max(const struct snd_pcm_hw_params *params,
  141. snd_pcm_hw_param_t var, int *dir)
  142. {
  143. if (hw_is_mask(var)) {
  144. if (dir)
  145. *dir = 0;
  146. return snd_mask_max(hw_param_mask_c(params, var));
  147. }
  148. if (hw_is_interval(var)) {
  149. const struct snd_interval *i = hw_param_interval_c(params, var);
  150. if (dir)
  151. *dir = - (int) i->openmax;
  152. return snd_interval_max(i);
  153. }
  154. return -EINVAL;
  155. }
  156. static int _snd_pcm_hw_param_mask(struct snd_pcm_hw_params *params,
  157. snd_pcm_hw_param_t var,
  158. const struct snd_mask *val)
  159. {
  160. int changed;
  161. changed = snd_mask_refine(hw_param_mask(params, var), val);
  162. if (changed > 0) {
  163. params->cmask |= 1 << var;
  164. params->rmask |= 1 << var;
  165. }
  166. return changed;
  167. }
  168. static int snd_pcm_hw_param_mask(struct snd_pcm_substream *pcm,
  169. struct snd_pcm_hw_params *params,
  170. snd_pcm_hw_param_t var,
  171. const struct snd_mask *val)
  172. {
  173. int changed = _snd_pcm_hw_param_mask(params, var, val);
  174. if (changed < 0)
  175. return changed;
  176. if (params->rmask) {
  177. int err = snd_pcm_hw_refine(pcm, params);
  178. if (err < 0)
  179. return err;
  180. }
  181. return 0;
  182. }
  183. static int _snd_pcm_hw_param_min(struct snd_pcm_hw_params *params,
  184. snd_pcm_hw_param_t var, unsigned int val,
  185. int dir)
  186. {
  187. int changed;
  188. int open = 0;
  189. if (dir) {
  190. if (dir > 0) {
  191. open = 1;
  192. } else if (dir < 0) {
  193. if (val > 0) {
  194. open = 1;
  195. val--;
  196. }
  197. }
  198. }
  199. if (hw_is_mask(var))
  200. changed = snd_mask_refine_min(hw_param_mask(params, var),
  201. val + !!open);
  202. else if (hw_is_interval(var))
  203. changed = snd_interval_refine_min(hw_param_interval(params, var),
  204. val, open);
  205. else
  206. return -EINVAL;
  207. if (changed > 0) {
  208. params->cmask |= 1 << var;
  209. params->rmask |= 1 << var;
  210. }
  211. return changed;
  212. }
  213. /**
  214. * snd_pcm_hw_param_min
  215. * @pcm: PCM instance
  216. * @params: the hw_params instance
  217. * @var: parameter to retrieve
  218. * @val: minimal value
  219. * @dir: pointer to the direction (-1,0,1) or NULL
  220. *
  221. * Inside configuration space defined by PARAMS remove from PAR all
  222. * values < VAL. Reduce configuration space accordingly.
  223. * Return new minimum or -EINVAL if the configuration space is empty
  224. */
  225. static int snd_pcm_hw_param_min(struct snd_pcm_substream *pcm,
  226. struct snd_pcm_hw_params *params,
  227. snd_pcm_hw_param_t var, unsigned int val,
  228. int *dir)
  229. {
  230. int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
  231. if (changed < 0)
  232. return changed;
  233. if (params->rmask) {
  234. int err = snd_pcm_hw_refine(pcm, params);
  235. if (err < 0)
  236. return err;
  237. }
  238. return snd_pcm_hw_param_value_min(params, var, dir);
  239. }
  240. static int _snd_pcm_hw_param_max(struct snd_pcm_hw_params *params,
  241. snd_pcm_hw_param_t var, unsigned int val,
  242. int dir)
  243. {
  244. int changed;
  245. int open = 0;
  246. if (dir) {
  247. if (dir < 0) {
  248. open = 1;
  249. } else if (dir > 0) {
  250. open = 1;
  251. val++;
  252. }
  253. }
  254. if (hw_is_mask(var)) {
  255. if (val == 0 && open) {
  256. snd_mask_none(hw_param_mask(params, var));
  257. changed = -EINVAL;
  258. } else
  259. changed = snd_mask_refine_max(hw_param_mask(params, var),
  260. val - !!open);
  261. } else if (hw_is_interval(var))
  262. changed = snd_interval_refine_max(hw_param_interval(params, var),
  263. val, open);
  264. else
  265. return -EINVAL;
  266. if (changed > 0) {
  267. params->cmask |= 1 << var;
  268. params->rmask |= 1 << var;
  269. }
  270. return changed;
  271. }
  272. /**
  273. * snd_pcm_hw_param_max
  274. * @pcm: PCM instance
  275. * @params: the hw_params instance
  276. * @var: parameter to retrieve
  277. * @val: maximal value
  278. * @dir: pointer to the direction (-1,0,1) or NULL
  279. *
  280. * Inside configuration space defined by PARAMS remove from PAR all
  281. * values >= VAL + 1. Reduce configuration space accordingly.
  282. * Return new maximum or -EINVAL if the configuration space is empty
  283. */
  284. static int snd_pcm_hw_param_max(struct snd_pcm_substream *pcm,
  285. struct snd_pcm_hw_params *params,
  286. snd_pcm_hw_param_t var, unsigned int val,
  287. int *dir)
  288. {
  289. int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
  290. if (changed < 0)
  291. return changed;
  292. if (params->rmask) {
  293. int err = snd_pcm_hw_refine(pcm, params);
  294. if (err < 0)
  295. return err;
  296. }
  297. return snd_pcm_hw_param_value_max(params, var, dir);
  298. }
  299. static int boundary_sub(int a, int adir,
  300. int b, int bdir,
  301. int *c, int *cdir)
  302. {
  303. adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
  304. bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
  305. *c = a - b;
  306. *cdir = adir - bdir;
  307. if (*cdir == -2) {
  308. (*c)--;
  309. } else if (*cdir == 2) {
  310. (*c)++;
  311. }
  312. return 0;
  313. }
  314. static int boundary_lt(unsigned int a, int adir,
  315. unsigned int b, int bdir)
  316. {
  317. if (adir < 0) {
  318. a--;
  319. adir = 1;
  320. } else if (adir > 0)
  321. adir = 1;
  322. if (bdir < 0) {
  323. b--;
  324. bdir = 1;
  325. } else if (bdir > 0)
  326. bdir = 1;
  327. return a < b || (a == b && adir < bdir);
  328. }
  329. /* Return 1 if min is nearer to best than max */
  330. static int boundary_nearer(int min, int mindir,
  331. int best, int bestdir,
  332. int max, int maxdir)
  333. {
  334. int dmin, dmindir;
  335. int dmax, dmaxdir;
  336. boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
  337. boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
  338. return boundary_lt(dmin, dmindir, dmax, dmaxdir);
  339. }
  340. /**
  341. * snd_pcm_hw_param_near
  342. * @pcm: PCM instance
  343. * @params: the hw_params instance
  344. * @var: parameter to retrieve
  345. * @best: value to set
  346. * @dir: pointer to the direction (-1,0,1) or NULL
  347. *
  348. * Inside configuration space defined by PARAMS set PAR to the available value
  349. * nearest to VAL. Reduce configuration space accordingly.
  350. * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
  351. * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
  352. * Return the value found.
  353. */
  354. static int snd_pcm_hw_param_near(struct snd_pcm_substream *pcm,
  355. struct snd_pcm_hw_params *params,
  356. snd_pcm_hw_param_t var, unsigned int best,
  357. int *dir)
  358. {
  359. struct snd_pcm_hw_params *save __free(kfree) = NULL;
  360. int v;
  361. unsigned int saved_min;
  362. int last = 0;
  363. int min, max;
  364. int mindir, maxdir;
  365. int valdir = dir ? *dir : 0;
  366. /* FIXME */
  367. if (best > INT_MAX)
  368. best = INT_MAX;
  369. min = max = best;
  370. mindir = maxdir = valdir;
  371. if (maxdir > 0)
  372. maxdir = 0;
  373. else if (maxdir == 0)
  374. maxdir = -1;
  375. else {
  376. maxdir = 1;
  377. max--;
  378. }
  379. save = kmalloc(sizeof(*save), GFP_KERNEL);
  380. if (save == NULL)
  381. return -ENOMEM;
  382. *save = *params;
  383. saved_min = min;
  384. min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
  385. if (min >= 0) {
  386. struct snd_pcm_hw_params *params1 __free(kfree) = NULL;
  387. if (max < 0)
  388. goto _end;
  389. if ((unsigned int)min == saved_min && mindir == valdir)
  390. goto _end;
  391. params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
  392. if (params1 == NULL)
  393. return -ENOMEM;
  394. *params1 = *save;
  395. max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
  396. if (max < 0)
  397. goto _end;
  398. if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
  399. *params = *params1;
  400. last = 1;
  401. }
  402. } else {
  403. *params = *save;
  404. max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
  405. if (max < 0)
  406. return max;
  407. last = 1;
  408. }
  409. _end:
  410. if (last)
  411. v = snd_pcm_hw_param_last(pcm, params, var, dir);
  412. else
  413. v = snd_pcm_hw_param_first(pcm, params, var, dir);
  414. return v;
  415. }
  416. static int _snd_pcm_hw_param_set(struct snd_pcm_hw_params *params,
  417. snd_pcm_hw_param_t var, unsigned int val,
  418. int dir)
  419. {
  420. int changed;
  421. if (hw_is_mask(var)) {
  422. struct snd_mask *m = hw_param_mask(params, var);
  423. if (val == 0 && dir < 0) {
  424. changed = -EINVAL;
  425. snd_mask_none(m);
  426. } else {
  427. if (dir > 0)
  428. val++;
  429. else if (dir < 0)
  430. val--;
  431. changed = snd_mask_refine_set(hw_param_mask(params, var), val);
  432. }
  433. } else if (hw_is_interval(var)) {
  434. struct snd_interval *i = hw_param_interval(params, var);
  435. if (val == 0 && dir < 0) {
  436. changed = -EINVAL;
  437. snd_interval_none(i);
  438. } else if (dir == 0)
  439. changed = snd_interval_refine_set(i, val);
  440. else {
  441. struct snd_interval t;
  442. t.openmin = 1;
  443. t.openmax = 1;
  444. t.empty = 0;
  445. t.integer = 0;
  446. if (dir < 0) {
  447. t.min = val - 1;
  448. t.max = val;
  449. } else {
  450. t.min = val;
  451. t.max = val+1;
  452. }
  453. changed = snd_interval_refine(i, &t);
  454. }
  455. } else
  456. return -EINVAL;
  457. if (changed > 0) {
  458. params->cmask |= 1 << var;
  459. params->rmask |= 1 << var;
  460. }
  461. return changed;
  462. }
  463. /**
  464. * snd_pcm_hw_param_set
  465. * @pcm: PCM instance
  466. * @params: the hw_params instance
  467. * @var: parameter to retrieve
  468. * @val: value to set
  469. * @dir: pointer to the direction (-1,0,1) or NULL
  470. *
  471. * Inside configuration space defined by PARAMS remove from PAR all
  472. * values != VAL. Reduce configuration space accordingly.
  473. * Return VAL or -EINVAL if the configuration space is empty
  474. */
  475. static int snd_pcm_hw_param_set(struct snd_pcm_substream *pcm,
  476. struct snd_pcm_hw_params *params,
  477. snd_pcm_hw_param_t var, unsigned int val,
  478. int dir)
  479. {
  480. int changed = _snd_pcm_hw_param_set(params, var, val, dir);
  481. if (changed < 0)
  482. return changed;
  483. if (params->rmask) {
  484. int err = snd_pcm_hw_refine(pcm, params);
  485. if (err < 0)
  486. return err;
  487. }
  488. return snd_pcm_hw_param_value(params, var, NULL);
  489. }
  490. static int _snd_pcm_hw_param_setinteger(struct snd_pcm_hw_params *params,
  491. snd_pcm_hw_param_t var)
  492. {
  493. int changed;
  494. changed = snd_interval_setinteger(hw_param_interval(params, var));
  495. if (changed > 0) {
  496. params->cmask |= 1 << var;
  497. params->rmask |= 1 << var;
  498. }
  499. return changed;
  500. }
  501. /*
  502. * plugin
  503. */
  504. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  505. static int snd_pcm_oss_plugin_clear(struct snd_pcm_substream *substream)
  506. {
  507. struct snd_pcm_runtime *runtime = substream->runtime;
  508. struct snd_pcm_plugin *plugin, *next;
  509. plugin = runtime->oss.plugin_first;
  510. while (plugin) {
  511. next = plugin->next;
  512. snd_pcm_plugin_free(plugin);
  513. plugin = next;
  514. }
  515. runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;
  516. return 0;
  517. }
  518. static int snd_pcm_plugin_insert(struct snd_pcm_plugin *plugin)
  519. {
  520. struct snd_pcm_runtime *runtime = plugin->plug->runtime;
  521. plugin->next = runtime->oss.plugin_first;
  522. plugin->prev = NULL;
  523. if (runtime->oss.plugin_first) {
  524. runtime->oss.plugin_first->prev = plugin;
  525. runtime->oss.plugin_first = plugin;
  526. } else {
  527. runtime->oss.plugin_last =
  528. runtime->oss.plugin_first = plugin;
  529. }
  530. return 0;
  531. }
  532. int snd_pcm_plugin_append(struct snd_pcm_plugin *plugin)
  533. {
  534. struct snd_pcm_runtime *runtime = plugin->plug->runtime;
  535. plugin->next = NULL;
  536. plugin->prev = runtime->oss.plugin_last;
  537. if (runtime->oss.plugin_last) {
  538. runtime->oss.plugin_last->next = plugin;
  539. runtime->oss.plugin_last = plugin;
  540. } else {
  541. runtime->oss.plugin_last =
  542. runtime->oss.plugin_first = plugin;
  543. }
  544. return 0;
  545. }
  546. #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
  547. static long snd_pcm_oss_bytes(struct snd_pcm_substream *substream, long frames)
  548. {
  549. struct snd_pcm_runtime *runtime = substream->runtime;
  550. long buffer_size = snd_pcm_lib_buffer_bytes(substream);
  551. long bytes = frames_to_bytes(runtime, frames);
  552. if (buffer_size == runtime->oss.buffer_bytes)
  553. return bytes;
  554. #if BITS_PER_LONG >= 64
  555. return runtime->oss.buffer_bytes * bytes / buffer_size;
  556. #else
  557. {
  558. u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;
  559. return div_u64(bsize, buffer_size);
  560. }
  561. #endif
  562. }
  563. static long snd_pcm_alsa_frames(struct snd_pcm_substream *substream, long bytes)
  564. {
  565. struct snd_pcm_runtime *runtime = substream->runtime;
  566. long buffer_size = snd_pcm_lib_buffer_bytes(substream);
  567. if (buffer_size == runtime->oss.buffer_bytes)
  568. return bytes_to_frames(runtime, bytes);
  569. return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);
  570. }
  571. static inline
  572. snd_pcm_uframes_t get_hw_ptr_period(struct snd_pcm_runtime *runtime)
  573. {
  574. return runtime->hw_ptr_interrupt;
  575. }
  576. /* define extended formats in the recent OSS versions (if any) */
  577. /* linear formats */
  578. #define AFMT_S32_LE 0x00001000
  579. #define AFMT_S32_BE 0x00002000
  580. #define AFMT_S24_LE 0x00008000
  581. #define AFMT_S24_BE 0x00010000
  582. #define AFMT_S24_PACKED 0x00040000
  583. /* other supported formats */
  584. #define AFMT_FLOAT 0x00004000
  585. #define AFMT_SPDIF_RAW 0x00020000
  586. /* unsupported formats */
  587. #define AFMT_AC3 0x00000400
  588. #define AFMT_VORBIS 0x00000800
  589. static snd_pcm_format_t snd_pcm_oss_format_from(int format)
  590. {
  591. switch (format) {
  592. case AFMT_MU_LAW: return SNDRV_PCM_FORMAT_MU_LAW;
  593. case AFMT_A_LAW: return SNDRV_PCM_FORMAT_A_LAW;
  594. case AFMT_IMA_ADPCM: return SNDRV_PCM_FORMAT_IMA_ADPCM;
  595. case AFMT_U8: return SNDRV_PCM_FORMAT_U8;
  596. case AFMT_S16_LE: return SNDRV_PCM_FORMAT_S16_LE;
  597. case AFMT_S16_BE: return SNDRV_PCM_FORMAT_S16_BE;
  598. case AFMT_S8: return SNDRV_PCM_FORMAT_S8;
  599. case AFMT_U16_LE: return SNDRV_PCM_FORMAT_U16_LE;
  600. case AFMT_U16_BE: return SNDRV_PCM_FORMAT_U16_BE;
  601. case AFMT_MPEG: return SNDRV_PCM_FORMAT_MPEG;
  602. case AFMT_S32_LE: return SNDRV_PCM_FORMAT_S32_LE;
  603. case AFMT_S32_BE: return SNDRV_PCM_FORMAT_S32_BE;
  604. case AFMT_S24_LE: return SNDRV_PCM_FORMAT_S24_LE;
  605. case AFMT_S24_BE: return SNDRV_PCM_FORMAT_S24_BE;
  606. case AFMT_S24_PACKED: return SNDRV_PCM_FORMAT_S24_3LE;
  607. case AFMT_FLOAT: return SNDRV_PCM_FORMAT_FLOAT;
  608. case AFMT_SPDIF_RAW: return SNDRV_PCM_FORMAT_IEC958_SUBFRAME;
  609. default: return SNDRV_PCM_FORMAT_U8;
  610. }
  611. }
  612. static int snd_pcm_oss_format_to(snd_pcm_format_t format)
  613. {
  614. switch (format) {
  615. case SNDRV_PCM_FORMAT_MU_LAW: return AFMT_MU_LAW;
  616. case SNDRV_PCM_FORMAT_A_LAW: return AFMT_A_LAW;
  617. case SNDRV_PCM_FORMAT_IMA_ADPCM: return AFMT_IMA_ADPCM;
  618. case SNDRV_PCM_FORMAT_U8: return AFMT_U8;
  619. case SNDRV_PCM_FORMAT_S16_LE: return AFMT_S16_LE;
  620. case SNDRV_PCM_FORMAT_S16_BE: return AFMT_S16_BE;
  621. case SNDRV_PCM_FORMAT_S8: return AFMT_S8;
  622. case SNDRV_PCM_FORMAT_U16_LE: return AFMT_U16_LE;
  623. case SNDRV_PCM_FORMAT_U16_BE: return AFMT_U16_BE;
  624. case SNDRV_PCM_FORMAT_MPEG: return AFMT_MPEG;
  625. case SNDRV_PCM_FORMAT_S32_LE: return AFMT_S32_LE;
  626. case SNDRV_PCM_FORMAT_S32_BE: return AFMT_S32_BE;
  627. case SNDRV_PCM_FORMAT_S24_LE: return AFMT_S24_LE;
  628. case SNDRV_PCM_FORMAT_S24_BE: return AFMT_S24_BE;
  629. case SNDRV_PCM_FORMAT_S24_3LE: return AFMT_S24_PACKED;
  630. case SNDRV_PCM_FORMAT_FLOAT: return AFMT_FLOAT;
  631. case SNDRV_PCM_FORMAT_IEC958_SUBFRAME: return AFMT_SPDIF_RAW;
  632. default: return -EINVAL;
  633. }
  634. }
  635. static int snd_pcm_oss_period_size(struct snd_pcm_substream *substream,
  636. struct snd_pcm_hw_params *oss_params,
  637. struct snd_pcm_hw_params *slave_params)
  638. {
  639. ssize_t s;
  640. ssize_t oss_buffer_size;
  641. ssize_t oss_period_size, oss_periods;
  642. ssize_t min_period_size, max_period_size;
  643. struct snd_pcm_runtime *runtime = substream->runtime;
  644. size_t oss_frame_size;
  645. oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *
  646. params_channels(oss_params) / 8;
  647. oss_buffer_size = snd_pcm_hw_param_value_max(slave_params,
  648. SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
  649. NULL);
  650. if (oss_buffer_size <= 0)
  651. return -EINVAL;
  652. oss_buffer_size = snd_pcm_plug_client_size(substream,
  653. oss_buffer_size * oss_frame_size);
  654. if (oss_buffer_size <= 0)
  655. return -EINVAL;
  656. oss_buffer_size = rounddown_pow_of_two(oss_buffer_size);
  657. if (atomic_read(&substream->mmap_count)) {
  658. if (oss_buffer_size > runtime->oss.mmap_bytes)
  659. oss_buffer_size = runtime->oss.mmap_bytes;
  660. }
  661. if (substream->oss.setup.period_size > 16)
  662. oss_period_size = substream->oss.setup.period_size;
  663. else if (runtime->oss.fragshift) {
  664. oss_period_size = 1 << runtime->oss.fragshift;
  665. if (oss_period_size > oss_buffer_size / 2)
  666. oss_period_size = oss_buffer_size / 2;
  667. } else {
  668. int sd;
  669. size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;
  670. oss_period_size = oss_buffer_size;
  671. do {
  672. oss_period_size /= 2;
  673. } while (oss_period_size > bytes_per_sec);
  674. if (runtime->oss.subdivision == 0) {
  675. sd = 4;
  676. if (oss_period_size / sd > 4096)
  677. sd *= 2;
  678. if (oss_period_size / sd < 4096)
  679. sd = 1;
  680. } else
  681. sd = runtime->oss.subdivision;
  682. oss_period_size /= sd;
  683. if (oss_period_size < 16)
  684. oss_period_size = 16;
  685. }
  686. min_period_size = snd_pcm_plug_client_size(substream,
  687. snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
  688. if (min_period_size > 0) {
  689. min_period_size *= oss_frame_size;
  690. min_period_size = roundup_pow_of_two(min_period_size);
  691. if (oss_period_size < min_period_size)
  692. oss_period_size = min_period_size;
  693. }
  694. max_period_size = snd_pcm_plug_client_size(substream,
  695. snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));
  696. if (max_period_size > 0) {
  697. max_period_size *= oss_frame_size;
  698. max_period_size = rounddown_pow_of_two(max_period_size);
  699. if (oss_period_size > max_period_size)
  700. oss_period_size = max_period_size;
  701. }
  702. oss_periods = oss_buffer_size / oss_period_size;
  703. if (substream->oss.setup.periods > 1)
  704. oss_periods = substream->oss.setup.periods;
  705. s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
  706. if (s > 0 && runtime->oss.maxfrags && s > runtime->oss.maxfrags)
  707. s = runtime->oss.maxfrags;
  708. if (oss_periods > s)
  709. oss_periods = s;
  710. s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);
  711. if (s < 2)
  712. s = 2;
  713. if (oss_periods < s)
  714. oss_periods = s;
  715. while (oss_period_size * oss_periods > oss_buffer_size)
  716. oss_period_size /= 2;
  717. if (oss_period_size < 16)
  718. return -EINVAL;
  719. /* don't allocate too large period; 1MB period must be enough */
  720. if (oss_period_size > 1024 * 1024)
  721. return -ENOMEM;
  722. runtime->oss.period_bytes = oss_period_size;
  723. runtime->oss.period_frames = 1;
  724. runtime->oss.periods = oss_periods;
  725. return 0;
  726. }
  727. static int choose_rate(struct snd_pcm_substream *substream,
  728. struct snd_pcm_hw_params *params, unsigned int best_rate)
  729. {
  730. const struct snd_interval *it;
  731. struct snd_pcm_hw_params *save __free(kfree) = NULL;
  732. unsigned int rate, prev;
  733. save = kmalloc(sizeof(*save), GFP_KERNEL);
  734. if (save == NULL)
  735. return -ENOMEM;
  736. *save = *params;
  737. it = hw_param_interval_c(save, SNDRV_PCM_HW_PARAM_RATE);
  738. /* try multiples of the best rate */
  739. rate = best_rate;
  740. for (;;) {
  741. if (it->max < rate || (it->max == rate && it->openmax))
  742. break;
  743. if (it->min < rate || (it->min == rate && !it->openmin)) {
  744. int ret;
  745. ret = snd_pcm_hw_param_set(substream, params,
  746. SNDRV_PCM_HW_PARAM_RATE,
  747. rate, 0);
  748. if (ret == (int)rate)
  749. return rate;
  750. *params = *save;
  751. }
  752. prev = rate;
  753. rate += best_rate;
  754. if (rate <= prev)
  755. break;
  756. }
  757. /* not found, use the nearest rate */
  758. return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);
  759. }
  760. /* parameter locking: returns immediately if tried during streaming */
  761. static int lock_params(struct snd_pcm_runtime *runtime)
  762. {
  763. if (mutex_lock_interruptible(&runtime->oss.params_lock))
  764. return -ERESTARTSYS;
  765. if (atomic_read(&runtime->oss.rw_ref)) {
  766. mutex_unlock(&runtime->oss.params_lock);
  767. return -EBUSY;
  768. }
  769. return 0;
  770. }
  771. static void unlock_params(struct snd_pcm_runtime *runtime)
  772. {
  773. mutex_unlock(&runtime->oss.params_lock);
  774. }
  775. static void snd_pcm_oss_release_buffers(struct snd_pcm_substream *substream)
  776. {
  777. struct snd_pcm_runtime *runtime = substream->runtime;
  778. kvfree(runtime->oss.buffer);
  779. runtime->oss.buffer = NULL;
  780. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  781. snd_pcm_oss_plugin_clear(substream);
  782. #endif
  783. }
  784. /* call with params_lock held */
  785. static int snd_pcm_oss_change_params_locked(struct snd_pcm_substream *substream)
  786. {
  787. struct snd_pcm_runtime *runtime = substream->runtime;
  788. struct snd_pcm_hw_params *params, *sparams;
  789. struct snd_pcm_sw_params *sw_params;
  790. ssize_t oss_buffer_size, oss_period_size;
  791. size_t oss_frame_size;
  792. int err;
  793. int direct;
  794. snd_pcm_format_t format, sformat;
  795. int n;
  796. const struct snd_mask *sformat_mask;
  797. struct snd_mask mask;
  798. if (!runtime->oss.params)
  799. return 0;
  800. sw_params = kzalloc(sizeof(*sw_params), GFP_KERNEL);
  801. params = kmalloc(sizeof(*params), GFP_KERNEL);
  802. sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
  803. if (!sw_params || !params || !sparams) {
  804. err = -ENOMEM;
  805. goto failure;
  806. }
  807. if (atomic_read(&substream->mmap_count))
  808. direct = 1;
  809. else
  810. direct = substream->oss.setup.direct;
  811. _snd_pcm_hw_params_any(sparams);
  812. _snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);
  813. _snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);
  814. snd_mask_none(&mask);
  815. if (atomic_read(&substream->mmap_count))
  816. snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);
  817. else {
  818. snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED);
  819. if (!direct)
  820. snd_mask_set(&mask, (__force int)SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
  821. }
  822. err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);
  823. if (err < 0) {
  824. pcm_dbg(substream->pcm, "No usable accesses\n");
  825. err = -EINVAL;
  826. goto failure;
  827. }
  828. err = choose_rate(substream, sparams, runtime->oss.rate);
  829. if (err < 0)
  830. goto failure;
  831. err = snd_pcm_hw_param_near(substream, sparams,
  832. SNDRV_PCM_HW_PARAM_CHANNELS,
  833. runtime->oss.channels, NULL);
  834. if (err < 0)
  835. goto failure;
  836. format = snd_pcm_oss_format_from(runtime->oss.format);
  837. sformat_mask = hw_param_mask_c(sparams, SNDRV_PCM_HW_PARAM_FORMAT);
  838. if (direct)
  839. sformat = format;
  840. else
  841. sformat = snd_pcm_plug_slave_format(format, sformat_mask);
  842. if ((__force int)sformat < 0 ||
  843. !snd_mask_test_format(sformat_mask, sformat)) {
  844. pcm_for_each_format(sformat) {
  845. if (snd_mask_test_format(sformat_mask, sformat) &&
  846. snd_pcm_oss_format_to(sformat) >= 0)
  847. goto format_found;
  848. }
  849. pcm_dbg(substream->pcm, "Cannot find a format!!!\n");
  850. err = -EINVAL;
  851. goto failure;
  852. }
  853. format_found:
  854. err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, (__force int)sformat, 0);
  855. if (err < 0)
  856. goto failure;
  857. if (direct) {
  858. memcpy(params, sparams, sizeof(*params));
  859. } else {
  860. _snd_pcm_hw_params_any(params);
  861. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,
  862. (__force int)SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);
  863. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,
  864. (__force int)snd_pcm_oss_format_from(runtime->oss.format), 0);
  865. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,
  866. runtime->oss.channels, 0);
  867. _snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,
  868. runtime->oss.rate, 0);
  869. pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",
  870. params_access(params), params_format(params),
  871. params_channels(params), params_rate(params));
  872. }
  873. pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",
  874. params_access(sparams), params_format(sparams),
  875. params_channels(sparams), params_rate(sparams));
  876. oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *
  877. params_channels(params) / 8;
  878. err = snd_pcm_oss_period_size(substream, params, sparams);
  879. if (err < 0)
  880. goto failure;
  881. n = snd_pcm_plug_slave_size(substream, runtime->oss.period_bytes / oss_frame_size);
  882. err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, n, NULL);
  883. if (err < 0)
  884. goto failure;
  885. err = snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_PERIODS,
  886. runtime->oss.periods, NULL);
  887. if (err < 0)
  888. goto failure;
  889. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  890. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_HW_PARAMS, sparams);
  891. if (err < 0) {
  892. pcm_dbg(substream->pcm, "HW_PARAMS failed: %i\n", err);
  893. goto failure;
  894. }
  895. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  896. snd_pcm_oss_plugin_clear(substream);
  897. if (!direct) {
  898. /* add necessary plugins */
  899. err = snd_pcm_plug_format_plugins(substream, params, sparams);
  900. if (err < 0) {
  901. pcm_dbg(substream->pcm,
  902. "snd_pcm_plug_format_plugins failed: %i\n", err);
  903. goto failure;
  904. }
  905. if (runtime->oss.plugin_first) {
  906. struct snd_pcm_plugin *plugin;
  907. err = snd_pcm_plugin_build_io(substream, sparams, &plugin);
  908. if (err < 0) {
  909. pcm_dbg(substream->pcm,
  910. "snd_pcm_plugin_build_io failed: %i\n", err);
  911. goto failure;
  912. }
  913. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  914. err = snd_pcm_plugin_append(plugin);
  915. } else {
  916. err = snd_pcm_plugin_insert(plugin);
  917. }
  918. if (err < 0)
  919. goto failure;
  920. }
  921. }
  922. #endif
  923. if (runtime->oss.trigger) {
  924. sw_params->start_threshold = 1;
  925. } else {
  926. sw_params->start_threshold = runtime->boundary;
  927. }
  928. if (atomic_read(&substream->mmap_count) ||
  929. substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  930. sw_params->stop_threshold = runtime->boundary;
  931. else
  932. sw_params->stop_threshold = runtime->buffer_size;
  933. sw_params->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
  934. sw_params->period_step = 1;
  935. sw_params->avail_min = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  936. 1 : runtime->period_size;
  937. if (atomic_read(&substream->mmap_count) ||
  938. substream->oss.setup.nosilence) {
  939. sw_params->silence_threshold = 0;
  940. sw_params->silence_size = 0;
  941. } else {
  942. snd_pcm_uframes_t frames;
  943. frames = runtime->period_size + 16;
  944. if (frames > runtime->buffer_size)
  945. frames = runtime->buffer_size;
  946. sw_params->silence_threshold = frames;
  947. sw_params->silence_size = frames;
  948. }
  949. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_SW_PARAMS, sw_params);
  950. if (err < 0) {
  951. pcm_dbg(substream->pcm, "SW_PARAMS failed: %i\n", err);
  952. goto failure;
  953. }
  954. runtime->oss.periods = params_periods(sparams);
  955. oss_period_size = snd_pcm_plug_client_size(substream, params_period_size(sparams));
  956. if (oss_period_size < 0) {
  957. err = -EINVAL;
  958. goto failure;
  959. }
  960. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  961. if (runtime->oss.plugin_first) {
  962. err = snd_pcm_plug_alloc(substream, oss_period_size);
  963. if (err < 0)
  964. goto failure;
  965. }
  966. #endif
  967. oss_period_size = array_size(oss_period_size, oss_frame_size);
  968. oss_buffer_size = array_size(oss_period_size, runtime->oss.periods);
  969. if (oss_buffer_size <= 0) {
  970. err = -EINVAL;
  971. goto failure;
  972. }
  973. runtime->oss.period_bytes = oss_period_size;
  974. runtime->oss.buffer_bytes = oss_buffer_size;
  975. pdprintf("oss: period bytes = %i, buffer bytes = %i\n",
  976. runtime->oss.period_bytes,
  977. runtime->oss.buffer_bytes);
  978. pdprintf("slave: period_size = %i, buffer_size = %i\n",
  979. params_period_size(sparams),
  980. params_buffer_size(sparams));
  981. runtime->oss.format = snd_pcm_oss_format_to(params_format(params));
  982. runtime->oss.channels = params_channels(params);
  983. runtime->oss.rate = params_rate(params);
  984. kvfree(runtime->oss.buffer);
  985. runtime->oss.buffer = kvzalloc(runtime->oss.period_bytes, GFP_KERNEL);
  986. if (!runtime->oss.buffer) {
  987. err = -ENOMEM;
  988. goto failure;
  989. }
  990. runtime->oss.params = 0;
  991. runtime->oss.prepare = 1;
  992. runtime->oss.buffer_used = 0;
  993. if (runtime->dma_area)
  994. snd_pcm_format_set_silence(runtime->format, runtime->dma_area, bytes_to_samples(runtime, runtime->dma_bytes));
  995. runtime->oss.period_frames = snd_pcm_alsa_frames(substream, oss_period_size);
  996. err = 0;
  997. failure:
  998. if (err)
  999. snd_pcm_oss_release_buffers(substream);
  1000. kfree(sw_params);
  1001. kfree(params);
  1002. kfree(sparams);
  1003. return err;
  1004. }
  1005. /* this one takes the lock by itself */
  1006. static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream,
  1007. bool trylock)
  1008. {
  1009. struct snd_pcm_runtime *runtime = substream->runtime;
  1010. int err;
  1011. if (trylock) {
  1012. if (!(mutex_trylock(&runtime->oss.params_lock)))
  1013. return -EAGAIN;
  1014. } else if (mutex_lock_interruptible(&runtime->oss.params_lock))
  1015. return -ERESTARTSYS;
  1016. err = snd_pcm_oss_change_params_locked(substream);
  1017. mutex_unlock(&runtime->oss.params_lock);
  1018. return err;
  1019. }
  1020. static int snd_pcm_oss_get_active_substream(struct snd_pcm_oss_file *pcm_oss_file, struct snd_pcm_substream **r_substream)
  1021. {
  1022. int idx, err;
  1023. struct snd_pcm_substream *asubstream = NULL, *substream;
  1024. for (idx = 0; idx < 2; idx++) {
  1025. substream = pcm_oss_file->streams[idx];
  1026. if (substream == NULL)
  1027. continue;
  1028. if (asubstream == NULL)
  1029. asubstream = substream;
  1030. if (substream->runtime->oss.params) {
  1031. err = snd_pcm_oss_change_params(substream, false);
  1032. if (err < 0)
  1033. return err;
  1034. }
  1035. }
  1036. if (!asubstream)
  1037. return -EIO;
  1038. if (r_substream)
  1039. *r_substream = asubstream;
  1040. return 0;
  1041. }
  1042. /* call with params_lock held */
  1043. /* NOTE: this always call PREPARE unconditionally no matter whether
  1044. * runtime->oss.prepare is set or not
  1045. */
  1046. static int snd_pcm_oss_prepare(struct snd_pcm_substream *substream)
  1047. {
  1048. int err;
  1049. struct snd_pcm_runtime *runtime = substream->runtime;
  1050. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_PREPARE, NULL);
  1051. if (err < 0) {
  1052. pcm_dbg(substream->pcm,
  1053. "snd_pcm_oss_prepare: SNDRV_PCM_IOCTL_PREPARE failed\n");
  1054. return err;
  1055. }
  1056. runtime->oss.prepare = 0;
  1057. runtime->oss.prev_hw_ptr_period = 0;
  1058. runtime->oss.period_ptr = 0;
  1059. runtime->oss.buffer_used = 0;
  1060. return 0;
  1061. }
  1062. static int snd_pcm_oss_make_ready(struct snd_pcm_substream *substream)
  1063. {
  1064. struct snd_pcm_runtime *runtime;
  1065. int err;
  1066. runtime = substream->runtime;
  1067. if (runtime->oss.params) {
  1068. err = snd_pcm_oss_change_params(substream, false);
  1069. if (err < 0)
  1070. return err;
  1071. }
  1072. if (runtime->oss.prepare) {
  1073. if (mutex_lock_interruptible(&runtime->oss.params_lock))
  1074. return -ERESTARTSYS;
  1075. err = snd_pcm_oss_prepare(substream);
  1076. mutex_unlock(&runtime->oss.params_lock);
  1077. if (err < 0)
  1078. return err;
  1079. }
  1080. return 0;
  1081. }
  1082. /* call with params_lock held */
  1083. static int snd_pcm_oss_make_ready_locked(struct snd_pcm_substream *substream)
  1084. {
  1085. struct snd_pcm_runtime *runtime;
  1086. int err;
  1087. runtime = substream->runtime;
  1088. if (runtime->oss.params) {
  1089. err = snd_pcm_oss_change_params_locked(substream);
  1090. if (err < 0)
  1091. return err;
  1092. }
  1093. if (runtime->oss.prepare) {
  1094. err = snd_pcm_oss_prepare(substream);
  1095. if (err < 0)
  1096. return err;
  1097. }
  1098. return 0;
  1099. }
  1100. static int snd_pcm_oss_capture_position_fixup(struct snd_pcm_substream *substream, snd_pcm_sframes_t *delay)
  1101. {
  1102. struct snd_pcm_runtime *runtime;
  1103. snd_pcm_uframes_t frames;
  1104. int err = 0;
  1105. while (1) {
  1106. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, delay);
  1107. if (err < 0)
  1108. break;
  1109. runtime = substream->runtime;
  1110. if (*delay <= (snd_pcm_sframes_t)runtime->buffer_size)
  1111. break;
  1112. /* in case of overrun, skip whole periods like OSS/Linux driver does */
  1113. /* until avail(delay) <= buffer_size */
  1114. frames = (*delay - runtime->buffer_size) + runtime->period_size - 1;
  1115. frames /= runtime->period_size;
  1116. frames *= runtime->period_size;
  1117. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_FORWARD, &frames);
  1118. if (err < 0)
  1119. break;
  1120. }
  1121. return err;
  1122. }
  1123. snd_pcm_sframes_t snd_pcm_oss_write3(struct snd_pcm_substream *substream, const char *ptr, snd_pcm_uframes_t frames, int in_kernel)
  1124. {
  1125. struct snd_pcm_runtime *runtime = substream->runtime;
  1126. int ret;
  1127. while (1) {
  1128. if (runtime->state == SNDRV_PCM_STATE_XRUN ||
  1129. runtime->state == SNDRV_PCM_STATE_SUSPENDED) {
  1130. #ifdef OSS_DEBUG
  1131. pcm_dbg(substream->pcm,
  1132. "pcm_oss: write: recovering from %s\n",
  1133. runtime->state == SNDRV_PCM_STATE_XRUN ?
  1134. "XRUN" : "SUSPEND");
  1135. #endif
  1136. ret = snd_pcm_oss_prepare(substream);
  1137. if (ret < 0)
  1138. break;
  1139. }
  1140. mutex_unlock(&runtime->oss.params_lock);
  1141. ret = __snd_pcm_lib_xfer(substream, (void *)ptr, true,
  1142. frames, in_kernel);
  1143. mutex_lock(&runtime->oss.params_lock);
  1144. if (ret != -EPIPE && ret != -ESTRPIPE)
  1145. break;
  1146. /* test, if we can't store new data, because the stream */
  1147. /* has not been started */
  1148. if (runtime->state == SNDRV_PCM_STATE_PREPARED)
  1149. return -EAGAIN;
  1150. }
  1151. return ret;
  1152. }
  1153. snd_pcm_sframes_t snd_pcm_oss_read3(struct snd_pcm_substream *substream, char *ptr, snd_pcm_uframes_t frames, int in_kernel)
  1154. {
  1155. struct snd_pcm_runtime *runtime = substream->runtime;
  1156. snd_pcm_sframes_t delay;
  1157. int ret;
  1158. while (1) {
  1159. if (runtime->state == SNDRV_PCM_STATE_XRUN ||
  1160. runtime->state == SNDRV_PCM_STATE_SUSPENDED) {
  1161. #ifdef OSS_DEBUG
  1162. pcm_dbg(substream->pcm,
  1163. "pcm_oss: read: recovering from %s\n",
  1164. runtime->state == SNDRV_PCM_STATE_XRUN ?
  1165. "XRUN" : "SUSPEND");
  1166. #endif
  1167. ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
  1168. if (ret < 0)
  1169. break;
  1170. } else if (runtime->state == SNDRV_PCM_STATE_SETUP) {
  1171. ret = snd_pcm_oss_prepare(substream);
  1172. if (ret < 0)
  1173. break;
  1174. }
  1175. ret = snd_pcm_oss_capture_position_fixup(substream, &delay);
  1176. if (ret < 0)
  1177. break;
  1178. mutex_unlock(&runtime->oss.params_lock);
  1179. ret = __snd_pcm_lib_xfer(substream, (void *)ptr, true,
  1180. frames, in_kernel);
  1181. mutex_lock(&runtime->oss.params_lock);
  1182. if (ret == -EPIPE) {
  1183. if (runtime->state == SNDRV_PCM_STATE_DRAINING) {
  1184. ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1185. if (ret < 0)
  1186. break;
  1187. }
  1188. continue;
  1189. }
  1190. if (ret != -ESTRPIPE)
  1191. break;
  1192. }
  1193. return ret;
  1194. }
  1195. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  1196. snd_pcm_sframes_t snd_pcm_oss_writev3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames)
  1197. {
  1198. struct snd_pcm_runtime *runtime = substream->runtime;
  1199. int ret;
  1200. while (1) {
  1201. if (runtime->state == SNDRV_PCM_STATE_XRUN ||
  1202. runtime->state == SNDRV_PCM_STATE_SUSPENDED) {
  1203. #ifdef OSS_DEBUG
  1204. pcm_dbg(substream->pcm,
  1205. "pcm_oss: writev: recovering from %s\n",
  1206. runtime->state == SNDRV_PCM_STATE_XRUN ?
  1207. "XRUN" : "SUSPEND");
  1208. #endif
  1209. ret = snd_pcm_oss_prepare(substream);
  1210. if (ret < 0)
  1211. break;
  1212. }
  1213. ret = snd_pcm_kernel_writev(substream, bufs, frames);
  1214. if (ret != -EPIPE && ret != -ESTRPIPE)
  1215. break;
  1216. /* test, if we can't store new data, because the stream */
  1217. /* has not been started */
  1218. if (runtime->state == SNDRV_PCM_STATE_PREPARED)
  1219. return -EAGAIN;
  1220. }
  1221. return ret;
  1222. }
  1223. snd_pcm_sframes_t snd_pcm_oss_readv3(struct snd_pcm_substream *substream, void **bufs, snd_pcm_uframes_t frames)
  1224. {
  1225. struct snd_pcm_runtime *runtime = substream->runtime;
  1226. int ret;
  1227. while (1) {
  1228. if (runtime->state == SNDRV_PCM_STATE_XRUN ||
  1229. runtime->state == SNDRV_PCM_STATE_SUSPENDED) {
  1230. #ifdef OSS_DEBUG
  1231. pcm_dbg(substream->pcm,
  1232. "pcm_oss: readv: recovering from %s\n",
  1233. runtime->state == SNDRV_PCM_STATE_XRUN ?
  1234. "XRUN" : "SUSPEND");
  1235. #endif
  1236. ret = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
  1237. if (ret < 0)
  1238. break;
  1239. } else if (runtime->state == SNDRV_PCM_STATE_SETUP) {
  1240. ret = snd_pcm_oss_prepare(substream);
  1241. if (ret < 0)
  1242. break;
  1243. }
  1244. ret = snd_pcm_kernel_readv(substream, bufs, frames);
  1245. if (ret != -EPIPE && ret != -ESTRPIPE)
  1246. break;
  1247. }
  1248. return ret;
  1249. }
  1250. #endif /* CONFIG_SND_PCM_OSS_PLUGINS */
  1251. static ssize_t snd_pcm_oss_write2(struct snd_pcm_substream *substream, const char *buf, size_t bytes, int in_kernel)
  1252. {
  1253. struct snd_pcm_runtime *runtime = substream->runtime;
  1254. snd_pcm_sframes_t frames, frames1;
  1255. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  1256. if (runtime->oss.plugin_first) {
  1257. struct snd_pcm_plugin_channel *channels;
  1258. size_t oss_frame_bytes = (runtime->oss.plugin_first->src_width * runtime->oss.plugin_first->src_format.channels) / 8;
  1259. if (!in_kernel) {
  1260. if (copy_from_user(runtime->oss.buffer, (const char __force __user *)buf, bytes))
  1261. return -EFAULT;
  1262. buf = runtime->oss.buffer;
  1263. }
  1264. frames = bytes / oss_frame_bytes;
  1265. frames1 = snd_pcm_plug_client_channels_buf(substream, (char *)buf, frames, &channels);
  1266. if (frames1 < 0)
  1267. return frames1;
  1268. frames1 = snd_pcm_plug_write_transfer(substream, channels, frames1);
  1269. if (frames1 <= 0)
  1270. return frames1;
  1271. bytes = frames1 * oss_frame_bytes;
  1272. } else
  1273. #endif
  1274. {
  1275. frames = bytes_to_frames(runtime, bytes);
  1276. frames1 = snd_pcm_oss_write3(substream, buf, frames, in_kernel);
  1277. if (frames1 <= 0)
  1278. return frames1;
  1279. bytes = frames_to_bytes(runtime, frames1);
  1280. }
  1281. return bytes;
  1282. }
  1283. static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const char __user *buf, size_t bytes)
  1284. {
  1285. size_t xfer = 0;
  1286. ssize_t tmp = 0;
  1287. struct snd_pcm_runtime *runtime = substream->runtime;
  1288. if (atomic_read(&substream->mmap_count))
  1289. return -ENXIO;
  1290. atomic_inc(&runtime->oss.rw_ref);
  1291. while (bytes > 0) {
  1292. if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
  1293. tmp = -ERESTARTSYS;
  1294. break;
  1295. }
  1296. tmp = snd_pcm_oss_make_ready_locked(substream);
  1297. if (tmp < 0)
  1298. goto err;
  1299. if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
  1300. tmp = bytes;
  1301. if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
  1302. tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
  1303. if (tmp > 0) {
  1304. if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
  1305. tmp = -EFAULT;
  1306. goto err;
  1307. }
  1308. }
  1309. runtime->oss.buffer_used += tmp;
  1310. buf += tmp;
  1311. bytes -= tmp;
  1312. xfer += tmp;
  1313. if (substream->oss.setup.partialfrag ||
  1314. runtime->oss.buffer_used == runtime->oss.period_bytes) {
  1315. tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
  1316. runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
  1317. if (tmp <= 0)
  1318. goto err;
  1319. runtime->oss.bytes += tmp;
  1320. runtime->oss.period_ptr += tmp;
  1321. runtime->oss.period_ptr %= runtime->oss.period_bytes;
  1322. if (runtime->oss.period_ptr == 0 ||
  1323. runtime->oss.period_ptr == runtime->oss.buffer_used)
  1324. runtime->oss.buffer_used = 0;
  1325. else if ((substream->f_flags & O_NONBLOCK) != 0) {
  1326. tmp = -EAGAIN;
  1327. goto err;
  1328. }
  1329. }
  1330. } else {
  1331. tmp = snd_pcm_oss_write2(substream,
  1332. (const char __force *)buf,
  1333. runtime->oss.period_bytes, 0);
  1334. if (tmp <= 0)
  1335. goto err;
  1336. runtime->oss.bytes += tmp;
  1337. buf += tmp;
  1338. bytes -= tmp;
  1339. xfer += tmp;
  1340. if ((substream->f_flags & O_NONBLOCK) != 0 &&
  1341. tmp != runtime->oss.period_bytes)
  1342. tmp = -EAGAIN;
  1343. }
  1344. err:
  1345. mutex_unlock(&runtime->oss.params_lock);
  1346. if (tmp < 0)
  1347. break;
  1348. if (signal_pending(current)) {
  1349. tmp = -ERESTARTSYS;
  1350. break;
  1351. }
  1352. tmp = 0;
  1353. }
  1354. atomic_dec(&runtime->oss.rw_ref);
  1355. return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
  1356. }
  1357. static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
  1358. {
  1359. struct snd_pcm_runtime *runtime = substream->runtime;
  1360. snd_pcm_sframes_t frames, frames1;
  1361. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  1362. char __user *final_dst = (char __force __user *)buf;
  1363. if (runtime->oss.plugin_first) {
  1364. struct snd_pcm_plugin_channel *channels;
  1365. size_t oss_frame_bytes = (runtime->oss.plugin_last->dst_width * runtime->oss.plugin_last->dst_format.channels) / 8;
  1366. if (!in_kernel)
  1367. buf = runtime->oss.buffer;
  1368. frames = bytes / oss_frame_bytes;
  1369. frames1 = snd_pcm_plug_client_channels_buf(substream, buf, frames, &channels);
  1370. if (frames1 < 0)
  1371. return frames1;
  1372. frames1 = snd_pcm_plug_read_transfer(substream, channels, frames1);
  1373. if (frames1 <= 0)
  1374. return frames1;
  1375. bytes = frames1 * oss_frame_bytes;
  1376. if (!in_kernel && copy_to_user(final_dst, buf, bytes))
  1377. return -EFAULT;
  1378. } else
  1379. #endif
  1380. {
  1381. frames = bytes_to_frames(runtime, bytes);
  1382. frames1 = snd_pcm_oss_read3(substream, buf, frames, in_kernel);
  1383. if (frames1 <= 0)
  1384. return frames1;
  1385. bytes = frames_to_bytes(runtime, frames1);
  1386. }
  1387. return bytes;
  1388. }
  1389. static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __user *buf, size_t bytes)
  1390. {
  1391. size_t xfer = 0;
  1392. ssize_t tmp = 0;
  1393. struct snd_pcm_runtime *runtime = substream->runtime;
  1394. if (atomic_read(&substream->mmap_count))
  1395. return -ENXIO;
  1396. atomic_inc(&runtime->oss.rw_ref);
  1397. while (bytes > 0) {
  1398. if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
  1399. tmp = -ERESTARTSYS;
  1400. break;
  1401. }
  1402. tmp = snd_pcm_oss_make_ready_locked(substream);
  1403. if (tmp < 0)
  1404. goto err;
  1405. if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
  1406. if (runtime->oss.buffer_used == 0) {
  1407. tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
  1408. if (tmp <= 0)
  1409. goto err;
  1410. runtime->oss.bytes += tmp;
  1411. runtime->oss.period_ptr = tmp;
  1412. runtime->oss.buffer_used = tmp;
  1413. }
  1414. tmp = bytes;
  1415. if ((size_t) tmp > runtime->oss.buffer_used)
  1416. tmp = runtime->oss.buffer_used;
  1417. if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
  1418. tmp = -EFAULT;
  1419. goto err;
  1420. }
  1421. buf += tmp;
  1422. bytes -= tmp;
  1423. xfer += tmp;
  1424. runtime->oss.buffer_used -= tmp;
  1425. } else {
  1426. tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
  1427. runtime->oss.period_bytes, 0);
  1428. if (tmp <= 0)
  1429. goto err;
  1430. runtime->oss.bytes += tmp;
  1431. buf += tmp;
  1432. bytes -= tmp;
  1433. xfer += tmp;
  1434. }
  1435. err:
  1436. mutex_unlock(&runtime->oss.params_lock);
  1437. if (tmp < 0)
  1438. break;
  1439. if (signal_pending(current)) {
  1440. tmp = -ERESTARTSYS;
  1441. break;
  1442. }
  1443. tmp = 0;
  1444. }
  1445. atomic_dec(&runtime->oss.rw_ref);
  1446. return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
  1447. }
  1448. static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
  1449. {
  1450. struct snd_pcm_substream *substream;
  1451. struct snd_pcm_runtime *runtime;
  1452. int i;
  1453. for (i = 0; i < 2; i++) {
  1454. substream = pcm_oss_file->streams[i];
  1455. if (!substream)
  1456. continue;
  1457. runtime = substream->runtime;
  1458. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1459. mutex_lock(&runtime->oss.params_lock);
  1460. runtime->oss.prepare = 1;
  1461. runtime->oss.buffer_used = 0;
  1462. runtime->oss.prev_hw_ptr_period = 0;
  1463. runtime->oss.period_ptr = 0;
  1464. mutex_unlock(&runtime->oss.params_lock);
  1465. }
  1466. return 0;
  1467. }
  1468. static int snd_pcm_oss_post(struct snd_pcm_oss_file *pcm_oss_file)
  1469. {
  1470. struct snd_pcm_substream *substream;
  1471. int err;
  1472. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1473. if (substream != NULL) {
  1474. err = snd_pcm_oss_make_ready(substream);
  1475. if (err < 0)
  1476. return err;
  1477. snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_START, NULL);
  1478. }
  1479. /* note: all errors from the start action are ignored */
  1480. /* OSS apps do not know, how to handle them */
  1481. return 0;
  1482. }
  1483. static int snd_pcm_oss_sync1(struct snd_pcm_substream *substream, size_t size)
  1484. {
  1485. struct snd_pcm_runtime *runtime;
  1486. ssize_t result = 0;
  1487. snd_pcm_state_t state;
  1488. long res;
  1489. wait_queue_entry_t wait;
  1490. runtime = substream->runtime;
  1491. init_waitqueue_entry(&wait, current);
  1492. add_wait_queue(&runtime->sleep, &wait);
  1493. #ifdef OSS_DEBUG
  1494. pcm_dbg(substream->pcm, "sync1: size = %li\n", size);
  1495. #endif
  1496. while (1) {
  1497. result = snd_pcm_oss_write2(substream, runtime->oss.buffer, size, 1);
  1498. if (result > 0) {
  1499. runtime->oss.buffer_used = 0;
  1500. result = 0;
  1501. break;
  1502. }
  1503. if (result != 0 && result != -EAGAIN)
  1504. break;
  1505. result = 0;
  1506. set_current_state(TASK_INTERRUPTIBLE);
  1507. scoped_guard(pcm_stream_lock_irq, substream)
  1508. state = runtime->state;
  1509. if (state != SNDRV_PCM_STATE_RUNNING) {
  1510. set_current_state(TASK_RUNNING);
  1511. break;
  1512. }
  1513. res = schedule_timeout(10 * HZ);
  1514. if (signal_pending(current)) {
  1515. result = -ERESTARTSYS;
  1516. break;
  1517. }
  1518. if (res == 0) {
  1519. pcm_err(substream->pcm,
  1520. "OSS sync error - DMA timeout\n");
  1521. result = -EIO;
  1522. break;
  1523. }
  1524. }
  1525. remove_wait_queue(&runtime->sleep, &wait);
  1526. return result;
  1527. }
  1528. static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
  1529. {
  1530. int err = 0;
  1531. unsigned int saved_f_flags;
  1532. struct snd_pcm_substream *substream;
  1533. struct snd_pcm_runtime *runtime;
  1534. snd_pcm_format_t format;
  1535. unsigned long width;
  1536. size_t size;
  1537. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1538. if (substream != NULL) {
  1539. runtime = substream->runtime;
  1540. if (atomic_read(&substream->mmap_count))
  1541. goto __direct;
  1542. atomic_inc(&runtime->oss.rw_ref);
  1543. if (mutex_lock_interruptible(&runtime->oss.params_lock)) {
  1544. atomic_dec(&runtime->oss.rw_ref);
  1545. return -ERESTARTSYS;
  1546. }
  1547. err = snd_pcm_oss_make_ready_locked(substream);
  1548. if (err < 0)
  1549. goto unlock;
  1550. format = snd_pcm_oss_format_from(runtime->oss.format);
  1551. width = snd_pcm_format_physical_width(format);
  1552. if (runtime->oss.buffer_used > 0) {
  1553. #ifdef OSS_DEBUG
  1554. pcm_dbg(substream->pcm, "sync: buffer_used\n");
  1555. #endif
  1556. size = (8 * (runtime->oss.period_bytes - runtime->oss.buffer_used) + 7) / width;
  1557. snd_pcm_format_set_silence(format,
  1558. runtime->oss.buffer + runtime->oss.buffer_used,
  1559. size);
  1560. err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
  1561. if (err < 0)
  1562. goto unlock;
  1563. } else if (runtime->oss.period_ptr > 0) {
  1564. #ifdef OSS_DEBUG
  1565. pcm_dbg(substream->pcm, "sync: period_ptr\n");
  1566. #endif
  1567. size = runtime->oss.period_bytes - runtime->oss.period_ptr;
  1568. snd_pcm_format_set_silence(format,
  1569. runtime->oss.buffer,
  1570. size * 8 / width);
  1571. err = snd_pcm_oss_sync1(substream, size);
  1572. if (err < 0)
  1573. goto unlock;
  1574. }
  1575. /*
  1576. * The ALSA's period might be a bit large than OSS one.
  1577. * Fill the remain portion of ALSA period with zeros.
  1578. */
  1579. size = runtime->control->appl_ptr % runtime->period_size;
  1580. if (size > 0) {
  1581. size = runtime->period_size - size;
  1582. if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED)
  1583. snd_pcm_lib_write(substream, NULL, size);
  1584. else if (runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
  1585. snd_pcm_lib_writev(substream, NULL, size);
  1586. }
  1587. unlock:
  1588. mutex_unlock(&runtime->oss.params_lock);
  1589. atomic_dec(&runtime->oss.rw_ref);
  1590. if (err < 0)
  1591. return err;
  1592. /*
  1593. * finish sync: drain the buffer
  1594. */
  1595. __direct:
  1596. saved_f_flags = substream->f_flags;
  1597. substream->f_flags &= ~O_NONBLOCK;
  1598. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DRAIN, NULL);
  1599. substream->f_flags = saved_f_flags;
  1600. if (err < 0)
  1601. return err;
  1602. mutex_lock(&runtime->oss.params_lock);
  1603. runtime->oss.prepare = 1;
  1604. mutex_unlock(&runtime->oss.params_lock);
  1605. }
  1606. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1607. if (substream != NULL) {
  1608. err = snd_pcm_oss_make_ready(substream);
  1609. if (err < 0)
  1610. return err;
  1611. runtime = substream->runtime;
  1612. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DROP, NULL);
  1613. if (err < 0)
  1614. return err;
  1615. mutex_lock(&runtime->oss.params_lock);
  1616. runtime->oss.buffer_used = 0;
  1617. runtime->oss.prepare = 1;
  1618. mutex_unlock(&runtime->oss.params_lock);
  1619. }
  1620. return 0;
  1621. }
  1622. static int snd_pcm_oss_set_rate(struct snd_pcm_oss_file *pcm_oss_file, int rate)
  1623. {
  1624. int idx;
  1625. for (idx = 1; idx >= 0; --idx) {
  1626. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1627. struct snd_pcm_runtime *runtime;
  1628. int err;
  1629. if (substream == NULL)
  1630. continue;
  1631. runtime = substream->runtime;
  1632. if (rate < 1000)
  1633. rate = 1000;
  1634. else if (rate > 192000)
  1635. rate = 192000;
  1636. err = lock_params(runtime);
  1637. if (err < 0)
  1638. return err;
  1639. if (runtime->oss.rate != rate) {
  1640. runtime->oss.params = 1;
  1641. runtime->oss.rate = rate;
  1642. }
  1643. unlock_params(runtime);
  1644. }
  1645. return snd_pcm_oss_get_rate(pcm_oss_file);
  1646. }
  1647. static int snd_pcm_oss_get_rate(struct snd_pcm_oss_file *pcm_oss_file)
  1648. {
  1649. struct snd_pcm_substream *substream;
  1650. int err;
  1651. err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
  1652. if (err < 0)
  1653. return err;
  1654. return substream->runtime->oss.rate;
  1655. }
  1656. static int snd_pcm_oss_set_channels(struct snd_pcm_oss_file *pcm_oss_file, unsigned int channels)
  1657. {
  1658. int idx;
  1659. if (channels < 1)
  1660. channels = 1;
  1661. if (channels > 128)
  1662. return -EINVAL;
  1663. for (idx = 1; idx >= 0; --idx) {
  1664. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1665. struct snd_pcm_runtime *runtime;
  1666. int err;
  1667. if (substream == NULL)
  1668. continue;
  1669. runtime = substream->runtime;
  1670. err = lock_params(runtime);
  1671. if (err < 0)
  1672. return err;
  1673. if (runtime->oss.channels != channels) {
  1674. runtime->oss.params = 1;
  1675. runtime->oss.channels = channels;
  1676. }
  1677. unlock_params(runtime);
  1678. }
  1679. return snd_pcm_oss_get_channels(pcm_oss_file);
  1680. }
  1681. static int snd_pcm_oss_get_channels(struct snd_pcm_oss_file *pcm_oss_file)
  1682. {
  1683. struct snd_pcm_substream *substream;
  1684. int err;
  1685. err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
  1686. if (err < 0)
  1687. return err;
  1688. return substream->runtime->oss.channels;
  1689. }
  1690. static int snd_pcm_oss_get_block_size(struct snd_pcm_oss_file *pcm_oss_file)
  1691. {
  1692. struct snd_pcm_substream *substream;
  1693. int err;
  1694. err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
  1695. if (err < 0)
  1696. return err;
  1697. return substream->runtime->oss.period_bytes;
  1698. }
  1699. static int snd_pcm_oss_get_formats(struct snd_pcm_oss_file *pcm_oss_file)
  1700. {
  1701. struct snd_pcm_substream *substream;
  1702. int err;
  1703. int direct;
  1704. struct snd_pcm_hw_params *params __free(kfree) = NULL;
  1705. unsigned int formats = 0;
  1706. const struct snd_mask *format_mask;
  1707. int fmt;
  1708. err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
  1709. if (err < 0)
  1710. return err;
  1711. if (atomic_read(&substream->mmap_count))
  1712. direct = 1;
  1713. else
  1714. direct = substream->oss.setup.direct;
  1715. if (!direct)
  1716. return AFMT_MU_LAW | AFMT_U8 |
  1717. AFMT_S16_LE | AFMT_S16_BE |
  1718. AFMT_S8 | AFMT_U16_LE |
  1719. AFMT_U16_BE |
  1720. AFMT_S32_LE | AFMT_S32_BE |
  1721. AFMT_S24_LE | AFMT_S24_BE |
  1722. AFMT_S24_PACKED;
  1723. params = kmalloc(sizeof(*params), GFP_KERNEL);
  1724. if (!params)
  1725. return -ENOMEM;
  1726. _snd_pcm_hw_params_any(params);
  1727. err = snd_pcm_hw_refine(substream, params);
  1728. if (err < 0)
  1729. return err;
  1730. format_mask = hw_param_mask_c(params, SNDRV_PCM_HW_PARAM_FORMAT);
  1731. for (fmt = 0; fmt < 32; ++fmt) {
  1732. if (snd_mask_test(format_mask, fmt)) {
  1733. int f = snd_pcm_oss_format_to((__force snd_pcm_format_t)fmt);
  1734. if (f >= 0)
  1735. formats |= f;
  1736. }
  1737. }
  1738. return formats;
  1739. }
  1740. static int snd_pcm_oss_set_format(struct snd_pcm_oss_file *pcm_oss_file, int format)
  1741. {
  1742. int formats, idx;
  1743. int err;
  1744. if (format != AFMT_QUERY) {
  1745. formats = snd_pcm_oss_get_formats(pcm_oss_file);
  1746. if (formats < 0)
  1747. return formats;
  1748. if (!(formats & format))
  1749. format = AFMT_U8;
  1750. for (idx = 1; idx >= 0; --idx) {
  1751. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1752. struct snd_pcm_runtime *runtime;
  1753. if (substream == NULL)
  1754. continue;
  1755. runtime = substream->runtime;
  1756. err = lock_params(runtime);
  1757. if (err < 0)
  1758. return err;
  1759. if (runtime->oss.format != format) {
  1760. runtime->oss.params = 1;
  1761. runtime->oss.format = format;
  1762. }
  1763. unlock_params(runtime);
  1764. }
  1765. }
  1766. return snd_pcm_oss_get_format(pcm_oss_file);
  1767. }
  1768. static int snd_pcm_oss_get_format(struct snd_pcm_oss_file *pcm_oss_file)
  1769. {
  1770. struct snd_pcm_substream *substream;
  1771. int err;
  1772. err = snd_pcm_oss_get_active_substream(pcm_oss_file, &substream);
  1773. if (err < 0)
  1774. return err;
  1775. return substream->runtime->oss.format;
  1776. }
  1777. static int snd_pcm_oss_set_subdivide1(struct snd_pcm_substream *substream, int subdivide)
  1778. {
  1779. struct snd_pcm_runtime *runtime;
  1780. runtime = substream->runtime;
  1781. if (subdivide == 0) {
  1782. subdivide = runtime->oss.subdivision;
  1783. if (subdivide == 0)
  1784. subdivide = 1;
  1785. return subdivide;
  1786. }
  1787. if (runtime->oss.subdivision || runtime->oss.fragshift)
  1788. return -EINVAL;
  1789. if (subdivide != 1 && subdivide != 2 && subdivide != 4 &&
  1790. subdivide != 8 && subdivide != 16)
  1791. return -EINVAL;
  1792. runtime->oss.subdivision = subdivide;
  1793. runtime->oss.params = 1;
  1794. return subdivide;
  1795. }
  1796. static int snd_pcm_oss_set_subdivide(struct snd_pcm_oss_file *pcm_oss_file, int subdivide)
  1797. {
  1798. int err = -EINVAL, idx;
  1799. for (idx = 1; idx >= 0; --idx) {
  1800. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1801. struct snd_pcm_runtime *runtime;
  1802. if (substream == NULL)
  1803. continue;
  1804. runtime = substream->runtime;
  1805. err = lock_params(runtime);
  1806. if (err < 0)
  1807. return err;
  1808. err = snd_pcm_oss_set_subdivide1(substream, subdivide);
  1809. unlock_params(runtime);
  1810. if (err < 0)
  1811. return err;
  1812. }
  1813. return err;
  1814. }
  1815. static int snd_pcm_oss_set_fragment1(struct snd_pcm_substream *substream, unsigned int val)
  1816. {
  1817. struct snd_pcm_runtime *runtime;
  1818. int fragshift;
  1819. runtime = substream->runtime;
  1820. if (runtime->oss.subdivision || runtime->oss.fragshift)
  1821. return -EINVAL;
  1822. fragshift = val & 0xffff;
  1823. if (fragshift >= 25) /* should be large enough */
  1824. return -EINVAL;
  1825. runtime->oss.fragshift = fragshift;
  1826. runtime->oss.maxfrags = (val >> 16) & 0xffff;
  1827. if (runtime->oss.fragshift < 4) /* < 16 */
  1828. runtime->oss.fragshift = 4;
  1829. if (runtime->oss.maxfrags < 2)
  1830. runtime->oss.maxfrags = 2;
  1831. runtime->oss.params = 1;
  1832. return 0;
  1833. }
  1834. static int snd_pcm_oss_set_fragment(struct snd_pcm_oss_file *pcm_oss_file, unsigned int val)
  1835. {
  1836. int err = -EINVAL, idx;
  1837. for (idx = 1; idx >= 0; --idx) {
  1838. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1839. struct snd_pcm_runtime *runtime;
  1840. if (substream == NULL)
  1841. continue;
  1842. runtime = substream->runtime;
  1843. err = lock_params(runtime);
  1844. if (err < 0)
  1845. return err;
  1846. err = snd_pcm_oss_set_fragment1(substream, val);
  1847. unlock_params(runtime);
  1848. if (err < 0)
  1849. return err;
  1850. }
  1851. return err;
  1852. }
  1853. static int snd_pcm_oss_nonblock(struct file * file)
  1854. {
  1855. spin_lock(&file->f_lock);
  1856. file->f_flags |= O_NONBLOCK;
  1857. spin_unlock(&file->f_lock);
  1858. return 0;
  1859. }
  1860. static int snd_pcm_oss_get_caps1(struct snd_pcm_substream *substream, int res)
  1861. {
  1862. if (substream == NULL) {
  1863. res &= ~DSP_CAP_DUPLEX;
  1864. return res;
  1865. }
  1866. #ifdef DSP_CAP_MULTI
  1867. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1868. if (substream->pstr->substream_count > 1)
  1869. res |= DSP_CAP_MULTI;
  1870. #endif
  1871. /* DSP_CAP_REALTIME is set all times: */
  1872. /* all ALSA drivers can return actual pointer in ring buffer */
  1873. #if defined(DSP_CAP_REALTIME) && 0
  1874. {
  1875. struct snd_pcm_runtime *runtime = substream->runtime;
  1876. if (runtime->info & (SNDRV_PCM_INFO_BLOCK_TRANSFER|SNDRV_PCM_INFO_BATCH))
  1877. res &= ~DSP_CAP_REALTIME;
  1878. }
  1879. #endif
  1880. return res;
  1881. }
  1882. static int snd_pcm_oss_get_caps(struct snd_pcm_oss_file *pcm_oss_file)
  1883. {
  1884. int result, idx;
  1885. result = DSP_CAP_TRIGGER | DSP_CAP_MMAP | DSP_CAP_DUPLEX | DSP_CAP_REALTIME;
  1886. for (idx = 0; idx < 2; idx++) {
  1887. struct snd_pcm_substream *substream = pcm_oss_file->streams[idx];
  1888. result = snd_pcm_oss_get_caps1(substream, result);
  1889. }
  1890. result |= 0x0001; /* revision - same as SB AWE 64 */
  1891. return result;
  1892. }
  1893. static void snd_pcm_oss_simulate_fill(struct snd_pcm_substream *substream,
  1894. snd_pcm_uframes_t hw_ptr)
  1895. {
  1896. struct snd_pcm_runtime *runtime = substream->runtime;
  1897. snd_pcm_uframes_t appl_ptr;
  1898. appl_ptr = hw_ptr + runtime->buffer_size;
  1899. appl_ptr %= runtime->boundary;
  1900. runtime->control->appl_ptr = appl_ptr;
  1901. }
  1902. static int snd_pcm_oss_set_trigger(struct snd_pcm_oss_file *pcm_oss_file, int trigger)
  1903. {
  1904. struct snd_pcm_runtime *runtime;
  1905. struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
  1906. int err, cmd;
  1907. #ifdef OSS_DEBUG
  1908. pr_debug("pcm_oss: trigger = 0x%x\n", trigger);
  1909. #endif
  1910. psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1911. csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1912. if (psubstream) {
  1913. err = snd_pcm_oss_make_ready(psubstream);
  1914. if (err < 0)
  1915. return err;
  1916. }
  1917. if (csubstream) {
  1918. err = snd_pcm_oss_make_ready(csubstream);
  1919. if (err < 0)
  1920. return err;
  1921. }
  1922. if (psubstream) {
  1923. runtime = psubstream->runtime;
  1924. cmd = 0;
  1925. if (mutex_lock_interruptible(&runtime->oss.params_lock))
  1926. return -ERESTARTSYS;
  1927. if (trigger & PCM_ENABLE_OUTPUT) {
  1928. if (runtime->oss.trigger)
  1929. goto _skip1;
  1930. if (atomic_read(&psubstream->mmap_count))
  1931. snd_pcm_oss_simulate_fill(psubstream,
  1932. get_hw_ptr_period(runtime));
  1933. runtime->oss.trigger = 1;
  1934. runtime->start_threshold = 1;
  1935. cmd = SNDRV_PCM_IOCTL_START;
  1936. } else {
  1937. if (!runtime->oss.trigger)
  1938. goto _skip1;
  1939. runtime->oss.trigger = 0;
  1940. runtime->start_threshold = runtime->boundary;
  1941. cmd = SNDRV_PCM_IOCTL_DROP;
  1942. runtime->oss.prepare = 1;
  1943. }
  1944. _skip1:
  1945. mutex_unlock(&runtime->oss.params_lock);
  1946. if (cmd) {
  1947. err = snd_pcm_kernel_ioctl(psubstream, cmd, NULL);
  1948. if (err < 0)
  1949. return err;
  1950. }
  1951. }
  1952. if (csubstream) {
  1953. runtime = csubstream->runtime;
  1954. cmd = 0;
  1955. if (mutex_lock_interruptible(&runtime->oss.params_lock))
  1956. return -ERESTARTSYS;
  1957. if (trigger & PCM_ENABLE_INPUT) {
  1958. if (runtime->oss.trigger)
  1959. goto _skip2;
  1960. runtime->oss.trigger = 1;
  1961. runtime->start_threshold = 1;
  1962. cmd = SNDRV_PCM_IOCTL_START;
  1963. } else {
  1964. if (!runtime->oss.trigger)
  1965. goto _skip2;
  1966. runtime->oss.trigger = 0;
  1967. runtime->start_threshold = runtime->boundary;
  1968. cmd = SNDRV_PCM_IOCTL_DROP;
  1969. runtime->oss.prepare = 1;
  1970. }
  1971. _skip2:
  1972. mutex_unlock(&runtime->oss.params_lock);
  1973. if (cmd) {
  1974. err = snd_pcm_kernel_ioctl(csubstream, cmd, NULL);
  1975. if (err < 0)
  1976. return err;
  1977. }
  1978. }
  1979. return 0;
  1980. }
  1981. static int snd_pcm_oss_get_trigger(struct snd_pcm_oss_file *pcm_oss_file)
  1982. {
  1983. struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
  1984. int result = 0;
  1985. psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  1986. csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  1987. if (psubstream && psubstream->runtime && psubstream->runtime->oss.trigger)
  1988. result |= PCM_ENABLE_OUTPUT;
  1989. if (csubstream && csubstream->runtime && csubstream->runtime->oss.trigger)
  1990. result |= PCM_ENABLE_INPUT;
  1991. return result;
  1992. }
  1993. static int snd_pcm_oss_get_odelay(struct snd_pcm_oss_file *pcm_oss_file)
  1994. {
  1995. struct snd_pcm_substream *substream;
  1996. struct snd_pcm_runtime *runtime;
  1997. snd_pcm_sframes_t delay;
  1998. int err;
  1999. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2000. if (substream == NULL)
  2001. return -EINVAL;
  2002. err = snd_pcm_oss_make_ready(substream);
  2003. if (err < 0)
  2004. return err;
  2005. runtime = substream->runtime;
  2006. if (runtime->oss.params || runtime->oss.prepare)
  2007. return 0;
  2008. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
  2009. if (err == -EPIPE)
  2010. delay = 0; /* hack for broken OSS applications */
  2011. else if (err < 0)
  2012. return err;
  2013. return snd_pcm_oss_bytes(substream, delay);
  2014. }
  2015. static int snd_pcm_oss_get_ptr(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct count_info __user * _info)
  2016. {
  2017. struct snd_pcm_substream *substream;
  2018. struct snd_pcm_runtime *runtime;
  2019. snd_pcm_sframes_t delay;
  2020. int fixup;
  2021. struct count_info info;
  2022. int err;
  2023. if (_info == NULL)
  2024. return -EFAULT;
  2025. substream = pcm_oss_file->streams[stream];
  2026. if (substream == NULL)
  2027. return -EINVAL;
  2028. err = snd_pcm_oss_make_ready(substream);
  2029. if (err < 0)
  2030. return err;
  2031. runtime = substream->runtime;
  2032. if (runtime->oss.params || runtime->oss.prepare) {
  2033. memset(&info, 0, sizeof(info));
  2034. if (copy_to_user(_info, &info, sizeof(info)))
  2035. return -EFAULT;
  2036. return 0;
  2037. }
  2038. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2039. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &delay);
  2040. if (err == -EPIPE || err == -ESTRPIPE || (! err && delay < 0)) {
  2041. err = 0;
  2042. delay = 0;
  2043. fixup = 0;
  2044. } else {
  2045. fixup = runtime->oss.buffer_used;
  2046. }
  2047. } else {
  2048. err = snd_pcm_oss_capture_position_fixup(substream, &delay);
  2049. fixup = -runtime->oss.buffer_used;
  2050. }
  2051. if (err < 0)
  2052. return err;
  2053. info.ptr = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr % runtime->buffer_size);
  2054. if (atomic_read(&substream->mmap_count)) {
  2055. snd_pcm_sframes_t n;
  2056. delay = get_hw_ptr_period(runtime);
  2057. n = delay - runtime->oss.prev_hw_ptr_period;
  2058. if (n < 0)
  2059. n += runtime->boundary;
  2060. info.blocks = n / runtime->period_size;
  2061. runtime->oss.prev_hw_ptr_period = delay;
  2062. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  2063. snd_pcm_oss_simulate_fill(substream, delay);
  2064. info.bytes = snd_pcm_oss_bytes(substream, runtime->status->hw_ptr) & INT_MAX;
  2065. } else {
  2066. delay = snd_pcm_oss_bytes(substream, delay);
  2067. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2068. if (substream->oss.setup.buggyptr)
  2069. info.blocks = (runtime->oss.buffer_bytes - delay - fixup) / runtime->oss.period_bytes;
  2070. else
  2071. info.blocks = (delay + fixup) / runtime->oss.period_bytes;
  2072. info.bytes = (runtime->oss.bytes - delay) & INT_MAX;
  2073. } else {
  2074. delay += fixup;
  2075. info.blocks = delay / runtime->oss.period_bytes;
  2076. info.bytes = (runtime->oss.bytes + delay) & INT_MAX;
  2077. }
  2078. }
  2079. if (copy_to_user(_info, &info, sizeof(info)))
  2080. return -EFAULT;
  2081. return 0;
  2082. }
  2083. static int snd_pcm_oss_get_space(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct audio_buf_info __user *_info)
  2084. {
  2085. struct snd_pcm_substream *substream;
  2086. struct snd_pcm_runtime *runtime;
  2087. snd_pcm_sframes_t avail;
  2088. int fixup;
  2089. struct audio_buf_info info;
  2090. int err;
  2091. if (_info == NULL)
  2092. return -EFAULT;
  2093. substream = pcm_oss_file->streams[stream];
  2094. if (substream == NULL)
  2095. return -EINVAL;
  2096. runtime = substream->runtime;
  2097. if (runtime->oss.params) {
  2098. err = snd_pcm_oss_change_params(substream, false);
  2099. if (err < 0)
  2100. return err;
  2101. }
  2102. info.fragsize = runtime->oss.period_bytes;
  2103. info.fragstotal = runtime->periods;
  2104. if (runtime->oss.prepare) {
  2105. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2106. info.bytes = runtime->oss.period_bytes * runtime->oss.periods;
  2107. info.fragments = runtime->oss.periods;
  2108. } else {
  2109. info.bytes = 0;
  2110. info.fragments = 0;
  2111. }
  2112. } else {
  2113. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2114. err = snd_pcm_kernel_ioctl(substream, SNDRV_PCM_IOCTL_DELAY, &avail);
  2115. if (err == -EPIPE || err == -ESTRPIPE || (! err && avail < 0)) {
  2116. avail = runtime->buffer_size;
  2117. err = 0;
  2118. fixup = 0;
  2119. } else {
  2120. avail = runtime->buffer_size - avail;
  2121. fixup = -runtime->oss.buffer_used;
  2122. }
  2123. } else {
  2124. err = snd_pcm_oss_capture_position_fixup(substream, &avail);
  2125. fixup = runtime->oss.buffer_used;
  2126. }
  2127. if (err < 0)
  2128. return err;
  2129. info.bytes = snd_pcm_oss_bytes(substream, avail) + fixup;
  2130. info.fragments = info.bytes / runtime->oss.period_bytes;
  2131. }
  2132. #ifdef OSS_DEBUG
  2133. pcm_dbg(substream->pcm,
  2134. "pcm_oss: space: bytes = %i, fragments = %i, fragstotal = %i, fragsize = %i\n",
  2135. info.bytes, info.fragments, info.fragstotal, info.fragsize);
  2136. #endif
  2137. if (copy_to_user(_info, &info, sizeof(info)))
  2138. return -EFAULT;
  2139. return 0;
  2140. }
  2141. static int snd_pcm_oss_get_mapbuf(struct snd_pcm_oss_file *pcm_oss_file, int stream, struct buffmem_desc __user * _info)
  2142. {
  2143. // it won't be probably implemented
  2144. // pr_debug("TODO: snd_pcm_oss_get_mapbuf\n");
  2145. return -EINVAL;
  2146. }
  2147. static const char *strip_task_path(const char *path)
  2148. {
  2149. const char *ptr, *ptrl = NULL;
  2150. for (ptr = path; *ptr; ptr++) {
  2151. if (*ptr == '/')
  2152. ptrl = ptr + 1;
  2153. }
  2154. return ptrl;
  2155. }
  2156. static void snd_pcm_oss_look_for_setup(struct snd_pcm *pcm, int stream,
  2157. const char *task_name,
  2158. struct snd_pcm_oss_setup *rsetup)
  2159. {
  2160. struct snd_pcm_oss_setup *setup;
  2161. guard(mutex)(&pcm->streams[stream].oss.setup_mutex);
  2162. do {
  2163. for (setup = pcm->streams[stream].oss.setup_list; setup;
  2164. setup = setup->next) {
  2165. if (!strcmp(setup->task_name, task_name))
  2166. goto out;
  2167. }
  2168. } while ((task_name = strip_task_path(task_name)) != NULL);
  2169. out:
  2170. if (setup)
  2171. *rsetup = *setup;
  2172. }
  2173. static void snd_pcm_oss_release_substream(struct snd_pcm_substream *substream)
  2174. {
  2175. snd_pcm_oss_release_buffers(substream);
  2176. substream->oss.oss = 0;
  2177. }
  2178. static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
  2179. struct snd_pcm_oss_setup *setup,
  2180. int minor)
  2181. {
  2182. struct snd_pcm_runtime *runtime;
  2183. substream->oss.oss = 1;
  2184. substream->oss.setup = *setup;
  2185. if (setup->nonblock)
  2186. substream->f_flags |= O_NONBLOCK;
  2187. else if (setup->block)
  2188. substream->f_flags &= ~O_NONBLOCK;
  2189. runtime = substream->runtime;
  2190. runtime->oss.params = 1;
  2191. runtime->oss.trigger = 1;
  2192. runtime->oss.rate = 8000;
  2193. mutex_init(&runtime->oss.params_lock);
  2194. switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
  2195. case SNDRV_MINOR_OSS_PCM_8:
  2196. runtime->oss.format = AFMT_U8;
  2197. break;
  2198. case SNDRV_MINOR_OSS_PCM_16:
  2199. runtime->oss.format = AFMT_S16_LE;
  2200. break;
  2201. default:
  2202. runtime->oss.format = AFMT_MU_LAW;
  2203. }
  2204. runtime->oss.channels = 1;
  2205. runtime->oss.fragshift = 0;
  2206. runtime->oss.maxfrags = 0;
  2207. runtime->oss.subdivision = 0;
  2208. substream->pcm_release = snd_pcm_oss_release_substream;
  2209. atomic_set(&runtime->oss.rw_ref, 0);
  2210. }
  2211. static int snd_pcm_oss_release_file(struct snd_pcm_oss_file *pcm_oss_file)
  2212. {
  2213. int cidx;
  2214. if (!pcm_oss_file)
  2215. return 0;
  2216. for (cidx = 0; cidx < 2; ++cidx) {
  2217. struct snd_pcm_substream *substream = pcm_oss_file->streams[cidx];
  2218. if (substream)
  2219. snd_pcm_release_substream(substream);
  2220. }
  2221. kfree(pcm_oss_file);
  2222. return 0;
  2223. }
  2224. static int snd_pcm_oss_open_file(struct file *file,
  2225. struct snd_pcm *pcm,
  2226. struct snd_pcm_oss_file **rpcm_oss_file,
  2227. int minor,
  2228. struct snd_pcm_oss_setup *setup)
  2229. {
  2230. int idx, err;
  2231. struct snd_pcm_oss_file *pcm_oss_file;
  2232. struct snd_pcm_substream *substream;
  2233. fmode_t f_mode = file->f_mode;
  2234. if (rpcm_oss_file)
  2235. *rpcm_oss_file = NULL;
  2236. pcm_oss_file = kzalloc(sizeof(*pcm_oss_file), GFP_KERNEL);
  2237. if (pcm_oss_file == NULL)
  2238. return -ENOMEM;
  2239. if ((f_mode & (FMODE_WRITE|FMODE_READ)) == (FMODE_WRITE|FMODE_READ) &&
  2240. (pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX))
  2241. f_mode = FMODE_WRITE;
  2242. file->f_flags &= ~O_APPEND;
  2243. for (idx = 0; idx < 2; idx++) {
  2244. if (setup[idx].disable)
  2245. continue;
  2246. if (! pcm->streams[idx].substream_count)
  2247. continue; /* no matching substream */
  2248. if (idx == SNDRV_PCM_STREAM_PLAYBACK) {
  2249. if (! (f_mode & FMODE_WRITE))
  2250. continue;
  2251. } else {
  2252. if (! (f_mode & FMODE_READ))
  2253. continue;
  2254. }
  2255. err = snd_pcm_open_substream(pcm, idx, file, &substream);
  2256. if (err < 0) {
  2257. snd_pcm_oss_release_file(pcm_oss_file);
  2258. return err;
  2259. }
  2260. pcm_oss_file->streams[idx] = substream;
  2261. snd_pcm_oss_init_substream(substream, &setup[idx], minor);
  2262. }
  2263. if (!pcm_oss_file->streams[0] && !pcm_oss_file->streams[1]) {
  2264. snd_pcm_oss_release_file(pcm_oss_file);
  2265. return -EINVAL;
  2266. }
  2267. file->private_data = pcm_oss_file;
  2268. if (rpcm_oss_file)
  2269. *rpcm_oss_file = pcm_oss_file;
  2270. return 0;
  2271. }
  2272. static int snd_task_name(struct task_struct *task, char *name, size_t size)
  2273. {
  2274. unsigned int idx;
  2275. if (snd_BUG_ON(!task || !name || size < 2))
  2276. return -EINVAL;
  2277. for (idx = 0; idx < sizeof(task->comm) && idx + 1 < size; idx++)
  2278. name[idx] = task->comm[idx];
  2279. name[idx] = '\0';
  2280. return 0;
  2281. }
  2282. static int snd_pcm_oss_open(struct inode *inode, struct file *file)
  2283. {
  2284. int err;
  2285. char task_name[32];
  2286. struct snd_pcm *pcm;
  2287. struct snd_pcm_oss_file *pcm_oss_file;
  2288. struct snd_pcm_oss_setup setup[2];
  2289. int nonblock;
  2290. wait_queue_entry_t wait;
  2291. err = nonseekable_open(inode, file);
  2292. if (err < 0)
  2293. return err;
  2294. pcm = snd_lookup_oss_minor_data(iminor(inode),
  2295. SNDRV_OSS_DEVICE_TYPE_PCM);
  2296. if (pcm == NULL) {
  2297. err = -ENODEV;
  2298. goto __error1;
  2299. }
  2300. err = snd_card_file_add(pcm->card, file);
  2301. if (err < 0)
  2302. goto __error1;
  2303. if (!try_module_get(pcm->card->module)) {
  2304. err = -EFAULT;
  2305. goto __error2;
  2306. }
  2307. if (snd_task_name(current, task_name, sizeof(task_name)) < 0) {
  2308. err = -EFAULT;
  2309. goto __error;
  2310. }
  2311. memset(setup, 0, sizeof(setup));
  2312. if (file->f_mode & FMODE_WRITE)
  2313. snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  2314. task_name, &setup[0]);
  2315. if (file->f_mode & FMODE_READ)
  2316. snd_pcm_oss_look_for_setup(pcm, SNDRV_PCM_STREAM_CAPTURE,
  2317. task_name, &setup[1]);
  2318. nonblock = !!(file->f_flags & O_NONBLOCK);
  2319. if (!nonblock)
  2320. nonblock = nonblock_open;
  2321. init_waitqueue_entry(&wait, current);
  2322. add_wait_queue(&pcm->open_wait, &wait);
  2323. mutex_lock(&pcm->open_mutex);
  2324. while (1) {
  2325. err = snd_pcm_oss_open_file(file, pcm, &pcm_oss_file,
  2326. iminor(inode), setup);
  2327. if (err >= 0)
  2328. break;
  2329. if (err == -EAGAIN) {
  2330. if (nonblock) {
  2331. err = -EBUSY;
  2332. break;
  2333. }
  2334. } else
  2335. break;
  2336. set_current_state(TASK_INTERRUPTIBLE);
  2337. mutex_unlock(&pcm->open_mutex);
  2338. schedule();
  2339. mutex_lock(&pcm->open_mutex);
  2340. if (pcm->card->shutdown) {
  2341. err = -ENODEV;
  2342. break;
  2343. }
  2344. if (signal_pending(current)) {
  2345. err = -ERESTARTSYS;
  2346. break;
  2347. }
  2348. }
  2349. remove_wait_queue(&pcm->open_wait, &wait);
  2350. mutex_unlock(&pcm->open_mutex);
  2351. if (err < 0)
  2352. goto __error;
  2353. snd_card_unref(pcm->card);
  2354. return err;
  2355. __error:
  2356. module_put(pcm->card->module);
  2357. __error2:
  2358. snd_card_file_remove(pcm->card, file);
  2359. __error1:
  2360. if (pcm)
  2361. snd_card_unref(pcm->card);
  2362. return err;
  2363. }
  2364. static int snd_pcm_oss_release(struct inode *inode, struct file *file)
  2365. {
  2366. struct snd_pcm *pcm;
  2367. struct snd_pcm_substream *substream;
  2368. struct snd_pcm_oss_file *pcm_oss_file;
  2369. pcm_oss_file = file->private_data;
  2370. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2371. if (substream == NULL)
  2372. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2373. if (snd_BUG_ON(!substream))
  2374. return -ENXIO;
  2375. pcm = substream->pcm;
  2376. if (!pcm->card->shutdown)
  2377. snd_pcm_oss_sync(pcm_oss_file);
  2378. mutex_lock(&pcm->open_mutex);
  2379. snd_pcm_oss_release_file(pcm_oss_file);
  2380. mutex_unlock(&pcm->open_mutex);
  2381. wake_up(&pcm->open_wait);
  2382. module_put(pcm->card->module);
  2383. snd_card_file_remove(pcm->card, file);
  2384. return 0;
  2385. }
  2386. static long snd_pcm_oss_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2387. {
  2388. struct snd_pcm_oss_file *pcm_oss_file;
  2389. int __user *p = (int __user *)arg;
  2390. int res;
  2391. pcm_oss_file = file->private_data;
  2392. if (cmd == OSS_GETVERSION)
  2393. return put_user(SNDRV_OSS_VERSION, p);
  2394. if (cmd == OSS_ALSAEMULVER)
  2395. return put_user(1, p);
  2396. #if IS_REACHABLE(CONFIG_SND_MIXER_OSS)
  2397. if (((cmd >> 8) & 0xff) == 'M') { /* mixer ioctl - for OSS compatibility */
  2398. struct snd_pcm_substream *substream;
  2399. int idx;
  2400. for (idx = 0; idx < 2; ++idx) {
  2401. substream = pcm_oss_file->streams[idx];
  2402. if (substream != NULL)
  2403. break;
  2404. }
  2405. if (snd_BUG_ON(idx >= 2))
  2406. return -ENXIO;
  2407. return snd_mixer_oss_ioctl_card(substream->pcm->card, cmd, arg);
  2408. }
  2409. #endif
  2410. if (((cmd >> 8) & 0xff) != 'P')
  2411. return -EINVAL;
  2412. #ifdef OSS_DEBUG
  2413. pr_debug("pcm_oss: ioctl = 0x%x\n", cmd);
  2414. #endif
  2415. switch (cmd) {
  2416. case SNDCTL_DSP_RESET:
  2417. return snd_pcm_oss_reset(pcm_oss_file);
  2418. case SNDCTL_DSP_SYNC:
  2419. return snd_pcm_oss_sync(pcm_oss_file);
  2420. case SNDCTL_DSP_SPEED:
  2421. if (get_user(res, p))
  2422. return -EFAULT;
  2423. res = snd_pcm_oss_set_rate(pcm_oss_file, res);
  2424. if (res < 0)
  2425. return res;
  2426. return put_user(res, p);
  2427. case SOUND_PCM_READ_RATE:
  2428. res = snd_pcm_oss_get_rate(pcm_oss_file);
  2429. if (res < 0)
  2430. return res;
  2431. return put_user(res, p);
  2432. case SNDCTL_DSP_STEREO:
  2433. if (get_user(res, p))
  2434. return -EFAULT;
  2435. res = res > 0 ? 2 : 1;
  2436. res = snd_pcm_oss_set_channels(pcm_oss_file, res);
  2437. if (res < 0)
  2438. return res;
  2439. return put_user(--res, p);
  2440. case SNDCTL_DSP_GETBLKSIZE:
  2441. res = snd_pcm_oss_get_block_size(pcm_oss_file);
  2442. if (res < 0)
  2443. return res;
  2444. return put_user(res, p);
  2445. case SNDCTL_DSP_SETFMT:
  2446. if (get_user(res, p))
  2447. return -EFAULT;
  2448. res = snd_pcm_oss_set_format(pcm_oss_file, res);
  2449. if (res < 0)
  2450. return res;
  2451. return put_user(res, p);
  2452. case SOUND_PCM_READ_BITS:
  2453. res = snd_pcm_oss_get_format(pcm_oss_file);
  2454. if (res < 0)
  2455. return res;
  2456. return put_user(res, p);
  2457. case SNDCTL_DSP_CHANNELS:
  2458. if (get_user(res, p))
  2459. return -EFAULT;
  2460. res = snd_pcm_oss_set_channels(pcm_oss_file, res);
  2461. if (res < 0)
  2462. return res;
  2463. return put_user(res, p);
  2464. case SOUND_PCM_READ_CHANNELS:
  2465. res = snd_pcm_oss_get_channels(pcm_oss_file);
  2466. if (res < 0)
  2467. return res;
  2468. return put_user(res, p);
  2469. case SOUND_PCM_WRITE_FILTER:
  2470. case SOUND_PCM_READ_FILTER:
  2471. return -EIO;
  2472. case SNDCTL_DSP_POST:
  2473. return snd_pcm_oss_post(pcm_oss_file);
  2474. case SNDCTL_DSP_SUBDIVIDE:
  2475. if (get_user(res, p))
  2476. return -EFAULT;
  2477. res = snd_pcm_oss_set_subdivide(pcm_oss_file, res);
  2478. if (res < 0)
  2479. return res;
  2480. return put_user(res, p);
  2481. case SNDCTL_DSP_SETFRAGMENT:
  2482. if (get_user(res, p))
  2483. return -EFAULT;
  2484. return snd_pcm_oss_set_fragment(pcm_oss_file, res);
  2485. case SNDCTL_DSP_GETFMTS:
  2486. res = snd_pcm_oss_get_formats(pcm_oss_file);
  2487. if (res < 0)
  2488. return res;
  2489. return put_user(res, p);
  2490. case SNDCTL_DSP_GETOSPACE:
  2491. case SNDCTL_DSP_GETISPACE:
  2492. return snd_pcm_oss_get_space(pcm_oss_file,
  2493. cmd == SNDCTL_DSP_GETISPACE ?
  2494. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  2495. (struct audio_buf_info __user *) arg);
  2496. case SNDCTL_DSP_NONBLOCK:
  2497. return snd_pcm_oss_nonblock(file);
  2498. case SNDCTL_DSP_GETCAPS:
  2499. res = snd_pcm_oss_get_caps(pcm_oss_file);
  2500. if (res < 0)
  2501. return res;
  2502. return put_user(res, p);
  2503. case SNDCTL_DSP_GETTRIGGER:
  2504. res = snd_pcm_oss_get_trigger(pcm_oss_file);
  2505. if (res < 0)
  2506. return res;
  2507. return put_user(res, p);
  2508. case SNDCTL_DSP_SETTRIGGER:
  2509. if (get_user(res, p))
  2510. return -EFAULT;
  2511. return snd_pcm_oss_set_trigger(pcm_oss_file, res);
  2512. case SNDCTL_DSP_GETIPTR:
  2513. case SNDCTL_DSP_GETOPTR:
  2514. return snd_pcm_oss_get_ptr(pcm_oss_file,
  2515. cmd == SNDCTL_DSP_GETIPTR ?
  2516. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  2517. (struct count_info __user *) arg);
  2518. case SNDCTL_DSP_MAPINBUF:
  2519. case SNDCTL_DSP_MAPOUTBUF:
  2520. return snd_pcm_oss_get_mapbuf(pcm_oss_file,
  2521. cmd == SNDCTL_DSP_MAPINBUF ?
  2522. SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK,
  2523. (struct buffmem_desc __user *) arg);
  2524. case SNDCTL_DSP_SETSYNCRO:
  2525. /* stop DMA now.. */
  2526. return 0;
  2527. case SNDCTL_DSP_SETDUPLEX:
  2528. if (snd_pcm_oss_get_caps(pcm_oss_file) & DSP_CAP_DUPLEX)
  2529. return 0;
  2530. return -EIO;
  2531. case SNDCTL_DSP_GETODELAY:
  2532. res = snd_pcm_oss_get_odelay(pcm_oss_file);
  2533. if (res < 0) {
  2534. /* it's for sure, some broken apps don't check for error codes */
  2535. put_user(0, p);
  2536. return res;
  2537. }
  2538. return put_user(res, p);
  2539. case SNDCTL_DSP_PROFILE:
  2540. return 0; /* silently ignore */
  2541. default:
  2542. pr_debug("pcm_oss: unknown command = 0x%x\n", cmd);
  2543. }
  2544. return -EINVAL;
  2545. }
  2546. #ifdef CONFIG_COMPAT
  2547. /* all compatible */
  2548. static long snd_pcm_oss_ioctl_compat(struct file *file, unsigned int cmd,
  2549. unsigned long arg)
  2550. {
  2551. /*
  2552. * Everything is compatbile except SNDCTL_DSP_MAPINBUF/SNDCTL_DSP_MAPOUTBUF,
  2553. * which are not implemented for the native case either
  2554. */
  2555. return snd_pcm_oss_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  2556. }
  2557. #else
  2558. #define snd_pcm_oss_ioctl_compat NULL
  2559. #endif
  2560. static ssize_t snd_pcm_oss_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  2561. {
  2562. struct snd_pcm_oss_file *pcm_oss_file;
  2563. struct snd_pcm_substream *substream;
  2564. pcm_oss_file = file->private_data;
  2565. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2566. if (substream == NULL)
  2567. return -ENXIO;
  2568. substream->f_flags = file->f_flags & O_NONBLOCK;
  2569. #ifndef OSS_DEBUG
  2570. return snd_pcm_oss_read1(substream, buf, count);
  2571. #else
  2572. {
  2573. ssize_t res = snd_pcm_oss_read1(substream, buf, count);
  2574. pcm_dbg(substream->pcm,
  2575. "pcm_oss: read %li bytes (returned %li bytes)\n",
  2576. (long)count, (long)res);
  2577. return res;
  2578. }
  2579. #endif
  2580. }
  2581. static ssize_t snd_pcm_oss_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
  2582. {
  2583. struct snd_pcm_oss_file *pcm_oss_file;
  2584. struct snd_pcm_substream *substream;
  2585. long result;
  2586. pcm_oss_file = file->private_data;
  2587. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2588. if (substream == NULL)
  2589. return -ENXIO;
  2590. substream->f_flags = file->f_flags & O_NONBLOCK;
  2591. result = snd_pcm_oss_write1(substream, buf, count);
  2592. #ifdef OSS_DEBUG
  2593. pcm_dbg(substream->pcm, "pcm_oss: write %li bytes (wrote %li bytes)\n",
  2594. (long)count, (long)result);
  2595. #endif
  2596. return result;
  2597. }
  2598. static int snd_pcm_oss_playback_ready(struct snd_pcm_substream *substream)
  2599. {
  2600. struct snd_pcm_runtime *runtime = substream->runtime;
  2601. if (atomic_read(&substream->mmap_count))
  2602. return runtime->oss.prev_hw_ptr_period !=
  2603. get_hw_ptr_period(runtime);
  2604. else
  2605. return snd_pcm_playback_avail(runtime) >=
  2606. runtime->oss.period_frames;
  2607. }
  2608. static int snd_pcm_oss_capture_ready(struct snd_pcm_substream *substream)
  2609. {
  2610. struct snd_pcm_runtime *runtime = substream->runtime;
  2611. if (atomic_read(&substream->mmap_count))
  2612. return runtime->oss.prev_hw_ptr_period !=
  2613. get_hw_ptr_period(runtime);
  2614. else
  2615. return snd_pcm_capture_avail(runtime) >=
  2616. runtime->oss.period_frames;
  2617. }
  2618. static __poll_t snd_pcm_oss_poll(struct file *file, poll_table * wait)
  2619. {
  2620. struct snd_pcm_oss_file *pcm_oss_file;
  2621. __poll_t mask;
  2622. struct snd_pcm_substream *psubstream = NULL, *csubstream = NULL;
  2623. pcm_oss_file = file->private_data;
  2624. psubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2625. csubstream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2626. mask = 0;
  2627. if (psubstream != NULL) {
  2628. struct snd_pcm_runtime *runtime = psubstream->runtime;
  2629. poll_wait(file, &runtime->sleep, wait);
  2630. scoped_guard(pcm_stream_lock_irq, psubstream) {
  2631. if (runtime->state != SNDRV_PCM_STATE_DRAINING &&
  2632. (runtime->state != SNDRV_PCM_STATE_RUNNING ||
  2633. snd_pcm_oss_playback_ready(psubstream)))
  2634. mask |= EPOLLOUT | EPOLLWRNORM;
  2635. }
  2636. }
  2637. if (csubstream != NULL) {
  2638. struct snd_pcm_runtime *runtime = csubstream->runtime;
  2639. snd_pcm_state_t ostate;
  2640. poll_wait(file, &runtime->sleep, wait);
  2641. scoped_guard(pcm_stream_lock_irq, csubstream) {
  2642. ostate = runtime->state;
  2643. if (ostate != SNDRV_PCM_STATE_RUNNING ||
  2644. snd_pcm_oss_capture_ready(csubstream))
  2645. mask |= EPOLLIN | EPOLLRDNORM;
  2646. }
  2647. if (ostate != SNDRV_PCM_STATE_RUNNING && runtime->oss.trigger) {
  2648. struct snd_pcm_oss_file ofile;
  2649. memset(&ofile, 0, sizeof(ofile));
  2650. ofile.streams[SNDRV_PCM_STREAM_CAPTURE] = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2651. runtime->oss.trigger = 0;
  2652. snd_pcm_oss_set_trigger(&ofile, PCM_ENABLE_INPUT);
  2653. }
  2654. }
  2655. return mask;
  2656. }
  2657. static int snd_pcm_oss_mmap(struct file *file, struct vm_area_struct *area)
  2658. {
  2659. struct snd_pcm_oss_file *pcm_oss_file;
  2660. struct snd_pcm_substream *substream = NULL;
  2661. struct snd_pcm_runtime *runtime;
  2662. int err;
  2663. #ifdef OSS_DEBUG
  2664. pr_debug("pcm_oss: mmap begin\n");
  2665. #endif
  2666. pcm_oss_file = file->private_data;
  2667. switch ((area->vm_flags & (VM_READ | VM_WRITE))) {
  2668. case VM_READ | VM_WRITE:
  2669. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2670. if (substream)
  2671. break;
  2672. fallthrough;
  2673. case VM_READ:
  2674. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_CAPTURE];
  2675. break;
  2676. case VM_WRITE:
  2677. substream = pcm_oss_file->streams[SNDRV_PCM_STREAM_PLAYBACK];
  2678. break;
  2679. default:
  2680. return -EINVAL;
  2681. }
  2682. /* set VM_READ access as well to fix memset() routines that do
  2683. reads before writes (to improve performance) */
  2684. vm_flags_set(area, VM_READ);
  2685. if (substream == NULL)
  2686. return -ENXIO;
  2687. runtime = substream->runtime;
  2688. if (!(runtime->info & SNDRV_PCM_INFO_MMAP_VALID))
  2689. return -EIO;
  2690. if (runtime->info & SNDRV_PCM_INFO_INTERLEAVED)
  2691. runtime->access = SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
  2692. else
  2693. return -EIO;
  2694. if (runtime->oss.params) {
  2695. /* use mutex_trylock() for params_lock for avoiding a deadlock
  2696. * between mmap_lock and params_lock taken by
  2697. * copy_from/to_user() in snd_pcm_oss_write/read()
  2698. */
  2699. err = snd_pcm_oss_change_params(substream, true);
  2700. if (err < 0)
  2701. return err;
  2702. }
  2703. #ifdef CONFIG_SND_PCM_OSS_PLUGINS
  2704. if (runtime->oss.plugin_first != NULL)
  2705. return -EIO;
  2706. #endif
  2707. if (area->vm_pgoff != 0)
  2708. return -EINVAL;
  2709. err = snd_pcm_mmap_data(substream, file, area);
  2710. if (err < 0)
  2711. return err;
  2712. runtime->oss.mmap_bytes = area->vm_end - area->vm_start;
  2713. runtime->silence_threshold = 0;
  2714. runtime->silence_size = 0;
  2715. #ifdef OSS_DEBUG
  2716. pr_debug("pcm_oss: mmap ok, bytes = 0x%x\n",
  2717. runtime->oss.mmap_bytes);
  2718. #endif
  2719. /* In mmap mode we never stop */
  2720. runtime->stop_threshold = runtime->boundary;
  2721. return 0;
  2722. }
  2723. #ifdef CONFIG_SND_VERBOSE_PROCFS
  2724. /*
  2725. * /proc interface
  2726. */
  2727. static void snd_pcm_oss_proc_read(struct snd_info_entry *entry,
  2728. struct snd_info_buffer *buffer)
  2729. {
  2730. struct snd_pcm_str *pstr = entry->private_data;
  2731. struct snd_pcm_oss_setup *setup = pstr->oss.setup_list;
  2732. guard(mutex)(&pstr->oss.setup_mutex);
  2733. while (setup) {
  2734. snd_iprintf(buffer, "%s %u %u%s%s%s%s%s%s\n",
  2735. setup->task_name,
  2736. setup->periods,
  2737. setup->period_size,
  2738. setup->disable ? " disable" : "",
  2739. setup->direct ? " direct" : "",
  2740. setup->block ? " block" : "",
  2741. setup->nonblock ? " non-block" : "",
  2742. setup->partialfrag ? " partial-frag" : "",
  2743. setup->nosilence ? " no-silence" : "");
  2744. setup = setup->next;
  2745. }
  2746. }
  2747. static void snd_pcm_oss_proc_free_setup_list(struct snd_pcm_str * pstr)
  2748. {
  2749. struct snd_pcm_oss_setup *setup, *setupn;
  2750. for (setup = pstr->oss.setup_list, pstr->oss.setup_list = NULL;
  2751. setup; setup = setupn) {
  2752. setupn = setup->next;
  2753. kfree(setup->task_name);
  2754. kfree(setup);
  2755. }
  2756. pstr->oss.setup_list = NULL;
  2757. }
  2758. static void snd_pcm_oss_proc_write(struct snd_info_entry *entry,
  2759. struct snd_info_buffer *buffer)
  2760. {
  2761. struct snd_pcm_str *pstr = entry->private_data;
  2762. char line[128], str[32], task_name[32];
  2763. const char *ptr;
  2764. int idx1;
  2765. struct snd_pcm_oss_setup *setup, *setup1, template;
  2766. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  2767. guard(mutex)(&pstr->oss.setup_mutex);
  2768. memset(&template, 0, sizeof(template));
  2769. ptr = snd_info_get_str(task_name, line, sizeof(task_name));
  2770. if (!strcmp(task_name, "clear") || !strcmp(task_name, "erase")) {
  2771. snd_pcm_oss_proc_free_setup_list(pstr);
  2772. continue;
  2773. }
  2774. for (setup = pstr->oss.setup_list; setup; setup = setup->next) {
  2775. if (!strcmp(setup->task_name, task_name)) {
  2776. template = *setup;
  2777. break;
  2778. }
  2779. }
  2780. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2781. template.periods = simple_strtoul(str, NULL, 10);
  2782. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2783. template.period_size = simple_strtoul(str, NULL, 10);
  2784. for (idx1 = 31; idx1 >= 0; idx1--)
  2785. if (template.period_size & (1 << idx1))
  2786. break;
  2787. for (idx1--; idx1 >= 0; idx1--)
  2788. template.period_size &= ~(1 << idx1);
  2789. do {
  2790. ptr = snd_info_get_str(str, ptr, sizeof(str));
  2791. if (!strcmp(str, "disable")) {
  2792. template.disable = 1;
  2793. } else if (!strcmp(str, "direct")) {
  2794. template.direct = 1;
  2795. } else if (!strcmp(str, "block")) {
  2796. template.block = 1;
  2797. } else if (!strcmp(str, "non-block")) {
  2798. template.nonblock = 1;
  2799. } else if (!strcmp(str, "partial-frag")) {
  2800. template.partialfrag = 1;
  2801. } else if (!strcmp(str, "no-silence")) {
  2802. template.nosilence = 1;
  2803. } else if (!strcmp(str, "buggy-ptr")) {
  2804. template.buggyptr = 1;
  2805. }
  2806. } while (*str);
  2807. if (setup == NULL) {
  2808. setup = kmalloc(sizeof(*setup), GFP_KERNEL);
  2809. if (! setup) {
  2810. buffer->error = -ENOMEM;
  2811. return;
  2812. }
  2813. if (pstr->oss.setup_list == NULL)
  2814. pstr->oss.setup_list = setup;
  2815. else {
  2816. for (setup1 = pstr->oss.setup_list;
  2817. setup1->next; setup1 = setup1->next);
  2818. setup1->next = setup;
  2819. }
  2820. template.task_name = kstrdup(task_name, GFP_KERNEL);
  2821. if (! template.task_name) {
  2822. kfree(setup);
  2823. buffer->error = -ENOMEM;
  2824. return;
  2825. }
  2826. }
  2827. *setup = template;
  2828. }
  2829. }
  2830. static void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
  2831. {
  2832. int stream;
  2833. for (stream = 0; stream < 2; ++stream) {
  2834. struct snd_info_entry *entry;
  2835. struct snd_pcm_str *pstr = &pcm->streams[stream];
  2836. if (pstr->substream_count == 0)
  2837. continue;
  2838. entry = snd_info_create_card_entry(pcm->card, "oss", pstr->proc_root);
  2839. if (entry) {
  2840. entry->content = SNDRV_INFO_CONTENT_TEXT;
  2841. entry->mode = S_IFREG | 0644;
  2842. entry->c.text.read = snd_pcm_oss_proc_read;
  2843. entry->c.text.write = snd_pcm_oss_proc_write;
  2844. entry->private_data = pstr;
  2845. if (snd_info_register(entry) < 0) {
  2846. snd_info_free_entry(entry);
  2847. entry = NULL;
  2848. }
  2849. }
  2850. pstr->oss.proc_entry = entry;
  2851. }
  2852. }
  2853. static void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
  2854. {
  2855. int stream;
  2856. for (stream = 0; stream < 2; ++stream) {
  2857. struct snd_pcm_str *pstr = &pcm->streams[stream];
  2858. snd_info_free_entry(pstr->oss.proc_entry);
  2859. pstr->oss.proc_entry = NULL;
  2860. snd_pcm_oss_proc_free_setup_list(pstr);
  2861. }
  2862. }
  2863. #else /* !CONFIG_SND_VERBOSE_PROCFS */
  2864. static inline void snd_pcm_oss_proc_init(struct snd_pcm *pcm)
  2865. {
  2866. }
  2867. static inline void snd_pcm_oss_proc_done(struct snd_pcm *pcm)
  2868. {
  2869. }
  2870. #endif /* CONFIG_SND_VERBOSE_PROCFS */
  2871. /*
  2872. * ENTRY functions
  2873. */
  2874. static const struct file_operations snd_pcm_oss_f_reg =
  2875. {
  2876. .owner = THIS_MODULE,
  2877. .read = snd_pcm_oss_read,
  2878. .write = snd_pcm_oss_write,
  2879. .open = snd_pcm_oss_open,
  2880. .release = snd_pcm_oss_release,
  2881. .poll = snd_pcm_oss_poll,
  2882. .unlocked_ioctl = snd_pcm_oss_ioctl,
  2883. .compat_ioctl = snd_pcm_oss_ioctl_compat,
  2884. .mmap = snd_pcm_oss_mmap,
  2885. };
  2886. static void register_oss_dsp(struct snd_pcm *pcm, int index)
  2887. {
  2888. if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2889. pcm->card, index, &snd_pcm_oss_f_reg,
  2890. pcm) < 0) {
  2891. pcm_err(pcm, "unable to register OSS PCM device %i:%i\n",
  2892. pcm->card->number, pcm->device);
  2893. }
  2894. }
  2895. static int snd_pcm_oss_register_minor(struct snd_pcm *pcm)
  2896. {
  2897. pcm->oss.reg = 0;
  2898. if (dsp_map[pcm->card->number] == (int)pcm->device) {
  2899. char name[128];
  2900. int duplex;
  2901. register_oss_dsp(pcm, 0);
  2902. duplex = (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream_count > 0 &&
  2903. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream_count &&
  2904. !(pcm->info_flags & SNDRV_PCM_INFO_HALF_DUPLEX));
  2905. sprintf(name, "%s%s", pcm->name, duplex ? " (DUPLEX)" : "");
  2906. #ifdef SNDRV_OSS_INFO_DEV_AUDIO
  2907. snd_oss_info_register(SNDRV_OSS_INFO_DEV_AUDIO,
  2908. pcm->card->number,
  2909. name);
  2910. #endif
  2911. pcm->oss.reg++;
  2912. pcm->oss.reg_mask |= 1;
  2913. }
  2914. if (adsp_map[pcm->card->number] == (int)pcm->device) {
  2915. register_oss_dsp(pcm, 1);
  2916. pcm->oss.reg++;
  2917. pcm->oss.reg_mask |= 2;
  2918. }
  2919. if (pcm->oss.reg)
  2920. snd_pcm_oss_proc_init(pcm);
  2921. return 0;
  2922. }
  2923. static int snd_pcm_oss_disconnect_minor(struct snd_pcm *pcm)
  2924. {
  2925. if (pcm->oss.reg) {
  2926. if (pcm->oss.reg_mask & 1) {
  2927. pcm->oss.reg_mask &= ~1;
  2928. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2929. pcm->card, 0);
  2930. }
  2931. if (pcm->oss.reg_mask & 2) {
  2932. pcm->oss.reg_mask &= ~2;
  2933. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_PCM,
  2934. pcm->card, 1);
  2935. }
  2936. if (dsp_map[pcm->card->number] == (int)pcm->device) {
  2937. #ifdef SNDRV_OSS_INFO_DEV_AUDIO
  2938. snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_AUDIO, pcm->card->number);
  2939. #endif
  2940. }
  2941. pcm->oss.reg = 0;
  2942. }
  2943. return 0;
  2944. }
  2945. static int snd_pcm_oss_unregister_minor(struct snd_pcm *pcm)
  2946. {
  2947. snd_pcm_oss_disconnect_minor(pcm);
  2948. snd_pcm_oss_proc_done(pcm);
  2949. return 0;
  2950. }
  2951. static struct snd_pcm_notify snd_pcm_oss_notify =
  2952. {
  2953. .n_register = snd_pcm_oss_register_minor,
  2954. .n_disconnect = snd_pcm_oss_disconnect_minor,
  2955. .n_unregister = snd_pcm_oss_unregister_minor,
  2956. };
  2957. static int __init alsa_pcm_oss_init(void)
  2958. {
  2959. int i;
  2960. int err;
  2961. /* check device map table */
  2962. for (i = 0; i < SNDRV_CARDS; i++) {
  2963. if (dsp_map[i] < 0 || dsp_map[i] >= SNDRV_PCM_DEVICES) {
  2964. pr_err("ALSA: pcm_oss: invalid dsp_map[%d] = %d\n",
  2965. i, dsp_map[i]);
  2966. dsp_map[i] = 0;
  2967. }
  2968. if (adsp_map[i] < 0 || adsp_map[i] >= SNDRV_PCM_DEVICES) {
  2969. pr_err("ALSA: pcm_oss: invalid adsp_map[%d] = %d\n",
  2970. i, adsp_map[i]);
  2971. adsp_map[i] = 1;
  2972. }
  2973. }
  2974. err = snd_pcm_notify(&snd_pcm_oss_notify, 0);
  2975. if (err < 0)
  2976. return err;
  2977. return 0;
  2978. }
  2979. static void __exit alsa_pcm_oss_exit(void)
  2980. {
  2981. snd_pcm_notify(&snd_pcm_oss_notify, 1);
  2982. }
  2983. module_init(alsa_pcm_oss_init)
  2984. module_exit(alsa_pcm_oss_exit)