pcm_oss.c 87 KB

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