pcm_oss.c 86 KB

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